Note: You are currently viewing documentation for Moodle 3.9. 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 gives you access to a more powerful system for analysing the student's answers with the aim of providing more relevant immediate feedback. The RegExp Short Answer question is meant to be used with questions requiring natural language answers. This question type is not well-suited to questions requiring mathematical, scientific, programming "languages".

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" (quotes should not be typed, of course).
  • If Case sensivity is set to "No", 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

Example 2.

Question: "What are blue, red and yellow?".

  • Answer 1: "they are colours".
  • Answer 2: "(|they('| a)re )colou?rs".
  • This will match any of those 6 responses:
   colours
   colors
   they're colours
   they're colors
   they are colours
   they are colors

Note.- The beginning of this regular expression "(|they('| a)re )" will match either nothing or "they're " or "they are ". In "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 consists of 3 letters and the middle letter is the vowel a".

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

Note.- In Regular Expression syntax, the inclusion of characters between square brackets means than ANY of those characters can be used. So, in the above example, the regular expression "[bcr]at" is the exact equivalent of "(b|c|r)at". Be careful NOT to include the pipe character as separator in your [...] regular expressions. For instance, "[b|c|r]at" will NOT WORK CORRECTLY.

Example 4.

The 'permutation' feature (introduced in regexp version 2012102900 for Moodle 2.3+)

Question: "What are the colours of the French flag (in any order)".

  • Answer 1: "it's blue, white and red"
  • Answer 2: "it's [[_blue_, _white_(,| and) _red_]]".

Upon saving the question, Answer 2 will be automatically re-written as Answer 2b:

it's (blue, white(,| and) red|blue, red(,| and) white|white, red(,| and) blue|white, blue(,| and) red|red, blue(,| and) white|red, white(,| and) blue)

and it will match all the following answers:

   it's blue, white, red
   it's blue, white and red
   it's blue, red, white
   it's blue, red and white
   it's white, red, blue
   it's white, red and blue
   it's white, blue, red
   it's white, blue and red
   it's red, blue, white
   it's red, blue and white
   it's red, white, blue
   it's red, white and blue

Note.- This 'permutation feature' has been asked quite a few times by regexp users. It is definitely not possible to obtain it by using standard Regular Expressions syntax.

It is possible (but tedious) to write a regular expression including all the possible permutations - as in Answer 2b above - but the ad hoc syntax I am offering makes it easier to write... provided you strictly adhere to that syntax!

Include within double square brackets the part of the Answer which will contain 'permutable' words or phrases. You are actually allowed to have a maximum of 2 such sets of 'permutable' words or phrases. But you cannot embed one set within another!

Then, use pairs of underscores (the _ character) to delimit each 'permutable' word or phrase. You can still use any of the accepted Regular Expressions characters, as explained here, in your Answers which contain one (or two) such sets of 'permutable' words or phrases. If your Answer does not contain an even number of underscores, an Error warning will be displayed upon clicking the Show Alternate Answers button or when trying to Save your question.

Example 5.

Another 'permutation' example

Question: "Quote the English proverb that is an encouragement to hard, diligent work."

  • Answer 1: "Early to bed and early to rise makes a man healthy, wealthy and wise"
  • Answer 2: "Early to [[_bed_ and early to _rise_]], makes a man [[_healthy_, _wealthy_ and _wise_]]"

Upon saving the question, Answer 2 will be automatically re-written as Answer 2b:

Early to (bed and early to rise|rise and early to bed) makes a man (healthy, wealthy and wise|healthy, wise and wealthy|wealthy, wise and healthy|wealthy, healthy and wise|wise, healthy and wealthy|wise, wealthy and healthy)

