Regular Expressions

Home » Assessments » Quizzes » Regular Expressions

Regular expressions (Regex) give users grading certain question types the ability to evaluate responses against a set of acceptable values. A regular expression uses alpha-numeric and meta-characters to create a pattern that describes one or more strings that must be identically matched within a body of text.

You can use regular expressions in short answers, multi-short answers, arithmetic, significant figures, and fill-in-the-blanks questions. A regular expression uses both alpha-numeric and meta-characters in patterns that are matched with the text in the students’ answers. 

For example, the fill-in-the-blank question “What word describes red, blue, green, yellow, pink, etc.” can use regular expressions for the answer “colou?r*”

A regular expression uses both alpha-numeric and meta-characters in patterns that are matched with the text in the students’ answers. The answer key can be set to Regex for the following question types: short answer, multi-short answer, fill in the blanks, arithmetic, and significant figures

Characters used in regular expressions

CharacterDescriptionExample
\Marks the next character as a special character, a literal, a back-reference, or an octal escape.The sequence \\ matches \ and \( matches (
n matches the character n
\n matches a new-line character
^Matches the position at the beginning of the input string. If the RegExp object’s Multi-line property is set, ^ also matches the position following ‘\n’ or ‘\r’.^cat matches strings that begin with cat
$Matches the position at the end of the input string. If the RegExp object’s Multi-line property is set, $ also matches the position preceding ‘\n’ or ‘\r’.cat$ matches any string that ends with cat
*Matches the preceding character or sub-expression zero or more times.
* equals {0,}
be* matches b or be or beeeeeeeeee
zo* matches z and zoo
+Matches the preceding character or sub-expression one or more times.
+ equals {1,}.
be+ matches be or bee but not b
?Matches the preceding character or sub-expression zero or one time.
? equals {0,1}
abc? matches ab or abc
colou?r matches color or colour but not colouur
do(es)? matches the do in do or does
When this character immediately follows any of the other quantifiers (*, +, ?, {n}, {n,}, {n,m}), the matching pattern is non-greedy. A non-greedy pattern matches as little of the searched string as possible, whereas the default greedy pattern matches as much of the searched string as possible.In the string oooo, o+? matches a single o, while o+ matches all os
()Parentheses create a sub-string or item that you can apply meta-characters to.a(bee)?t matches at or abeet but not abet
{n,}n is a non-negative integer. Matches at least n times.[0-9]{3,} matches any three digits
o{2,} does not match the o in Bob, but matches the two os in food
b{4,} matches bbbb
{n}n is a non-negative integer. Matches exactly n times.[0-9]{3} matches any three or more digits
o{2} does not match the o in Bob and matches all the os in foooood
o{1} is equivalent to o+
o{0} is equivalent to o*
{n,m}m and n are non-negative integers, where n <= m. Matches at least n and at most m times.
Note: You cannot put a space between the comma and the numbers.
[0-9]{3,5} matches any three, four, or five digits
o{1,3} matches the first three os in fooooood
o{0,1} is equivalent to o?
c{2, 4} matches cc, ccc, cccc
.Matches any single character except “\n”.
To match any character including the ‘\n’, use a pattern such as ‘[\s\S]’.
cat. matches catT and cat2
(pattern)Matches pattern and captures the match. The captured match can be retrieved from the resulting Matches collection, using the SubMatches collection in VBScript or the $0$9 properties in JScript.
To match parentheses characters ( ), use ‘\(‘ or ‘\)’.
(jam){2} matches jamjam
First group matches jam
(?:pattern)Matches pattern but does not capture the match, that is, it is a non-capturing match that is not stored for possible later use.
This is useful for combining parts of a pattern with the “or” character (|).
industr(?:y|ies) is a more economical expression than industry|industries
(?=pattern)Positive lookahead matches the search string at any point where a string matching pattern begins. This is a non-capturing match, that is, the match is not captured for possible later use.
Lookaheads do not consume characters: after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead.
Windows (?=95|98|NT|2000) matches Windows in Windows 2000 but not Windows in Windows 3.1
(?!pattern)Negative lookahead matches the search string at any point where a string not matching pattern begins. This is a non-capturing match, that is, the match is not captured for possible later use.
Lookaheads do not consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead.
Windows (?!95|98|NT|2000) matches Windows in Windows 3.1 but does not match Windows in Windows 2000
x|yMatches x or y.July (first|1st|1) matches July 1st but does not match July 2
z|food matches z or food
(z|f)ood matches zood or food
[xyz]A character set. Matches any one of the enclosed characters.gr[ae]y matches gray or grey
[abc] matches the a in plain
[^xyz]A negative character set. Matches any character not enclosed.1[^02] matches 13 or 11 but not 10 or 12
[^abc] matches every letter except a in plain
[a-z]A range of characters. Matches any character in the specified range.[1-9] matches any single digit except 0
[a-z] matches any lowercase alphabetic character in the range a through z
[^a-z]A negative range of characters.
Matches any character not in the specified range.
[^a-z] matches any character not in the range a through z
\bMatches a word boundary: the position between a word and a space.er\b matches the er in never but not the er in verb
\BMatches a non-word boundary.
er\B matches the er in verb but not the er in never
\cxMatches the control character indicated by x.
The value of x must be in the range of A-Z or a-z.
If not, c is assumed to be a literal ‘c’ character.
\cM matches a Control-M or carriage return character
\d
Matches a digit character.
Equivalent to [0-9]
\DMatches a non-digit character
Equivalent to [^0-9]
\fMatches a form-feed character.
Equivalent to \x0c and \cL
\nMatches a new-line character.
Equivalent to \x0a and \cJ
\rMatches a carriage return character.
Equivalent to \x0d and \cM
\sMatches any white space character including space, tab, form-feed, etc.
Equivalent to [\f\n\r\t\v]
Can be combined in the same way as [\d\s], which matches a character that is a digit or whitespace
\SMatches any non-white space character.
Equivalent to [^\f\n\r\t\v]
\tMatches a tab character.
Equivalent to \x09 and \cI
\vMatches any word character including underscore.
Equivalent to ‘[A-Za-z0-9_]’
\WMatches any non-word character.
Equivalent to ‘[^A-Za-z0-9_]’
You should only use \D, \W and \S outside character classes.
\ZMatches the end of the string the regular expression is applied to. Matches a position, but never matches before line breaks..\Z matches k in jol\hok
\xnMatches n, where n is a hexadecimal escape value.
Hexadecimal escape values must be exactly two digits long.
Allows ASCII codes to be used in regular expressions.
\x41 matches A
\x041 is equivalent to \x04 and 1
\numMatches num, where num is a positive integer.
A reference back to captured matches.
(.)\1 matches two consecutive identical characters
\nIdentifies either an octal escape value or a back-reference.
If \n is preceded by at least n captured sub-expressions, n is a back-reference.
Otherwise, n is an octal escape value if n is an octal digit (0-7).
\11 and \011 both match a tab character
\0011 is the equivalent of 1
\nmIdentifies either an octal escape value or a back-reference.
If \nm is preceded by at least nm captured sub-expressions, nm is a back-reference.
If \nm is preceded by at least n captures, n is a back-reference followed by literal m.
If neither of the preceding conditions exists, \nm matches octal escape value nm when n and m are octal digits (0-7).
\nmlMatches octal escape value nml when n is an octal digit (0-3) and m and l are octal digits (0-7).
\unMatches n, where n is a Unicode character expressed as four hexadecimal digits.For example, \u00A9 matches the copyright symbol (©)

Turnitin recently introduced an AI tool designed to assist instructors in detecting AI-generated content in student submissions. This tool potentially analyzes writing styles and may detect anomalies that could possibly indicate the use of generative AI. However, while this technology represents a significant step forward, caution is advised in its application. It’s important to recognize that the tool is not infallible and may not catch every instance of AI-generated content.  Instructors should not rely on the AI tool to detect AI generated content, but rather, use it as one of several strategies in a comprehensive approach to uphold academic standards and encourage genuine student learning and creativity.

Regex example

250(\.0+)?\s*mL

Explanation:

250 matches exactly 250

() are used to group a sub expression

? means 0 or 1 occurrences of the previous character or subexpression

so, in our example, 0 or 1 occurrences of \.0+

. normally matches any single character \ is used to escape it, so \. literally matches a single period

+ means 1 or more occurrences of the previous character or subexpression

so, in our example 0+ means 1 or more 0s

so, (\.0+) means a period followed by 1 or more 0s

so, (\.0+)? means optionally a (period followed by 1 or more 0s

\s means exactly 1 whitespace character (space or tab)

* means 0 more occurrences of the previous character or subexpression

so, \s* means 0 or more whitespace characters

mL matches exactly mL

üSo this expression will match:

250mL

250      mL

        250mL

     250                    mL

250.0mL

250.0000000mL

        250.000000000             mL

ûIt will NOT match:

250ML

250Ml

250.0000000001       mL 251 mL

Turnitin recently introduced an AI tool designed to assist instructors in detecting AI-generated content in student submissions. This tool potentially analyzes writing styles and may detect anomalies that could possibly indicate the use of generative AI. However, while this technology represents a significant step forward, caution is advised in its application. It’s important to recognize that the tool is not infallible and may not catch every instance of AI-generated content.  Instructors should not rely on the AI tool to detect AI generated content, but rather, use it as one of several strategies in a comprehensive approach to uphold academic standards and encourage genuine student learning and creativity.

Checking Regex

To make sure that there are no mistakes in your Regex syntax, you can check your expressions at https://regexr.com/ (note: for complex ones, it’s best to also check in Brightspace by doing a preview): 

  1. Enter the REgex in the Expression field
  2. Enter the target text in the Text field; if the Regex is correct, the target text should be highlighted in blue.

Turnitin recently introduced an AI tool designed to assist instructors in detecting AI-generated content in student submissions. This tool potentially analyzes writing styles and may detect anomalies that could possibly indicate the use of generative AI. However, while this technology represents a significant step forward, caution is advised in its application. It’s important to recognize that the tool is not infallible and may not catch every instance of AI-generated content.  Instructors should not rely on the AI tool to detect AI generated content, but rather, use it as one of several strategies in a comprehensive approach to uphold academic standards and encourage genuine student learning and creativity.

Setting Up Regex Questions in Brightspace

To set up Regex questions:

  1. Go to Assessments > Quizzes > Question Library. Select New > Question type (Supported types are Short Answer, Multi-Short Answer, Fill in the Blanks, Arithmetic, Likert)
  2. Confirm you have the correct question type
  3. Enter the Question Text
    Important!!: Set the answer field to Regular Expression using the drop-down settings on the left (not setting to Regex from the default setting of Text will mean the Regex won’t work)
  4. Test at regexr.com to see if your expression will work for expected answers:
    • if it works, it will highlight the text in blue (like the example below)
    • if the text doesn’t highlight, check your expression syntax
  5. Enter your Regex into the answer field.

Turnitin recently introduced an AI tool designed to assist instructors in detecting AI-generated content in student submissions. This tool potentially analyzes writing styles and may detect anomalies that could possibly indicate the use of generative AI. However, while this technology represents a significant step forward, caution is advised in its application. It’s important to recognize that the tool is not infallible and may not catch every instance of AI-generated content.  Instructors should not rely on the AI tool to detect AI generated content, but rather, use it as one of several strategies in a comprehensive approach to uphold academic standards and encourage genuine student learning and creativity.

Check Auto-grading in Brightspace for Regex Questions

Once the questions are imported into a Quiz, do a preview of the quiz to make sure that anticipated answers are being graded as correct. This will allow you to check the Regex coding for answer keys by previewing your quiz and then grading the preview attempt.

  1. When you reach the Quiz Submission Confirmation page when previewing a quiz, you will have the option to Allow this preview attempt to be graded in the Grade Quiz area. Check to box to allow for grading and submit the quiz and then click Done on the last page of the preview.
  2. Go to Manage Quizzes and on the drop-down options next to the name of the quiz, select Grade.
  3. You will land on the Users tab. Click on the Attempts tab and click Show Search Options.
  4. In the Restrict to drop-down, select Preview Attempts.
  5. Then click the magnifying glass icon in the search field.
  6. Click on attempt link(s) to check auto-grading.
  7. For this question, the Regex was coded to allow any of “colour/color/s” as an acceptable answer. We can confirm that the sample answer has been correctly auto-graded.

Turnitin recently introduced an AI tool designed to assist instructors in detecting AI-generated content in student submissions. This tool potentially analyzes writing styles and may detect anomalies that could possibly indicate the use of generative AI. However, while this technology represents a significant step forward, caution is advised in its application. It’s important to recognize that the tool is not infallible and may not catch every instance of AI-generated content.  Instructors should not rely on the AI tool to detect AI generated content, but rather, use it as one of several strategies in a comprehensive approach to uphold academic standards and encourage genuine student learning and creativity.