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: Difference between revisions

From MoodleDocs
m (Text replacement - "class="nicetable"" to "class="wikitable"")
 
(56 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Infobox plugin
|type = question type
|entry = https://moodle.org/plugins/pluginversions.php?plugin=qtype_regexp
|tracker = https://github.com/ndunand/moodle-qtype_regexp/issues
|discussion = https://moodle.org/plugins/qtype_regexp
|maintainer = [[user:Nicolas Dunand]]
|float = right
}}{{Questions}}
===The RegExp Short Answer Question===
===The RegExp Short Answer Question===


* '''IMPORTANT NOTE'''.-
* '''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 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: [https://docs.moodle.org/en/Short_answer_analysis Short answer analysis].
** The documentation for the "Use regular expressions" option in the '''''Lesson''''' module is to be found at: [https://docs.moodle.org/en/Short_answer_analysis 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.
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 ''analyzing the student's answers'' with the aim of ''providing more relevant immediate feedback''.


===Correct answer matching a regular expression pattern===
===Correct answer matching a regular expression pattern===
Line 11: Line 19:
It is not possible to give complete examples of the vast possibilities offered by this system, and the following are just some possibilities.
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 [[#firstcorrect|First correct answer]] below.
===='''Example 1.'''====


*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).
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 [[#firstcorrect|First correct answer]] below.
* 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.
*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 [[#casesensivity|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


See [[#casesensivity|Case sensivity]] below.
===='''Example 2'''.====


'''Example 2'''. Question: "What is blue, or red, or yellow?".  
Question: "What are blue, red and yellow?".  
* Teacher Answer: "(|it's )a colou?r".  
* Answer 1: "they are colours".
* This will match any of those 4 responses:
* Answer 2: "(|they('| a)re )colou?rs".  
** a colour
* This will match any of those 6 responses:
** a color
    colours
** it's a colour
    colors
** it's a color
    they're colours
    they're colors
    they are colours
    they are colors


'''''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.  
'''''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 is made of 3 letters and the middle letter is the vowel ''a''".
===='''Example 3.'''====
* Teacher Answer: "[bcr]at".  
 
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.
* 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: <nowiki>"it's [[_blue_, _white_(,| and) _red_]]".</nowiki>
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 an ma healthy, wealthy and wise"
* Answer 2: "Early to <nowiki>[[_bed_ and early to _rise_]], makes a man [[_healthy_, _wealthy_ and _wise_]]</nowiki>"
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.
[[Image:Errors_en.jpg]]


===Detecting missing required words or character strings===
===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.
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?".
'''Example 4. ''' Question "What are the colors of the French flag?".
Line 50: Line 184:
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.
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''".  
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:
* Teacher Answer: "--[b|c|r]".  
 
'''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: '''^(?!'''.*\blue\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"
* 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?".
'''Example 6.''' Question "What are the colors of the French flag?".
Line 63: Line 213:


'''''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.
'''''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?".
'''Example 7.''' Question "What are the colors of the French flag?".
Line 74: Line 227:
'''''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.
'''''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===
'''Note.-'''
 