and it will match all the following answers:

   Early to bed and early to rise makes a man healthy, wealthy and wise
   Early to bed and early to rise makes a man healthy, wise and wealthy
   Early to bed and early to rise makes a man wealthy, wise and healthy
   Early to bed and early to rise makes a man wealthy, healthy and wise
   Early to bed and early to rise makes a man wise, healthy and wealthy
   Early to bed and early to rise makes a man wise, wealthy and healthy
   Early to rise and early to bed makes a man healthy, wealthy and wise
   Early to rise and early to bed makes a man healthy, wise and wealthy
   Early to rise and early to bed makes a man wealthy, wise and healthy
   Early to rise and early to bed makes a man wealthy, healthy and wise
   Early to rise and early to bed makes a man wise, healthy and wealthy
   Early to rise and early to bed makes a man wise, wealthy and healthy

Escaping metacharacters

Definition

In the Regular Expressions syntax, a number of special characters or meta characters have special functions; but it is possible to force these special characters to be interpreted as normal (or literal) characters by preceding them with a so-called escape character, the backslash "\". Below is a (partial) list of those meta characters:

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

In Accepted Answers

  • Accepted Answers are Answers which have a grade greater than zero, i.e. are totally (grade = 100%) or partially (grade > 0% < 100%) correct Answers.

In those Answers, if you need to use one or more meta characters for their literal value, you must escape them (i.e. precede them with a backslash).

Example 1.- If you want to accept the answer "This computer costs 1000$ in the US.", you must write the Answer as "This computer costs 1000\$ in the US\.".

Example 2.- If you want to accept the answer "Desktop computers are (usually) more powerful than laptops.", you must write the Answer as "Desktop computers are \(usually\) more powerful than laptops\.".

  • You can mix metacharacters that have a special function with others that have a literal value, within one Answer.

Example 3.- If you want to accept both answers "Computers are (usually) cheaper than cars." and "Computers are (usually) less expensive than cars.", you must write the Answer as "Computers are \(usually\) (cheaper|less expensive) than cars.".

  • In the Accepted Answers boxes you can only enter regular expressions which can generate a finite number of sentences. That is why you will not be allowed to use some meta characters which match a potentially infinite number of sentences.
  • List of meta characters which you can use for their RegExp functions:

( ) [ ] ? |

  • List of meta characters which you cannot use for their RegExp functions, and can only be used for their literal value (and must be escaped).

. ^ $ * + { } \ /

  • The question mark (?) can be used either for its RegExp function OR, if escaped, for its literal value.

Example 4.- "Do you like Jack(ie)?\?" will accept both "Do you like Jack?" and "Do you like Jackie?".

In Incorrect Answers

  • Incorrect Answers are Answers which have a grade equal to zero (or None).

When you write those Incorrect Answers, you can use the whole range of meta characters for their special function value:

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

For examples of use, see Detecting missing required words or character strings below.

Answers Validation

When you validate your Question, the question engine checks the validity of your expression, according to the features explained above. If an error is found, an ERROR message is displayed above the erroneous Answer(s) and you cannot save the Question until that error has been corrected.

The validation system also checks that your parentheses and square brackets are correctly balanced.

Note.- The faulty Answer text is "underlined" with the list of errors, as shown below.

Errors en.jpg

Detecting missing required words or character strings

This is a powerful feature of the RegExp question type. It will analyse the student's answer for words that are required for the answer to be correct. There are 2 ways to do this.

  • Use what is called "negative lookahead assertion" in regular expressions syntax: ^(?!.*required.*)
  • or use an ad hoc pseudo-syntax provided in RegExp (an initial double hyphen): --.*required.*.

In the examples below, we shall be using the 'ad hoc' RegExp pseudo-syntax, and sometimes give the "negative lookahead assertion" equivalent for anyone interested.

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.

Actually, this syntax is not sufficient to track the absence of the word "blue" in a student's answer such as "it's blueish, white and red". To make sure that we want to track the absence of "blue" as a word(and not just as part of a word), we must use the metacharacter \b which is an anchor which matches at a position that is called a "word boundary". Hence the new version of our Example 4:

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

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

