Regular Expression Short-Answer question type

Revision as of 14:04, 8 February 2011 by Joseph Rézeau (talk | contribs) (moved wiki page from question/type/regexp (to be redirected here))

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.

The RegExp Short Answer Question

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 is "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

Please note that 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.

Example 2. Question: "What is blue, or red, or yellow?". Answer: "(|it's )a colou?r". This answer will match:

  • a colour
  • a color
  • it's a colour
  • it's a color

Notes.- The beginning of this regular expression "(|it's )" will match either nothing or "it's " (i.e. "it's" followed by a space). The ? (question-mark) means: preceding character 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". Anwer: "[bcr]at". This will match: bat, cat and 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.

  • Answer 2: --.*blue.*/i
  • student answer: "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 Answer2 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 the 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"

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 tick a Display Hint Button box 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. When the student takes the quiz, mousing over the Hint button will display how much it will cost to "buy" a letter. By default Display Hint button value is set at No. Questions created prior to Feb. 2007 will have this No value. If you want to display the Hint button you'll have to edit those questions and select Yes and save. 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.

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.

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.

However this does not totally protect you from other errors. If you made an error in one of your regular expressions, either of the following can happen: a) a PHP error notice or warning will be displayed, something like [Fatal error: Maximum execution time of 30 seconds exceeded etc. ] or b) no error will be displayed but you will see by yourself that the alternative answers generated from your faulty regexp are in fact not acceptable. You should of course immediately edit the problem question, trying to find and correct the faulty regular expression. Hopefully a more robust and user-friendly checking mechanism will be available soon.

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.

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.

The syntax for inserting regexp sub-questions in Cloze type questions is as shown in the following examples.

  • The colors of the French flag are {:REGEXP:=blue, white and red#Correct!~=it’s blue, white(,| and) red#Correct!~--.*blue.*#The color of the sky is missing.}
  • The colors of the French flag are {:REGEXP:%100%blue, white and red#Correct!~%100%it’s blue, white(,| and) red#Correct!~--.*blue.*#The color of the sky is missing.}

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. If you forget to give a 100% grade to the very first answer, a warning message will be displayed upon saving the question: "Answer 1 must be correct and its grade must be 100%. Do you want to go back and correct this?". However, if you see this message, you must not click the '''Yes''' button, because it would not take you back to your Cloze type editing window. You must click No and immediately edit the faulty Cloze question and correct the problem.

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.

Unfortunately the Hint button is not available for a regexp question inserted into a Cloze-type question.

See also