Using the "negative lookahead assertion" syntax, Teacher Answer would look like this: '''(^(?!.*(blue).*)|^(?!.*(white).*)|^(?!.*(red).*))'''
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 [[#firstcorrect|First correct answer]] below. The special characters which must be escaped are the following
 
'''. ^ $ * ( ) [ ] + ? | { } \ /'''


===Entering Answers in Edit Question mode (teacher)===
===Editing a regular expression question===


====Display Hint Button====
[[Image:settings01.jpg]]


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.
====Help Button Mode====


{{Moodle 2.1}}In 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
Selecting a mode other than ''None'' will display a button to enable the student to get the next letter or word (including the very first letter or word).
that they'll have to Check their answer before the Hint button is re-activated.
[[Image: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.
In ''Adaptive mode'' the button displayed will say "Buy next letter" or "Buy next word" 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.
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.


{{Moodle 2.1}}In Moodle 2.1 each time a student clicks the Hint button to buy a letter, an ellipse character ( … ) is added to his response.
In ''Adaptive No penalty'' mode the button displayed will say "Get next letter" or "Get next word"


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.
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.
 
On the ''''Review Attempt'''' pages, ''''History of responses'''' section, a [Hint] mark is displayed whenever the student clicked the Hint button.
 
[[Image:regexp_hint_history.jpg]]
 
{{Moodle 2.1}}In Moodle 2.1 the Hint tag is followed by the letter which was added when the student clicked the Hint button.
 
[[Image:regexp_hint_history_21.jpg]]


====<span id="casesensivity">Case sensitivity</span>====
====<span id="casesensivity">Case sensitivity</span>====


{{Moodle 1.9}}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.  
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.


{{Moodle 2.1}}{{Moodle 2.0}}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.
====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:"


====<span id="firstcorrect">First correct answer</span>====
====<span id="firstcorrect">First correct answer</span>====


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.
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?
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?
Line 126: Line 266:


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


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.
[[Image:showhidealternateanswers.jpg]]


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.
===Previewing questions in popup window (teacher only)===


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.
When the teacher previews a question in the popup preview question window they can 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.


[[Image:alternate_answers.jpg]]
[[Image:alternate_answers.jpg]]


===Automatic formatted extra feedback===
===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).
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 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''.
* (line 3) The standard correct/incorrect Quiz message (plus the color associated with either state).
* The standard correct/incorrect Quiz message (plus the color associated with either state).
* (line 2) The Feedback message entered by the question creator for each Teacher Answer.
* 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:
* 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;
** 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 from the submitted 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.
** any words not present in the potential Alternate Answers following the initial correct part submitted are colored in red and formatted as strike-through.
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".
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 strike-through".
 
[[Image:regexp_colored_feedback_21.jpg]]
 
===Feedback given by the Help button===
 
Each time a student clicks the '''Buy/Get next letter/word''' button to buy/get a letter/word, that letter or word is added to his 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, ''''History of responses'''' section, the response history shows ''Submit (with a request for help)'' with the response states before and after the letter/word was added.
 
[[Image:21-addedletterhistory.jpg]]
 
----
[[Image:21-addedwordhistory.jpg]]
----
If the student clicked the '''Buy/Get next word''' button while his current submitted answer contained the beginning of a (correct) word, the full correct word is displayed in the Answer field, and the feedback message says "''Completed'' word" rather than "''Added'' word".


'''''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.
[[Image:21-completedwordhistory.jpg]]


{{Moodle 2.0}}
===Display right answers===


[[Image:regexp_colored_feedback.jpg]]
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.


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


[[Image:regexp_colored_feedback_21.jpg]]
[[Image:23 correct responses.jpg]]


===Inserting regexp sub-questions in Cloze type questions===
===Inserting RegExp sub-questions in Cloze type questions===


{| class="nicetable"
{| class="wikitable"
|-
|-
!Important notice
!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 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.
 
https://raw.github.com/rezeau/moodle/multianswer_regexp-26/question/type/multianswer/questiontype.php
 
https://raw.github.com/rezeau/moodle/multianswer_regexp-26/question/type/multianswer/renderer.php
 
Note.- If you are interested in comparing the 2 "standard" Moodle 2.6 multianswer files with the "RegExp hacked" files, go to  
 
https://github.com/rezeau/moodle/compare/MOODLE_26_STABLE...multianswer_regexp-26
|}
|}


Syntax for inserting regexp sub-questions in Cloze type questions.
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
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 {:REGEXP:=blue, white and red#Correct!}.
* The colors of the French flag are {:RX:=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."
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
use '''REGEXP_C''' or shorter '''RXC''' coding for questions in which case matters
Line 185: Line 364:
* The colors of the French flag are {:RXC:=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).
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, 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.
Line 191: Line 370:
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.
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.
Note that the ''Hint'' button is not available for a RegExp question embedded in a Cloze-type question.


==See also==
==See also==
* Download [http://moodle.org/mod/data/view.php?d=13&rid=338 the Regular Expression Short-Answer question type] from the Moodle Modules & plugins page.
====Downloads====
* Download [http://moodle.org/plugins/view.php?plugin=qtype_regexp the Regexp question type] from the Moodle Plugins repository.
* IMPORTANT : Starting with the 2.2 version of REGEXP, if you want the Help feature, you must also download and install the following 2 "question behaviours" from the Moodle Plugins repository: [http://moodle.org/plugins/view.php?plugin=qbehaviour_regexpadaptivewithhelp RegExp Adaptive mode with Help]  and [http://moodle.org/plugins/view.php?plugin=qbehaviour_regexpadaptivewithhelpnopenalty RegExp Adaptive mode with Help (no penalties)].
 
====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====
 
*[http://www.regular-expressions.info/tutorial.html Regular Expressions Tutorial] A complete introduction to the topic.
*[http://www.regular-expressions.info/tutorial.html Regular Expressions Tutorial] A complete introduction to the topic.
*[http://www.regexplanet.com/simple/index.html Regular Expression Test Page] Test your regular expressions on a variety of "answers". Very useful for this question type; will save you a lot of time.
*[http://www.regexplanet.com/simple/index.html Regular Expression Test Page] Test your regular expressions on a variety of "answers".
[[fr:question/type/regexp]]
 
[[Category:Teacher]]
====Other Moodle question types based on regular expressions====
[https://docs.moodle.org/dev/The_OU_PMatch_algorithm The OUP's PMatch]
 
[https://docs.moodle.org/20/en/Preg_question_type Oleg Sychev's Preg question type]
 
 
[[Category:Quiz]]
[[Category:Quiz]]
[[Category:Questions]]
[[Category:Questions]]
[[Category:Contributed code]]
[[Category:Contributed code]]
[[fr:question/type/regexp]]
[[es:Tipo de pregunta respuesta corta de expresión regular]]

Latest revision as of 15:17, 10 August 2021


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 analyzing the student's answers with the aim of providing more relevant immediate feedback.

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 an ma 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: ^(?!.*\blue\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

settings01.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 (including the very first letter or word).

In Adaptive mode the button displayed will say "Buy next letter" or "Buy next word" 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"

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.

Case sensitivity

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.

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

Previewing questions in popup window (teacher only)

When the teacher previews a question in the popup preview question window they can 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.

alternate answers.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 color 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 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 strike-through.

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 strike-through".

regexp colored feedback 21.jpg

Feedback given by the Help button

Each time a student clicks the Buy/Get next letter/word button to buy/get a letter/word, that letter or word is added to his 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, 'History of responses' section, the response history shows Submit (with a request for help) with the response states before and after the letter/word was added.

21-addedletterhistory.jpg


21-addedwordhistory.jpg


If the student clicked the Buy/Get next word button while his current submitted answer contained the beginning of a (correct) word, the full correct word is displayed in the Answer field, and the feedback message says "Completed word" rather than "Added word".

21-completedwordhistory.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

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.

https://raw.github.com/rezeau/moodle/multianswer_regexp-26/question/type/multianswer/questiontype.php

https://raw.github.com/rezeau/moodle/multianswer_regexp-26/question/type/multianswer/renderer.php

Note.- If you are interested in comparing the 2 "standard" Moodle 2.6 multianswer files with the "RegExp hacked" files, go to

https://github.com/rezeau/moodle/compare/MOODLE_26_STABLE...multianswer_regexp-26

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.

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

Other Moodle question types based on regular expressions

The OUP's PMatch

Oleg Sychev's Preg question type