Learn@UW - Using Regular Expressions and Case Sensitivity Issues for Quiz Questions

This document describes regular expressions and how to use them in quiz questions in the Question Library.

A regular expression is a pattern of text that consists of ordinary characters that match themselves literally (letters a-z and A-Z, digits 0-9, the underscore, and a few other characters) and characters similar to wild card characters, known as metacharacters. The pattern describes one or more strings to match when searching a body of text. The regular expression serves as a template for matching a character pattern to the string being searched.

It is often convenient to use a regular expression to include multiple possible answers to a quiz question instead of specifying a separate answer for each possibility. For example, if we want to allow the answers "gray" and "grey" to the question "What color paint do we get when we mix black paint with white paint?", we could use the regular expression

gr[a|e]y

where the [a|e] part matches either a or e. Both gray and grey would be flagged as correct answers.

A complete table of all the metacharacter groupings and their behavior in the context of regular expressions is given in Desire2Learn, Using Regular Expressions in the D2L Quizzing Tool. An abbreviated table giving many of the common metacharacter groupings is given at the end of this document.

Using Regular Expressions in Quiz Questions

Regular expressions can be used in the possible answers for Short Answer, Fill In the Blank, and Multi-Short Answer questions. They can also be used for Units in calculation type questions, i.e., Arithmetic and Significant Figures questions.

To use regular expressions in possible answers for Short Answer, Fill In the Blank, or Multi-Short Answer questions, select the Regular Expression option under "Evaluation" for each answer that you want to use a regular expression:

Short Answer Example
02546-SA90.jpg

Fill In the Blank Example
02546-FIB90.jpg
Multi-Short Answer Example
02546-MSA90.jpg

To use regular expressions in Units for calculation type questions, select the Regular Expression option under "Units".

Example
02546-U90.jpg

Formatting Issues

Case Sensitivity

When regular expressions are used, answer matching is case-sensitive. For example, if the regular expression in the answer box contains [H|h]ot dog, then the answers Hot dog and hot dog will be flagged correct but HOT DOG and Hot Dog will be flagged incorrect.

If regular expressions are not used, answer matching is case-sensitive if the Case Sensitive option is selected and not case-sensitive if the Case Insensitive option is selected.

02546-Z90.jpg

If the answer box contains hot dog and the Case Sensitive option is selected, Hot dog, Hot Dog, and HOT DOG will be flagged incorrect. If the Case Insensitive option is selected, all of the above as well as HoT dOg and other combinations of lower-case and upper-case letters will be flagged correct.

Extra Spaces

White space is disregarded in an answer. In the above example, we could have two or more, instead of one, space between hot and dog and the answer would be flagged correct. We could also have space(s) before hot or after dog and the answer would be flagged correct.

Table of Common Metacharacter Groupings

Metacharacter
Grouping
Description Example
\t A backslash escapes the metacharacter t, to match that character literally. \( matches the character (.
. A period matches any single character except a newline character (see below). r.ce matches race and rice but not royce.
* An asterisk matches the preceding character or subexpression 0 or more times. x7* matches x, x7, and x77777.
+ An plus matches the preceding character or subexpression 1 or more times. zo+ matches zo and zoo but not z.
? A question mark matches the preceding character or subexpression 0 or 1 times. mo?n matches mn and mon but not moon.
^ A caret matches the position at the beginning of the input string. ^cat matches any string that begins with cat.
$ A dollar sign matches the position at the end of the input string. cat$ matches any string that ends with cat.
( subexpression ) Parentheses create a subexpression that metacharacters can be applied to. to(ne)? matches to and tone but not ton.
{n} A number n in curly braces matches the preceding character or subexpression exactly n times. [0-9]{3} matches any 3 digits.
{n1,n2} Two comma separated numbers n1 and n2 in curly braces matches the preceding character or subexpression between n1 and n2 times. [0-9]{2,4} matches any 2, 3, or 4 digits.
exp1 | exp2 If two or more strings or subexpressions are separated by vertical bars, matches either of the strings or subexpressions (l|m)oon matches loon or moon.
Windows (ME|2000|XP) matches Windows ME or Windows 2000 or Windows XP.
[abc] If two or more characters are enclosed in square brackets, matches any one of the enclosed characters. b[aeiu]t matches bat, bet, bit, or but.
1[36]* matches 1, 13, 166, and 13336366.
[^abc] A preceding caret means match any character except one of the enclosed characters. ca[^br] matches can and cat but not cab or car.
[a-b] If two characters are separated with a hyphen, matches a range of characters, according to the ASCII collating sequence. [1-9] matches any digit except 0.
[^a-b] A preceding caret means match any character except one in the specified range of characters, according to the ASCII collating sequence. [^a-z] matches any character not in the range a through z.
\n A backslash followed by an n matches a newline character.  
\s A backslash followed by an s matches any whitespace character including space, tab, and form-feed  
\xuv A backslash followed by x and two hexadecimal digits uv matches the character whose ASCII code is uv \x41 matches A.



Keywords:brightspace learn@uw leanuw d2l desire2learn regular expression pattern match metacharacter case sensitive sensitivity format quiz question string space   Doc ID:2546
Owner:Susan D.Group:Learn@UW Utility
Created:2004-03-01 19:00 CDTUpdated:2015-06-04 11:48 CDT
Sites:DoIT Help Desk, DoIT Tech Store, Learn@UW Madison, Learn@UW Utility
Feedback:  15   10