Regular Expression Short-Answer question type

Jump to: navigation, search

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

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" (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.

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.

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

. ^ $ * + ? { } \ /

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 engine also checks that your parentheses and square brackets are correctly balanced.

Note.- The Validation engine works better in the Moodle 2.1 version of the REGEXP question type than in previous versions. The faulty Answer text is "underlined" with the list of errors, as in the attached screenshot. Errors en.jpg

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: "--[bcr]". OR * 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.

Entering Answers in Edit Question mode (teacher)

Display Hint/Help Button

It is possible for the teacher to select a Display Hint Button (in 2.1 a Display Help Button) option to enable the student to "buy" the next correct letter. This letter will be added 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. 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. In Moodle 2.1, the Help button will only be available to quizzes that have their Question behaviour mode set to Adaptive or Adaptive (no penalties).

Case sensitivity

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.

Moodle 2.1

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

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

Moodle 2.1

Please note that in Moodle 2.1 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).

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

regexp colored feedback.jpg

Moodle 2.1regexp colored feedback 21.jpg

Feedback given by the Help button

Moodle 2.1

In Moodle 2.1 each time a student clicks the Help me please button to buy a letter, that letter is added to his response. The 3rd line of the feedback zone shows the following information: added letter; penalty cost; total penalties so far. 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, 'History of responses' section, the response history shows Submit (with a request for help) with the response states before and after the letter was added.

21-addedletterhistory.jpg

Display right answers

Moodle 2.1

If your Quiz settings Review options are set to display the Right answer (During the attempt or Immediately after the attempt etc.), 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.

21 correct responses.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(s) available from the Moodle Plugins repository, according to the instructions given there.

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

Moodle 2.1

In Moodle 2.0 or 2.1, 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.

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

Learn more about regular expressions

Other Moodle question types based on regular expressions

The OUP's PMatch

Oleg Sychev's Preg question type