Note.- Using the "negative lookahead assertion" syntax mentioned at the beginning of this section, Teacher Answer 2 would look like this:

  • Teacher Answer 2: ^(?!.*\bblue\b.*)

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

  • Teacher Answer: "--^[bcr].*". OR * Teacher Answer: "--^(b|c|r).*".
  • Sample student Response: "dog"
  • Feedback: "Your answer should start with one of these letters: b, c or r"

Note.- In regular expressions syntax, the caret ^ stands for "beginning of character string to be matched", while the dollar sign $ stands for "end of character string".

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.

Note.- Using the "negative lookahead assertion" syntax, Teacher Answer would look like this: ^(?!.*(blue|red|white).*)

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.

Note.- Using the "negative lookahead assertion" syntax, Teacher Answer would look like this: (^(?!.*(blue).*)|^(?!.*(white).*)|^(?!.*(red).*))

Editing a regular expression question

regexp settings 01.jpg

Help Button Mode

Selecting a mode other than None will display a button to enable the student to get the next letter or word or punctuation mark (including the very first letter or word).

The "Word or Punctuation" help mode is a new feature starting in Moodle 3.1.

In Adaptive mode the button displayed will say "Buy next letter" or "Buy next word" or "Buy next word or punctuation" according to the mode selected by the teacher. For setting the "cost" of buying a letter or word, see the Penalty for incorrect tries and Buying a letter or word settings further down the Edit form.

In Adaptive No penalty mode the button displayed will say "Get next letter" or "Get next word" or "Get next word or punctuation".

By default the Help button mode value is set at None. The Help button will only be available to quizzes that have their Question behaviour mode set to Adaptive or Adaptive (no penalties) as it does not make sense to enable the Help button for non-adaptive tests.

Show alternate answers to student

Show all correct alternative answers to student when on review page? If there are a lot of automatically generated correct alternative answers, displaying them all can make the review page quite long. So, you may wish to not display all those alternative correct answers. The first correct answer will always be displayed, under the label "The best correct answer is:"

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%. You will notice that when you create a new RegExp question the Grade value for Answer 1 is already automatically set at 100% and cannot be changed.

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%).

For example, you cannot enter the following Answer with a grade greater than zero:

.*blue, white(,| and) red.*

The reason is that this expression would accept as correct (with a non-null grade) an infinity of answers, many of which would be incorrect, e.g.: "My hat it blue, white, red and orange", "The French flag is blue, white, red, black and nice" etc.

If you try to do so, validation of your question will fail and an error message will be displayed to tell you where you went wrong.

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.

Show/Hide alternate answers

When you are creating (or modifying) a RegExp question, you may want to make sure that all the alternative correct answers that you have created in the Answers fields will work. You can click the Show alternate answers button to calculate and display all the correct answers in the form you are editing. This may take quite some time on your server, depending on the number and complexity of the regular expressions you have entered in the Answer fields!

On the other hand, it is the recommended way to check that your "correct answers" expressions are correctly written. Here is an example.

Please remember that only Answers regular expressions with a score greater than zero will be used to calculate those alternative answers.

Please note that clicking the Show alternate answers button will perform an analysis of all the regular expressions you entered in the Answers field. If a syntax error is detected at this stage, the alternative correct answers will not be displayed, and an ad hoc error message will displayed above the faulty regular expression.

showhidealternateanswers.jpg

Automatic formatted extra feedback

Please note that the RegExp question can be used in any Question behaviour mode. However, it is 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 (with or without penalty).

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).

  • (line 3) The standard correct/incorrect Quiz message (plus the colour associated with either state).
  • (line 2) The Feedback message entered by the question creator for each Teacher Answer.
  • (line 1) An extra feedback system is automatically provided, displaying the student's submitted response, 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 submitted response which are present in the potential Alternate Answers following the initial correct part submitted (correct but misplaced words) are displayed on a green background;
    • any words not present in the potential Alternate Answers following the initial correct part submitted (Wrong words) are displayed on a red background.

The meaning of those colours is explained below the feedback with the 2 labels "Wrong words" and "Misplaced words".

