Auto Grading with Regular Expressions
On This Page
Learn about Regular Expressions
Note
If you are using a screen reader make sure that punctuation reading settings are set to read for voice symbols that are found in this article.
Learn About Regular Expressions
Step 1
Regular Expressions (RegEx) are algorithms that can machine-grade short answer, fill-in-the-blank, arithmetic, and significant figures questions.
Step 2
Review commands:
Example 1: Case Insensitive Command (?i)
Question: Natural carmine dye is made from the _____ insect.
RegEx: (?i)cochineal
Accepted answer/s: Cochineal, cochineal, CoChInEaL
Example 2: Either Or Command | (Pipe)
Question: What sound does a cat make?
RegEx: Meow|Purr|Hiss
Accepted answer/s: Meow, Purr, or Hiss
Example 3: Replace any literal character. (Period)
Question: What three-letter "spooky" animal is associated with Halloween?
RegEx: .at
Accepted answer/s: Bat, Cat, or Rat
Example 4: Define the Beginning and End of a Pattern ^ (Caret) and $ (Dollar)
Question: Magnetic resonance imaging is abbreviated as ______.
RegEx: ^MRI$
Accepted answer: MRI - only "MRI" with no additional characters or spaces would be scored correctly.
Note
RegEx commands will match any portion of a pattern unless specified with anchor commands like ^ (Caret) and $ (Dollar). For example, yes matches any answer with the word "yes" in it, e.g., yes, yes please, and my answer is yes. Again, use anchors to limit matches.
Common Regular Expressions
Character | Name | Description | Example |
---|---|---|---|
* | Asterisk | Matches the preceding character zero or more times. | cat* matches ca, cat, or catttt |
\ | Backslash | Marks a metacharacter as literal. | c\.t matches c.t but not cat |
^ | Caret | Starts the input string or pattern. | ^cat matches cat, catnip, but not tomcat |
$ | Dollar | Ends the input string or pattern. | cat$ matches cat, tomcat, but not catnip. ^cat$ matches only cat |
(?i) | NA | Makes the pattern case insensitive. | (?i)cat matches CAT, cat, or CaT |
( ) | Parentheses | Creates a subexpression. | wild(cat)* matches wild or wildcat |
. | Period | Matches any literal character. | .at matches cat, bat, or rat |
| | Pipe | Matches either or. | cat|dog matches cat or dog. |
Step 3
For more information review D2L Brightspace Documentation: Understanding Regular Expressions.
Learn about the RegEx Generator Tool (Creates Code for You)
The RegEx Generator offers four templates that appear as tabs. The templates are designed to fit common use cases for Regulation Expressions. These include:
- The Multiple Correct Answers template, used to account for multiple, correct answers.
- The Multiple Separators template, used when spaces, dashes, slashes, or any single character might separate words in an answer.
- The Multiple Suffixes template, used when answers may have different endings.
- The Diacritics template, used when answers may contain diacritical marks.
Use the RegEx Generator Tool
Step 1
Navigate to the RegEx Generator tool.
Step 2
Decide whether accepted answers will be case-sensitive. Leave the Case-sensitive checkbox unchecked if you do not want capitalization to matter. Checkmark the box if you want answers to match the capitalization you specify.
Step 3
Select the template you want to use.
Note
All of the templates operate similarly. Any specific instructions for usage can be found in the template.
Step 4
Enter correct answer(s). The number of input fields will expand as needed for the Multiple Correct Answers Template.
Step 5
As correct answer(s) are entered, the RegEx code will manifest in the designated area below.
Step 6
Check the RegEx in the Test area by entering correct and incorrect answers.
Note
The RegEx Generator is an evolving tool. Check back often for updates and new templates.
Insert Regular Expressions into D2L Quizzes
Step 1
Follow instructions to Create Open-Ended Questions or access an already existing question in Edit mode.
Step 2
Locate the answer text field.
Step 3
Type the RegEx into the answer text field. If you generated the RegEx from the RegEx Generator Tool, paste the RegEx into the answer text field.
Step 4
For short answer questions, select the Actions Menu icon to the left of the answer text field, then choose Regular Expression. For fill-in-the-blank questions, select the Regular Expression option from the Evaluation column.
Note
You must indicate that the supplied answer is a Regular Expression. If you do not complete Step 4, D2L will treat the code as the literal, correct answer. Be sure that Regular Expression is selected.
Step 5
Select Save.