Note: You are currently viewing documentation for Moodle 3.0. Up-to-date documentation for the latest stable version of Moodle may be available here: Regular Expression Short-Answer question type.

Regular Expression Short-Answer question type

From MoodleDocs

The RegExp Short Answer Question

  • IMPORTANT NOTE.-
    • The RegExp Short Answer question described in this documentation page is a 3rd-party plugin, which allows you to create questions for the Quiz activity. It is different from the "Use regular expressions" option in the Lesson module.
    • The documentation for the "Use regular expressions" option in the Lesson module is to be found at: Short answer analysis.

Like the Short Answer question, the RegExp Short Answer question expects the respondent to answer an "open" question with a word or a short phrase. However, the RegExp system system gives you access to a more powerful system for analysing the student's answers.

Correct answer matching a regular expression pattern

It is not possible to give complete examples of the vast possibilities offered by this system, and the following are just some possibilities.

Example 1. Suppose your question was "What are the colors of the French flag?". In the Answer 1 box you would type the "best" answer, e.g. "it's blue, white and red". For more details, see First correct answer below.

  • In the Answer 2 box you would type this regular expression: "it’s blue, white(,| and) red/i" (quotes should not be typed, of course).
  • This will match any of those 4 responses:
    • it’s blue, white, red
    • it’s blue, white and red
    • It’s blue, white, red
    • It’s blue, white and red

In Moodle 1.9, by default a regular expression match is case sensitive; to make the match case insensitive you must add the /i parameter right at the end of your expression. For instance the expression "it’s blue, white(,| and) red/i" would match all of the above responses plus any combination of capital letters: It's blue, white, red; IT'S BLUE, WHITE, RED, etc.

See Case sensivity below.

Example 2. Question: "What is blue, or red, or yellow?".

  • Teacher Answer: "(|it's )a colou?r".
  • This will match any of those 4 responses:
    • a colour
    • a color
    • it's a colour
    • it's a color

Note.- The beginning of this regular expression "(|it's )" will match either nothing or "it's " (i.e. "it's" followed by a space). It's also possibe to write this as "(it's )?a colou?r".The ? (question-mark) means: the preceding character (or parenthesized group of characters) zero or one time; it is used here to match British English as well as US spelling.

Example 3. Question: "Name an animal whose name is made of 3 letters and the middle letter is the vowel a".

  • Teacher Answer: "[bcr]at".
  • This will match: bat, cat or rat.

Detecting missing required words or character strings

Regular expressions alone cannot detect absent character strings, so you have to add a little code in your Answer to take care of this. Any Teacher Answer which begins with a double hyphen will analyse the student’s response to find out whether the following string is present or absent. If present, the analysis continues to the next question; if absent, the analysis stops and the relevant Feedback message is displayed.

Example 4. Question "What are the colors of the French flag?".

  • Teacher Answer 2: --.*blue.*
  • Sample student Response: "it's red and white"
  • Feedback 2: The color of the sky is missing!

Here, the . (dot) stands for “any character” and the * (asterisk) means “preceding special character repeated any number of times”. The Teacher Answer 2 regular expression above means: check whether the character string "blue", preceded with anything and followed by anything is absent from the student's answer. Please note that the use of the asterisk is different in Moodle's "normal" Short Answer question type and in the RegExp question type.

Example 5. Question: "Name an animal whose name is made of 3 letters and the middle letter is the vowel a".

  • Teacher Answer: "--[b|c|r]".
  • Feedback: "Your answer should start with one of these letters: b, c or r"

Example 6. Question "What are the colors of the French flag?".

  • Teacher Answer: "--.*(blue|red|white).*"
  • Sample student Response #1: "It's black and orange."
  • Feedback: "You have not even found one of the colors of the French flag!"
  • Sample student Response #2: "It's blue and orange."
  • Feedback: None, the analysis continues to the next Teacher Answer expression.

Explanation.- The regular expression looks for a missing word among those listed between brackets and separated by the | sign. As soon as one of those words is found, the "missing condition" is considered false, and the response analysis continues to the next Answer's regular expression.

Example 7. Question "What are the colors of the French flag?".

  • Teacher Answer: "--.*(&&blue&&red&&white).*"
  • Sample student Response #1: "It's blue and orange."
  • Feedback: "You have not found all the colors of the French flag".
  • Sample student Response #2: "white blue red".
  • Feedback: None, the analysis continues to the next Teacher Answer expression.