Please note that the colour scheme has been changed starting with the Moodle 3.1 version of RegExp.

regexp04.jpg

Feedback given by the Help button

Each time a student clicks the Buy/Get next letter/word/punctuation button to buy/get a letter/word/punctuation mark, that letter, word or punctuation mark is added to their response. The last line of the feedback zone shows the following information: added letter/word; penalty cost (if applicable); total penalties so far (if applicable). Note that if the total of penalties exceeds 1 (i.e. 100%), that total is displayed in red.

When the teacher views the quiz results, on the 'Review Attempt' pages, 'Response history' section, the response history shows Submit (with a request for help) with the response states before and after the letter/word/punctuation mark was added.


regexp03.jpg

Display right answers

If your Quiz settings Review options are set to display the Right answer (During the attempt or Immediately after the attempt etc.), and your question's Show alternate answers to student setting is set to Yes, when the student has submitted his attempt, and is reviewing his answers, all of the possible answers will be displayed, as shown in this screenshot. Correct responses with a grade < 100% are also listed, with their grade value.

Please note that the teacher will always be able to see that "other accepted answers" section when reviewing the Quiz answers.

23 correct responses.jpg

In the Mobile App

Starting with the Moodle 3.5 version, RegExp includes code for the Moodle Mobile App. If you access a quiz with the mobile app that contains RegExp questions it will be automatically loaded as a remote add-on.

regexp05.jpg

Similar to the short answer core question type instructions, "Normally the answer box appears below the question text. However, if you include five or more underscores in the text, the input box will be placed there." This inline input feature has recently been made available to the Moodle mobile version for "short answer" questions. It is now available for the "regexp" question type as well.

If you enter this regexp question text: The ____________ sat on the white mat.

This is what it will look like in a moodle mobile quiz:

2019-06-04 18-40-30.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 2 files (renderer.php and questiontype.php) on your <yourmoodle>/question/type/multianswer with the hacked files available from the links below. UPdated 17 AUGUST 2020.

https://raw.githubusercontent.com/rezeau/moodle_multianswer_regexp_compatible/master/questiontype.php

https://raw.githubusercontent.com/rezeau/moodle_multianswer_regexp_compatible/master/renderer.php

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

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 "blue, white and red" as a correct answer as well as "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 letters).

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.

A more complete example. If you enter the following in the text of a Cloze question:

The colors of the French flag are {:REGEXP:=blue, white and red#Very correct indeed!~--.*\bblue\b.*#The color of the sky is missing!~--.*(blue|red|white).*#You have not even found one of the colors of the French flag!}.

and click the Decode and verify the question text button, you will see this:

Question definition {:REGEXP:=blue, white and red#Very correct indeed!~--.*(blue|red|white).*#You have not even found one of the colors of the French flag!~--.*\bblue\b.*#The color of the sky is missing!} Default mark 1 Answer blue, white and red Grade 1 Feedback Very correct indeed! Answer --.*(blue|red|white).* Grade 0 Feedback You have not even found one of the colors of the French flag! Answer --.*\bblue\b.* Grade 0 Feedback The color of the sky is missing! regexp in cloze question.jpg

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

Downloads

Installation


If you have downloaded the zip archive from the new moodle.org plugins page

1.- Unzip the zip archive to your local computer.

2.- This will give you a folder named "regexp".

3.- GO TO STEP 4 below ---

If you have downloaded the zip archive from https://github.com/rezeau/moodle-qtype_regexp (for latest developments)

1.- Unzip the zip archive to your local computer.

2.- This will give you a folder named something like "rezeau-moodle_qtype_regexp-ff8c6a1". The end of the name may vary.

3.- ***Rename*** that folder to "regexp".

---

4.- Upload the regexp folder to <yourmoodle>/question/type/ folder.

5.- Visit your Admin/Notifications page so that the new question type gets installed.

Learn more about regular expressions

See also these other Moodle question types based on regular expressions