D2L Brightspace Tips: Use Regular Expressions in Quizzes to Accept a Range of Numbers
If you need to accept a range of numbers, such as 5-15, as the possible answers to a quiz question, you will need to use regular expressions (or regex) in certain question types in D2L. For example, in a short answer question, click abc to open the drop-down and choose Regular Expression from the list. You may be tempted to enter something like [5-15], but Regular Expressions search for a match based on individual digits. Therefore, you must account for each individual digit in your regex.
Single Digits
The regex for 5-9 is relatively simple: ^[5-9]$
The ^ and $ symbols are just to denote the beginning and end of the line. Without them, any of those numbers within a larger number (such as the 5 in 15 or 150) would technically be a match. This will also ensure that students only type numbers as their answer and do not enter other text along with the numbers. See below if you do want students to enter units as part of their answer.
Multiple Digits
If you want to include numbers that are more than one digit, such as 5-15, you will need to use the pipe symbol | which means or, as well as parentheses to treat the alternatives as one group with regards to the beginning and end of line.
Accept any number from 5 to 15: ^(?:[5-9]|1[0-5])$
Without the parentheses, ^ would only apply to [5-9] and $ would only apply to 1[0-5]. Note the question mark and colon after the opening parenthesis. This signifies that the patterns within the parentheses are not captured or stored for later use by any code. The ?: is actually not strictly needed within quizzes since D2L is only evaluating rather than capturing any matches, but it is best practice to include it. This is more useful when using regular expressions to extract certain patterns from documents, for example.
How about the range 1-100? ^(?:[1-9]|[1-9][0-9]|100)$
For 1000-1500, use: ^(?:1[0-4][0-9][0-9]|1500)$
Optional Commas
You may also want to allow for commas in the answer when you get to four digits. In that case, add ,? to the regex. This second use of the ? is different from the first where it refers to a non-capturing group. It just means 0 or 1 occurences of the preceding character, which is the comma in this example.
^(?:1,?[0-4][0-9][0-9]|1,?500)$
Word Boundaries and Units
If you want to accept answers that might have other text, replace ^ and $ with \b to denote word boundaries.
The regex for 5-9 among other text is: \b[5-9]\b
This would mean that the response "it costs 5 dollars" would be accepted as correct because 5 is indeed found somewhere in the entire reponse.
A slightly more complicated scenario accounts for units. If any number from 5 to 9 is acceptable as well as an optional use of the unit mm, with or without a space after the number, and millimeters with a space are all acceptable answers, you could use a regex like this:
\b[5-9](?: ?mm| millimeters)?\b
The second ? makes the space before mm optional, but notice that a space is entered directly into the regex before millimeters which makes is required. The last ? also makes either unit optional. It does not account for case, however, so MM or Millimeters would not match.
Regex in Quiz Results Displays
One drawback to using regex in D2L quizzes is that the actual regex will show to students if you setup a quiz results display to include the correct answers. You will need to Add Feedback to each question with the correct answer if your students are not familiar with regular expressions.
More Resources
You can test out writing regexes using websites like regex101.com or asking your favorite AI chatbot to help you construct them. The Brightspace Community also has a list of characters that can be used in regular expressions within D2L.