Explanation.- The regular expression looks for a missing word among all of those listed between brackets and separated by the && double character combination. Only if all of those words are present, will the "missing condition" be considered false, and the response analysis continue to the next Answer's regular expression. Please note that the list of parenthesized words must begin with the && character sequence.

Escaping special characters

If you need to use (in your Answers) characters which are part of the regular expressions set of special characters (or meta characters), you need to "escape" them (i.e. precede them with a backslash). E.g. if you want to accept the answer "My computer cost 1000$", you must write the regular expression as "My computer cost 1000\$". But please see First correct answer below. The special characters which must be escaped are the following

. ^ $ * ( ) [ ] + ? | { } \ /

Entering Answers in Edit Question mode (teacher)

Display Hint Button

It is possible for the teacher to select a Display Hint Button option in order to allow the student to ask for a hint. This hint will add the next correct letter to the student's input response field and - if the penalty scheme has been enabled in the Quiz settings - will cost the student one penalty factor. By default the Display Hint button value is set at No. In Moodle 1.9 & 2.0, please note that the Hint button will only be available to quizzes which have their Adaptive mode set to Yes, as it does not make sense to enable the Hint button for non-adaptive tests.

Template:Moodle 2.1In Moodle 2.1, because of a change in the scoring system, clicking the Hint button does not immediately take a penalty off the student's score. The penalty is taken from the score when the student clicks the Check button. Meanwhile, the Hint button is de-activated (greyed out), to prevent the student from getting "free Hint letters"! When the Hint button is grayed out, a ? button is displayed next to it explaining to the student that they'll have to Check their answer before the Hint button is re-activated. regexp click check button.jpg

In Moodle 2.0 each time a student clicks the Hint button to buy a letter, an invisible paragraph character ( ¶ ) is added to his response. When the teacher views the quiz results, that paragraph character is visible so the teacher knows that the Hint button was clicked for that response.

Template:Moodle 2.1In Moodle 2.1 each time a student clicks the Hint button to buy a letter, an ellipse character ( … ) is added to his response.

When the teacher views the quiz results, that character is replaced by a [HINT] tag so the teacher knows that the Hint button was clicked for that response.

On the 'Review Attempt' pages, 'History of responses' section, a [Hint] mark is displayed whenever the student clicked the Hint button.

regexp hint history.jpg

Template:Moodle 2.1In Moodle 2.1 the Hint tag is followed by the letter which was added when the student clicked the Hint button.

regexp hint history 21.jpg

Case sensitivity

Template:Moodle 1.9In Moodle 1.9, by default a regular expression match is case sensitive; to make the match case insensitive you must add the /i parameter right at the end of your expression. For instance the expression "it’s blue, white(,| and) red/i" would match all of the above responses plus any combination of capital letters: It's blue, white, red; IT'S BLUE, WHITE, RED, etc.

Template:Moodle 2.1Template:Moodle 2.0In Moodle 2.0 and 2.1, the editing form features a Case sensitivity setting, which is valid for all of the answers of the current question. You should not add an /i parameter at the end of your regular expressions. You may need to edit questions authored in 1.9 when you upgrade to 2.0 and remove any /i parameters from your regular expressions.

First correct answer

For Answer 1 you must enter an answer text which a) is the "best" possible answer; b) is not a regular expression or - more exactly - will not be interpreted as a regular expression but "as is" and c) has a Grade value of 100%. This means that you may have to edit regexp questions created prior to Feb. 2007 if they do not obey these rules. You will notice that when you create a new regexp question the Grade value for Answer 1 is already automatically set at 100%. You must not change this. If, however, you did, a warning message would be issued when you save your question, allowing you the opportunity to go back and set things right.

Note.- There are two ways to enter an answer containing meta characters, according to whether this is Answer 1 or any of the subsequent Answers. Exemple question: how much did your computer cost?

Answer 1: It cost $1,000.

Answer 2: It cost ( me)?\$1,000\.

In Anwer 1 you just type the expected answer "as is". The text in Answer 2 will be interpreted as a regular expression, and thus you need to escape the two meta characters (the $ sign and the end-of-sentence full stop). Note that here I have added the optional pronoun "me".

Other answers

