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

Preg question type: Difference between revisions

From MoodleDocs
(New page: Preg question type is a question type using regular expression pattern matching to find if studen response is correct. It is use Perl-compatible regular expressions dialect. For detailed d...)
 
Line 7: Line 7:


===Matching===
===Matching===
You should enter regular expressions as '''answers''' to the question without modifiers or enclosing characters (modifiers would be added for you by question - '''u''' added always and '''i''' in case-insensitive mode). You should also enter one  correct response (that matches at least one 100% grade regular expression) to be shown to the student as '''correct answer'''.
You could construct you expression from these components:
# ''simple characters'' match with themselves
# ''escaped special characters'' if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: '''a\*''' matches with a* (while '''a*''' matches with a zero or more times), backslash is a special character too and should be escaped '''\\''' matches with \
# ''character classes'' you could specify a number of possible characters in one place in square brackets:
#* '''[ab,!]''' matches with a or b or , or !
#* ''ranges'': '''[a-szC-F0-9]''' you could specify ranges for letters and digits in character classes, mixing them with single characters
#* ''negative character classes'' starts with ^ '''[^ab]''' means any characters except a and b
#* ''escaping inside character classes'': '''[\-\]\\]''' match with - or ] or \
Most common regular expression operators used (could anyone help expand descriptions and examples please?):
# ''alternative'' let you define a set of alternatives:
#* '''a|b''' mean a or b
#* '''ab|cd|de''' mean ab or cd or de
#* '''ab|cd|''' mean ab or cd or emptiness (useful as a part more complex expressions
#* '''(aa|bb)c''' mean aac or bbc - use brackets to outline alternative set
#* '''(aa|bb|)c''' mean aac or bbc or c - typical use of emptiness
# ''quantifiers'' let you define repetition of a character (or regular expression):
#* '''x*''' mean x zero or more times
#* '''x+''' mean x one or more times
#* '''x?''' mean x zero or one times
#* '''x{2,4}''' mean x from 2 to 4 times
#* '''x{2,}''' mean x two or more times
#* '''x{,2}''' mean x from 0 to 2 times
#* '''x{2}''' mean x exactly 2 times
#* '''(ab)*''' mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets
#* '''(a|b){2}''' mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it
# ''asserts'' - these are assertions about some part of the string that doesn't actually goes into matching text
#* ''positive lookahead assert'' '''a+(?=b)''' matches with any number of a ending with b without including b in the match
#* ''negative lookahead assert'' '''a+(?!b)''' matches with any number of a that is not followed by b
#* ''positive lookbehind assert'' '''(?<=b)a+''' matches with any number of a preceeded by b
#* ''negative lookbehind assert'' '''(?<!b)a+''' matches with any number of a that is not preceeded by b
The matching process work like any regular expression matching. I.e. if you don't set '''exact matching''' to yes, than the match could be found ''inside'' student's response (not necessary the ''whole'' response) - it is still considered full match and give a grade. You could avoid this in individual expressions by anchoring them (i.e. starting with ^ and ending with $). Then you could use some non-anchored regexes to catch common errors and give feedback while using anchored expression for grading.


===Hinting===
===Hinting===

Revision as of 19:50, 15 September 2010

Preg question type is a question type using regular expression pattern matching to find if studen response is correct. It is use Perl-compatible regular expressions dialect. For detailed description of regular expression syntax see http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm

Authors:

  1. idea, design, question type code - Oleg Sychev;
  2. parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.

We would gladly accept testers and contributors (see Development plans section) - there is still more to be done than we have time.

Matching

You should enter regular expressions as answers to the question without modifiers or enclosing characters (modifiers would be added for you by question - u added always and i in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regular expression) to be shown to the student as correct answer.

You could construct you expression from these components:

  1. simple characters match with themselves
  2. escaped special characters if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: a\* matches with a* (while a* matches with a zero or more times), backslash is a special character too and should be escaped \\ matches with \
  3. character classes you could specify a number of possible characters in one place in square brackets:
    • [ab,!] matches with a or b or , or !
    • ranges: [a-szC-F0-9] you could specify ranges for letters and digits in character classes, mixing them with single characters
    • negative character classes starts with ^ [^ab] means any characters except a and b
    • escaping inside character classes: [\-\]\\] match with - or ] or \

Most common regular expression operators used (could anyone help expand descriptions and examples please?):

  1. alternative let you define a set of alternatives:
    • a|b mean a or b
    • ab|cd|de mean ab or cd or de
    • ab|cd| mean ab or cd or emptiness (useful as a part more complex expressions
    • (aa|bb)c mean aac or bbc - use brackets to outline alternative set
    • (aa|bb|)c mean aac or bbc or c - typical use of emptiness
  2. quantifiers let you define repetition of a character (or regular expression):
    • x* mean x zero or more times
    • x+ mean x one or more times
    • x? mean x zero or one times
    • x{2,4} mean x from 2 to 4 times
    • x{2,} mean x two or more times
    • x{,2} mean x from 0 to 2 times
    • x{2} mean x exactly 2 times
    • (ab)* mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets
    • (a|b){2} mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it
  3. asserts - these are assertions about some part of the string that doesn't actually goes into matching text
    • positive lookahead assert a+(?=b) matches with any number of a ending with b without including b in the match
    • negative lookahead assert a+(?!b) matches with any number of a that is not followed by b
    • positive lookbehind assert (?<=b)a+ matches with any number of a preceeded by b
    • negative lookbehind assert (?<!b)a+ matches with any number of a that is not preceeded by b

The matching process work like any regular expression matching. I.e. if you don't set exact matching to yes, than the match could be found inside student's response (not necessary the whole response) - it is still considered full match and give a grade. You could avoid this in individual expressions by anchoring them (i.e. starting with ^ and ending with $). Then you could use some non-anchored regexes to catch common errors and give feedback while using anchored expression for grading.

Hinting

Matching engines

PHP preg extension

Deterministic finite state automata

Development plans

There is no definite shedule or order of development for those features - it depends on the available time and developers. Many features require complex code to achieve results. If you want to help us with specific feature, please contact question type maintainer (Oleg Sychev) using http://moodle.org messaging.

  • Update DFA matching engine to support all operations DFA algorithm could
  • Develop NFA and backtracking matching engines
  • Add possibility to insert match from response (or subpattern match) in the feedback
  • Add automatic generation of shortest possible correct answer in user-readable form
  • Add negative matching answers (AKA "missing words" from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code
  • Add generation of 'description' for regular expression to facilitate it's editing
  • Develop more help and examples for the people that don't know much about regular expressions.