Any answers with a Grade higher than 0% must be entered as valid regular expressions which can yield acceptable alternative answers (regardless of the Grade being less than 100%).

This means that some regular expressions, which are perfectly valid and would correctly analyse the student's (correct) answer are not recommended. The only case where they would work is a) if your question's Display Hint Button is set at No and b) your quiz Adaptative Mode is set at No. This means that you must not enter as an answer with a grade higher than 0% a regular expression beginning with a double hyphen "--", used for detecting missing character strings.

Previewing questions in popup window (teacher only)

When the teacher previews a question in the popup preview question window it is now possible to display all of the acceptable alternative answers. Those alternative answers are automatically generated from the regular expressions you have entered when creating the question which carry a grade higher than 0%. The very first acceptable answer is printed as is at the top of the list. This is followed by all the other alternative acceptable answers, consisting of a) the Grade attributed; b) a reminder of the regular expression you entered and c) a list of all alternative answers.

The only check done by the programme for the validity of your regular expressions consists in checking that opened parentheses and square brackets are correctly closed. If your parentheses and square brackets are not correctly closed, a warning message will be displayed showing the number of each. You should immediately edit the faulty question.

It is highly recommended to use the preview popup window systematically immediately after having created or edited a regexp type question, and to click on the Show alternate answers button in order to check the validity of one's regular expressions.

alternate answers.jpg

Automatic formatted extra feedback

When a student (or teacher in Preview Question mode) submits a response to a REGEXP question, 3 types of feedback messages are displayed (in Adaptive mode).

Template:Moodle 2.1Please note that in Moodle 2.1 the REGEXP question can only be used in Adaptive mode, regardless of the mode that the quiz it belongs to is set. You are therefore strongly advised to create quizzes containing only REGEXP questions or containing other types of questions, but preferably if the quiz's Question behaviour / How questions behave setting is set to Adaptive mode.

  • The standard correct/incorrect Quiz message (plus the color associated with either state).
  • The Feedback message entered by the question creator for each Teacher Answer.
  • An extra feedback system is automatically provided, displaying the student's submitted responses, with the following format codes:
    • the beginning of the student's submitted response which best matches one of the Alternate Answers is displayed in blue;
    • any words from the submittted response which are present in the potential Alternate Answers following the initial correct part submitted are colored in red;
    • any words not present in the potential Alternate Answers following the initial correct part submitted are colored in red and formatted as strikethrough.

The meaning of those colors etc. may need to be explained to the student before they take the quiz, especially the difference between "red" and "red plus strikethrough".

Note.- The coloring system is different in Moodle 1.9 and 2.0. The system explained and illustrated here is the one currently in use in Moodle 2.0 and (slightly different) in Moodle 2.1.

Template:Moodle 2.0

regexp colored feedback.jpg

Template:Moodle 2.1

regexp colored feedback 21.jpg

Inserting regexp sub-questions in Cloze type questions

Important notice

The RegExp question type is not recognized by the standard Moodle Cloze question type. If you want to use it you'll have to replace the question/type/multianswer/questiontype.php file from your standard Moodle with the hacked file available from the Modules and Plugins database, according to the instructions given there.

Syntax for inserting regexp sub-questions in Cloze type questions.

In Moodle 2.0, use REGEXP or shorter RX coding for questions which ignore case

  • The colors of the French flag are {:REGEXP:=blue, white and red.#Correct!}.
  • The colors of the French flag are {:RX:=blue, white and red.#Correct!}.

Will accept "it's blue, white and red." as a correct answer as well as "It's blue, white and red."

use REGEXP_C or shorter RXC coding for questions in which case matters

  • The colors of the French flag are {:REGEXP_C:=blue, white and red#Correct!}.
  • The colors of the French flag are {:RXC:=blue, white and red#Correct!}.

Will not accept "blue, white and red." as a correct answer (wrong capital letter).

Please note that, as explained above, the very first answer must be Graded 100% (in Cloze type question syntax, all correct is either = or 100%) and it must not be a regular expression.

Please note that the syntax of the sub-questions inside a Cloze-type question must be followed exactly and that you must never ever copy and paste any question text from e.g. a word-processor into the Cloze-type question editing window. Quite often Cloze-type questions yield errors because extraneous blank spaces, new lines, or any odd formatting character has made its way into the question text.

Note that the Hint button is not available for a regexp question embedded in a Cloze-type question.

See also