<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.moodle.org/21/en/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Oasychev</id>
	<title>MoodleDocs - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/21/en/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Oasychev"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/Special:Contributions/Oasychev"/>
	<updated>2026-04-21T07:06:21Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=96144</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=96144"/>
		<updated>2013-01-06T21:48:58Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code, behaviours code, regex parsing and error reporting - Oleg Sychev;&lt;br /&gt;
# regex parsing, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
# NFA regular expression matching engine, backreferences, cross-testing, backup&amp;amp;restore - Valeriy Streltsov&lt;br /&gt;
We would gladly accept testers and contributors (see [[#Development plans|development plans]] section) - there is still more to be done than we have time. Thanks to Joseph Rezeau for been devoted tester of Preg question type releases and original authors of many ideas, that was implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
===Notations===&lt;br /&gt;
Starting from Preg 2.1, notation feature allows you to choose notation in which regular expressions for answers will be written. &#039;&#039;&#039;Regular expression&#039;&#039;&#039; is a default one.&lt;br /&gt;
&lt;br /&gt;
One exciting part of it is that you could use preg question type just as improved shortanswer, having access to the hinting facility without any need to understand regular expressions at all! Just choose &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you could just copy answers from you shortanswer questions. &#039;*&#039; wildcard is supported. Choosing NFA or DFA engine you could get access to the hinting. You could omit all that is said on regular expression topic there, but be sure to read [[#Hinting|hinting section]] below to understand various settings you could alter to configure you question hniting behaviour.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
#* when &#039;&#039;&#039;unsure&#039;&#039;&#039; whether to escape some character, it is safe to place \ before any character except letters, digits and underscore, so you don&#039;t need to worry whether particular symbol is special or no&lt;br /&gt;
#* do not escape letters unless you know what you are doing, since they get special meaning when escaped and lose it when not&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
# &#039;&#039;escape sequences&#039;&#039; for common character classes:&lt;br /&gt;
#* &#039;&#039;&#039;\w&#039;&#039;&#039; for any word character (letter, underscore or digit) and &#039;&#039;&#039;\W&#039;&#039;&#039; for non-word character&lt;br /&gt;
#* &#039;&#039;&#039;\s&#039;&#039;&#039; for any space character and &#039;&#039;&#039;\S&#039;&#039;&#039; for any non-space character&lt;br /&gt;
#* &#039;&#039;&#039;\d&#039;&#039;&#039; for any digit and &#039;&#039;&#039;\D&#039;&#039;&#039; for any non-digit&lt;br /&gt;
# &#039;&#039;simple assertions&#039;&#039; - these are not characters, but conditions to test, they don&#039;t &amp;quot;eat&amp;quot; characters while matching, unlike other operands:&lt;br /&gt;
#* &#039;&#039;&#039;^&#039;&#039;&#039; - match in the start of response string, fails otherwise&lt;br /&gt;
#* &#039;&#039;&#039;$&#039;&#039;&#039; - match in the end of response string, fails otherwise&lt;br /&gt;
#* &#039;&#039;&#039;\b&#039;&#039;&#039; - match on the word boundary, i.e. between word (\w) and non-word (\W) characters, and in the start/end of the response if it starts (ends) with word character&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more d&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process using simple assertions:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, which make it&#039;s use slightly tricky.&lt;br /&gt;
#* &#039;&#039;&#039;^ab|cd$&#039;&#039;&#039; would match &#039;&#039;ab&#039;&#039; from the start of string or &#039;&#039;cd&#039;&#039; at the end of it&lt;br /&gt;
#* &#039;&#039;&#039;^(ab|cd)$&#039;&#039;&#039; use brackets to match exactly with &#039;&#039;ab&#039;&#039; or &#039;&#039;cd&#039;&#039;&lt;br /&gt;
#* &#039;&#039;&#039;^ab$|^cd$&#039;&#039;&#039; is another way to get exact matching of regex with top-level alternatives&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you (with correct brackets to not interfere with you subpatterns). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you could set case-(in)sensitivity for parts of you regular expression using standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
#* &#039;&#039;&#039;(?i)&#039;&#039;&#039; would turn case-sensitivity off&lt;br /&gt;
#* &#039;&#039;&#039;(?-i)&#039;&#039;&#039; would turn case-sensitivity on&lt;br /&gt;
&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you could make some answer case-sensitive while other as not, or even do this for the parts of answers. For example you could set question as &amp;quot;use case&amp;quot; and have 50% answer regular expression starting with &#039;&#039;(?i)&#039;&#039; to lessen grade when case don&#039;t match, but answer otherwise correct.&lt;br /&gt;
&lt;br /&gt;
When done in the round brackets, these local modifiers work up to the closest &#039;&#039;&#039;)&#039;&#039;&#039; (closing round bracket). When done on the main level (not inside brackets) they work up to the end of expression. I.e. with case sensitivity on for the question:&lt;br /&gt;
* abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz would have bold part case-insensitive&lt;br /&gt;
* abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039; would have bold part case-insensitive&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
If no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances - the placeholder syntax may become complex and prone to errors.&lt;br /&gt;
&lt;br /&gt;
===Error reporting===&lt;br /&gt;
Native PHP preg extension functions only report if there error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine couldn&#039;t tell you much about what is error .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DFA&#039;&#039;&#039; engine use custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so it supports advanced error reporting. The are several class of potential errors reported:&lt;br /&gt;
* unclosed square brackets of character class;&lt;br /&gt;
* unclosed opening parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* unopened closing parenthesis;&lt;br /&gt;
* empty parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* quantifiers without operand, i.e. at the start of (sub)expression with nothing to repeat;&lt;br /&gt;
* three or more top-level alternatives in the conditional subpattern.&lt;br /&gt;
PCRE (and preg functions) treat most of them as &#039;&#039;&#039;non-errors&#039;&#039;&#039;, making many characters meaning context-dependent. For example quantifier {2,4} placed at the start of regular expression lose it&#039;s meaning as quantifier and is treated as five-characters sequence instead (that matches with {2,4}). However such syntax is very prone to errors and make writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I&#039;m vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE/preg. If you are stand for or against this decision please write you positions and reasons on the page comments please. It may be best to have two modes, but this literally means two parsers and this is out of current scope of development. There are more pressing issues ahead.&lt;br /&gt;
&lt;br /&gt;
===Looking for missing things===&lt;br /&gt;
Joseph Rezeau REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that would work when something is absent in the answer (and give appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect could be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. Regular expression to look for the missing word &#039;&#039;&#039;necessary&#039;&#039;&#039; would be&lt;br /&gt;
  ^(?!.*\bnecessary\b.*)&lt;br /&gt;
where&lt;br /&gt;
* &#039;&#039;&#039;(?!.*\bnecessary\b.*)&#039;&#039;&#039; is a &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039;, that allows matching only if there are no word &#039;&#039;&#039;necessary&#039;&#039;&#039; ahead of some point in the string;&lt;br /&gt;
* &#039;&#039;&#039;^&#039;&#039;&#039; is an assertion too, that anchoring the match to the start of response (otherwise there would be places in response after the word &amp;quot;necessary&amp;quot;, where matching is possible even if the word is present).&lt;br /&gt;
&lt;br /&gt;
In case the description is difficult to you, just surround regexp to be missing with &#039;&#039;&#039;^(?!&#039;&#039;&#039; and &#039;&#039;&#039;)&#039;&#039;&#039;. Don&#039;t try &#039;--&#039; syntax that is specific to Jospeh Rezeau REGEX question type!&lt;br /&gt;
&lt;br /&gt;
Sadly, no engine except PHP_preg_matcher is supportting complex assertions (i.e. &#039;&#039;&#039;(?!&#039;&#039;&#039; part). We are working on it, but it stil some time away, requiring further complex work.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (locale-aware, but not Unicode for performance reasons, as in standard regular expression functions)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
====Non-deterministing finite state automata(NFA)====&lt;br /&gt;
NFA engine, introduced in 2.1 release, is a custom matcher that basically could do anything DFA matcher could, with addition of subpattern capturing and backreferences. &lt;br /&gt;
&lt;br /&gt;
Now you don&#039;t have to choose between hiting and using captured subpatterns in you questions: NFA could do them both!&lt;br /&gt;
&lt;br /&gt;
===The ways to give back===&lt;br /&gt;
I am a high school teacher, researcher and programmer who must do much on his main paid job and have not much free time to spend on developing this question type. If you could help me in some ways, I may be able to spend more time and effort doing this thought. Some examples:&lt;br /&gt;
* publishing a thesis or paper describing you use of Preg question I could give reference for would improve rating of preg project there and my rating as researcher/developers, so please publish and let me know the reference if you feel grateful for the software;&lt;br /&gt;
* if you would take some more work and organise publishing a paper (or at least thesis) with me as co-author, that would &#039;&#039;&#039;help even more&#039;&#039;&#039; - please inform me immediately if you consider this;&lt;br /&gt;
* if publishing is hard, you could just write me what you organisation is and how you use preg - that&#039;ll help a little and I would be able to better determine what should be done next;&lt;br /&gt;
* join the testing efforts, either by performing manual test or by writing unit tests (it&#039;s easy to do even if you aren&#039;t great programmer, you just need to know regular expressions - contact me and I&#039;ll tell you how.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Improved simple assertions support&lt;br /&gt;
* Support for complex assertions&lt;br /&gt;
* Hinting not one character, but completion of the whole world&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add a set of authoring tools to make writing regular expressions easier&lt;br /&gt;
* Develop backtracking matching engine&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=question/type/preg&amp;diff=96143</id>
		<title>question/type/preg</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=question/type/preg&amp;diff=96143"/>
		<updated>2013-01-06T21:41:35Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Redirected page to Preg question type&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Preg question type]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=94747</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=94747"/>
		<updated>2011-12-23T14:34:06Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Anchoring */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code, behaviours code, regex parsing and error reporting - Oleg Sychev;&lt;br /&gt;
# regex parsing, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
# NFA regular expression matching engine, backreferences, cross-testing, backup&amp;amp;restore - Valeriy Streltsov&lt;br /&gt;
We would gladly accept testers and contributors (see [[#Development plans|development plans]] section) - there is still more to be done than we have time. Thanks to Joseph Rezeau for been devoted tester of Preg question type releases and original authors of many ideas, that was implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
===Notations===&lt;br /&gt;
Starting from Preg 2.1, notation feature allows you to choose notation in which regular expressions for answers will be written. &#039;&#039;&#039;Regular expression&#039;&#039;&#039; is a default one.&lt;br /&gt;
&lt;br /&gt;
One exciting part of it is that you could use preg question type just as improved shortanswer, having access to the hinting facility without any need to understand regular expressions at all! Just choose &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you could just copy answers from you shortanswer questions. &#039;*&#039; wildcard is supported. Choosing NFA or DFA engine you could get access to the hinting. You could omit all that is said on regular expression topic there, but be sure to read [[#Hinting|hinting section]] below to understand various settings you could alter to configure you question hniting behaviour.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
#* when &#039;&#039;&#039;unsure&#039;&#039;&#039; whether to escape some character, it is safe to place \ before any character except letters, digits and underscore, so you don&#039;t need to worry whether particular symbol is special or no&lt;br /&gt;
#* do not escape letters unless you know what you are doing, since they get special meaning when escaped and lose it when not&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
# &#039;&#039;escape sequences&#039;&#039; for common character classes:&lt;br /&gt;
#* &#039;&#039;&#039;\w&#039;&#039;&#039; for any word character (letter, underscore or digit) and &#039;&#039;&#039;\W&#039;&#039;&#039; for non-word character&lt;br /&gt;
#* &#039;&#039;&#039;\s&#039;&#039;&#039; for any space character and &#039;&#039;&#039;\S&#039;&#039;&#039; for any non-space character&lt;br /&gt;
#* &#039;&#039;&#039;\d&#039;&#039;&#039; for any digit and &#039;&#039;&#039;\D&#039;&#039;&#039; for any non-digit&lt;br /&gt;
# &#039;&#039;simple assertions&#039;&#039; - these are not characters, but conditions to test, they don&#039;t &amp;quot;eat&amp;quot; characters while matching, unlike other operands:&lt;br /&gt;
#* &#039;&#039;&#039;^&#039;&#039;&#039; - match in the start of response string, fails otherwise&lt;br /&gt;
#* &#039;&#039;&#039;$&#039;&#039;&#039; - match in the end of response string, fails otherwise&lt;br /&gt;
#* &#039;&#039;&#039;\b&#039;&#039;&#039; - match on the word boundary, i.e. between word (\w) and non-word (\W) characters, and in the start/end of the response if it starts (ends) with word character&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more d&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process using simple assertions:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, which make it&#039;s use slightly tricky.&lt;br /&gt;
#* &#039;&#039;&#039;^ab|cd$&#039;&#039;&#039; would match &#039;&#039;ab&#039;&#039; from the start of string or &#039;&#039;cd&#039;&#039; at the end of it&lt;br /&gt;
#* &#039;&#039;&#039;^(ab|cd)$&#039;&#039;&#039; use brackets to match exactly with &#039;&#039;ab&#039;&#039; or &#039;&#039;cd&#039;&#039;&lt;br /&gt;
#* &#039;&#039;&#039;^ab$|^cd$&#039;&#039;&#039; is another way to get exact matching of regex with top-level alternatives&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you (with correct brackets to not interfere with you subpatterns). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you could set case-(in)sensitivity for parts of you regular expression using standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
#* &#039;&#039;&#039;(?i)&#039;&#039;&#039; would turn case-sensitivity off&lt;br /&gt;
#* &#039;&#039;&#039;(?-i)&#039;&#039;&#039; would turn case-sensitivity on&lt;br /&gt;
&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you could make some answer case-sensitive while other as not, or even do this for the parts of answers. For example you could set question as &amp;quot;use case&amp;quot; and have 50% answer regular expression starting with &#039;&#039;(?i)&#039;&#039; to lessen grade when case don&#039;t match, but answer otherwise correct.&lt;br /&gt;
&lt;br /&gt;
When done in the round brackets, these local modifiers work up to the closest &#039;&#039;&#039;)&#039;&#039;&#039; (closing round bracket). When done on the main level (not inside brackets) they work up to the end of expression. I.e. with case sensitivity on for the question:&lt;br /&gt;
* abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz would have bold part case-insensitive&lt;br /&gt;
* abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039; would have bold part case-insensitive&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
If no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances - the placeholder syntax may become complex and prone to errors.&lt;br /&gt;
&lt;br /&gt;
===Error reporting===&lt;br /&gt;
Native PHP preg extension functions only report if there error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine couldn&#039;t tell you much about what is error .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DFA&#039;&#039;&#039; engine use custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so it supports advanced error reporting. The are several class of potential errors reported:&lt;br /&gt;
* unclosed square brackets of character class;&lt;br /&gt;
* unclosed opening parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* unopened closing parenthesis;&lt;br /&gt;
* empty parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* quantifiers without operand, i.e. at the start of (sub)expression with nothing to repeat;&lt;br /&gt;
* three or more top-level alternatives in the conditional subpattern.&lt;br /&gt;
PCRE (and preg functions) treat most of them as &#039;&#039;&#039;non-errors&#039;&#039;&#039;, making many characters meaning context-dependent. For example quantifier {2,4} placed at the start of regular expression lose it&#039;s meaning as quantifier and is treated as five-characters sequence instead (that matches with {2,4}). However such syntax is very prone to errors and make writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I&#039;m vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE/preg. If you are stand for or against this decision please write you positions and reasons on the page comments please. It may be best to have two modes, but this literally means two parsers and this is out of current scope of development. There are more pressing issues ahead.&lt;br /&gt;
&lt;br /&gt;
===Looking for missing things===&lt;br /&gt;
Joseph Rezeau REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that would work when something is absent in the answer (and give appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect could be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. Regular expression to look for the missing word &#039;&#039;&#039;necessary&#039;&#039;&#039; would be&lt;br /&gt;
  ^(?!.*\bnecessary\b.*)&lt;br /&gt;
where&lt;br /&gt;
* &#039;&#039;&#039;(?!.*\bnecessary\b.*)&#039;&#039;&#039; is a &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039;, that allows matching only if there are no word &#039;&#039;&#039;necessary&#039;&#039;&#039; ahead of some point in the string;&lt;br /&gt;
* &#039;&#039;&#039;^&#039;&#039;&#039; is an assertion too, that anchoring the match to the start of response (otherwise there would be places in response after the word &amp;quot;necessary&amp;quot;, where matching is possible even if the word is present).&lt;br /&gt;
&lt;br /&gt;
In case the description is difficult to you, just surround regexp to be missing with &#039;&#039;&#039;^(?!&#039;&#039;&#039; and &#039;&#039;&#039;)&#039;&#039;&#039;. Don&#039;t try &#039;--&#039; syntax that is specific to Jospeh Rezeau REGEX question type!&lt;br /&gt;
&lt;br /&gt;
Sadly, no engine except PHP_preg_matcher is supportting complex assertions (i.e. &#039;&#039;&#039;(?!&#039;&#039;&#039; part). We are working on it, but it stil some time away, requiring further complex work.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (locale-aware, but not Unicode for performance reasons, as in standard regular expression functions)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
====Non-deterministing finite state automata(NFA)====&lt;br /&gt;
NFA engine, introduced in 2.1 release, is a custom matcher that basically could do anything DFA matcher could, with addition of subpattern capturing and backreferences. &lt;br /&gt;
&lt;br /&gt;
Now you don&#039;t have to choose between hiting and using captured subpatterns in you questions: NFA could do them both!&lt;br /&gt;
&lt;br /&gt;
===The ways to give back===&lt;br /&gt;
I am a high school teacher, researcher and programmer who must do much on his main paid job and have not much free time to spend on developing this question type. If you could help me in some ways, I may be able to spend more time and effort doing this thought. Some examples:&lt;br /&gt;
* publishing a thesis or paper describing you use of Preg question I could give reference for would improve rating of preg project there and my rating as researcher/developers, so please publish and let me know the reference if you feel grateful for the software;&lt;br /&gt;
* if you would take some more work and organise publishing a paper (or at least thesis) with me as co-author, that would &#039;&#039;&#039;help even more&#039;&#039;&#039; - please inform me immediately if you consider this;&lt;br /&gt;
* if publishing is hard, you could just write me what you organisation is and how you use preg - that&#039;ll help a little and I would be able to better determine what should be done next;&lt;br /&gt;
* join the testing efforts, either by performing manual test or by writing unit tests (it&#039;s easy to do even if you aren&#039;t great programmer, you just need to know regular expressions - contact me and I&#039;ll tell you how.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Improved simple assertions support&lt;br /&gt;
* Support for complex assertions&lt;br /&gt;
* Hinting not one character, but completion of the whole world&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add a set of authoring tools to make writing regular expressions easier&lt;br /&gt;
* Develop backtracking matching engine&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=94746</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=94746"/>
		<updated>2011-12-23T14:10:14Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code, behaviours code, regex parsing and error reporting - Oleg Sychev;&lt;br /&gt;
# regex parsing, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
# NFA regular expression matching engine, backreferences, cross-testing, backup&amp;amp;restore - Valeriy Streltsov&lt;br /&gt;
We would gladly accept testers and contributors (see [[#Development plans|development plans]] section) - there is still more to be done than we have time. Thanks to Joseph Rezeau for been devoted tester of Preg question type releases and original authors of many ideas, that was implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
===Notations===&lt;br /&gt;
Starting from Preg 2.1, notation feature allows you to choose notation in which regular expressions for answers will be written. &#039;&#039;&#039;Regular expression&#039;&#039;&#039; is a default one.&lt;br /&gt;
&lt;br /&gt;
One exciting part of it is that you could use preg question type just as improved shortanswer, having access to the hinting facility without any need to understand regular expressions at all! Just choose &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you could just copy answers from you shortanswer questions. &#039;*&#039; wildcard is supported. Choosing NFA or DFA engine you could get access to the hinting. You could omit all that is said on regular expression topic there, but be sure to read [[#Hinting|hinting section]] below to understand various settings you could alter to configure you question hniting behaviour.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
#* when &#039;&#039;&#039;unsure&#039;&#039;&#039; whether to escape some character, it is safe to place \ before any character except letters, digits and underscore, so you don&#039;t need to worry whether particular symbol is special or no&lt;br /&gt;
#* do not escape letters unless you know what you are doing, since they get special meaning when escaped and lose it when not&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
# &#039;&#039;escape sequences&#039;&#039; for common character classes:&lt;br /&gt;
#* &#039;&#039;&#039;\w&#039;&#039;&#039; for any word character (letter, underscore or digit) and &#039;&#039;&#039;\W&#039;&#039;&#039; for non-word character&lt;br /&gt;
#* &#039;&#039;&#039;\s&#039;&#039;&#039; for any space character and &#039;&#039;&#039;\S&#039;&#039;&#039; for any non-space character&lt;br /&gt;
#* &#039;&#039;&#039;\d&#039;&#039;&#039; for any digit and &#039;&#039;&#039;\D&#039;&#039;&#039; for any non-digit&lt;br /&gt;
# &#039;&#039;simple assertions&#039;&#039; - these are not characters, but conditions to test, they don&#039;t &amp;quot;eat&amp;quot; characters while matching, unlike other operands:&lt;br /&gt;
#* &#039;&#039;&#039;^&#039;&#039;&#039; - match in the start of response string, fails otherwise&lt;br /&gt;
#* &#039;&#039;&#039;$&#039;&#039;&#039; - match in the end of response string, fails otherwise&lt;br /&gt;
#* &#039;&#039;&#039;\b&#039;&#039;&#039; - match on the word boundary, i.e. between word (\w) and non-word (\W) characters, and in the start/end of the response if it starts (ends) with word character&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more d&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
If no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances - the placeholder syntax may become complex and prone to errors.&lt;br /&gt;
&lt;br /&gt;
===Error reporting===&lt;br /&gt;
Native PHP preg extension functions only report if there error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine couldn&#039;t tell you much about what is error .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DFA&#039;&#039;&#039; engine use custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so it supports advanced error reporting. The are several class of potential errors reported:&lt;br /&gt;
* unclosed square brackets of character class;&lt;br /&gt;
* unclosed opening parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* unopened closing parenthesis;&lt;br /&gt;
* empty parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* quantifiers without operand, i.e. at the start of (sub)expression with nothing to repeat;&lt;br /&gt;
* three or more top-level alternatives in the conditional subpattern.&lt;br /&gt;
PCRE (and preg functions) treat most of them as &#039;&#039;&#039;non-errors&#039;&#039;&#039;, making many characters meaning context-dependent. For example quantifier {2,4} placed at the start of regular expression lose it&#039;s meaning as quantifier and is treated as five-characters sequence instead (that matches with {2,4}). However such syntax is very prone to errors and make writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I&#039;m vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE/preg. If you are stand for or against this decision please write you positions and reasons on the page comments please. It may be best to have two modes, but this literally means two parsers and this is out of current scope of development. There are more pressing issues ahead.&lt;br /&gt;
&lt;br /&gt;
===Looking for missing things===&lt;br /&gt;
Joseph Rezeau REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that would work when something is absent in the answer (and give appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect could be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. Regular expression to look for the missing word &#039;&#039;&#039;necessary&#039;&#039;&#039; would be&lt;br /&gt;
  ^(?!.*\bnecessary\b.*)&lt;br /&gt;
where&lt;br /&gt;
* &#039;&#039;&#039;(?!.*\bnecessary\b.*)&#039;&#039;&#039; is a &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039;, that allows matching only if there are no word &#039;&#039;&#039;necessary&#039;&#039;&#039; ahead of some point in the string;&lt;br /&gt;
* &#039;&#039;&#039;^&#039;&#039;&#039; is an assertion too, that anchoring the match to the start of response (otherwise there would be places in response after the word &amp;quot;necessary&amp;quot;, where matching is possible even if the word is present).&lt;br /&gt;
&lt;br /&gt;
In case the description is difficult to you, just surround regexp to be missing with &#039;&#039;&#039;^(?!&#039;&#039;&#039; and &#039;&#039;&#039;)&#039;&#039;&#039;. Don&#039;t try &#039;--&#039; syntax that is specific to Jospeh Rezeau REGEX question type!&lt;br /&gt;
&lt;br /&gt;
Sadly, no engine except PHP_preg_matcher is supportting complex assertions (i.e. &#039;&#039;&#039;(?!&#039;&#039;&#039; part). We are working on it, but it stil some time away, requiring further complex work.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (locale-aware, but not Unicode for performance reasons, as in standard regular expression functions)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
====Non-deterministing finite state automata(NFA)====&lt;br /&gt;
NFA engine, introduced in 2.1 release, is a custom matcher that basically could do anything DFA matcher could, with addition of subpattern capturing and backreferences. &lt;br /&gt;
&lt;br /&gt;
Now you don&#039;t have to choose between hiting and using captured subpatterns in you questions: NFA could do them both!&lt;br /&gt;
&lt;br /&gt;
===The ways to give back===&lt;br /&gt;
I am a high school teacher, researcher and programmer who must do much on his main paid job and have not much free time to spend on developing this question type. If you could help me in some ways, I may be able to spend more time and effort doing this thought. Some examples:&lt;br /&gt;
* publishing a thesis or paper describing you use of Preg question I could give reference for would improve rating of preg project there and my rating as researcher/developers, so please publish and let me know the reference if you feel grateful for the software;&lt;br /&gt;
* if you would take some more work and organise publishing a paper (or at least thesis) with me as co-author, that would &#039;&#039;&#039;help even more&#039;&#039;&#039; - please inform me immediately if you consider this;&lt;br /&gt;
* if publishing is hard, you could just write me what you organisation is and how you use preg - that&#039;ll help a little and I would be able to better determine what should be done next;&lt;br /&gt;
* join the testing efforts, either by performing manual test or by writing unit tests (it&#039;s easy to do even if you aren&#039;t great programmer, you just need to know regular expressions - contact me and I&#039;ll tell you how.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Improved simple assertions support&lt;br /&gt;
* Support for complex assertions&lt;br /&gt;
* Hinting not one character, but completion of the whole world&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add a set of authoring tools to make writing regular expressions easier&lt;br /&gt;
* Develop backtracking matching engine&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=94632</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=94632"/>
		<updated>2011-12-13T10:32:37Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code, behaviours code, regex parsing and error reporting - Oleg Sychev;&lt;br /&gt;
# regex parsing, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
# NFA regular expression matching engine - Valeriy Streltsov&lt;br /&gt;
We would gladly accept testers and contributors (see [[#Development plans|development plans]] section) - there is still more to be done than we have time. Thanks to Joseph Rezeau for been devoted tester of Preg question type releases and original authors of many ideas, that was implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
===Notations===&lt;br /&gt;
Starting from Preg 2.1, notation feature allows you to choose notation in which regular expressions for answers will be written. &#039;&#039;&#039;Regular expression&#039;&#039;&#039; is a default one.&lt;br /&gt;
&lt;br /&gt;
One exciting part of it is that you could use preg question type just as improved shortanswer, having access to the hinting facility without any need to understand regular expressions at all! Just choose &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you could just copy answers from you shortanswer questions. &#039;*&#039; wildcard is supported. Choosing NFA or DFA engine you could get access to the hinting. You could omit all that is said on regular expression topic there, but be sure to read [[#Hinting|hinting section]] below to understand various settings you could alter to configure you question hniting behaviour.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more d&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
If no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances - the placeholder syntax may become complex and prone to errors.&lt;br /&gt;
&lt;br /&gt;
===Error reporting===&lt;br /&gt;
Native PHP preg extension functions only report if there error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine couldn&#039;t tell you much about what is error .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DFA&#039;&#039;&#039; engine use custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so it supports advanced error reporting. The are several class of potential errors reported:&lt;br /&gt;
* unclosed square brackets of character class;&lt;br /&gt;
* unclosed opening parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* unopened closing parenthesis;&lt;br /&gt;
* empty parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* quantifiers without operand, i.e. at the start of (sub)expression with nothing to repeat;&lt;br /&gt;
* three or more top-level alternatives in the conditional subpattern.&lt;br /&gt;
PCRE (and preg functions) treat most of them as &#039;&#039;&#039;non-errors&#039;&#039;&#039;, making many characters meaning context-dependent. For example quantifier {2,4} placed at the start of regular expression lose it&#039;s meaning as quantifier and is treated as five-characters sequence instead (that matches with {2,4}). However such syntax is very prone to errors and make writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I&#039;m vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE/preg. If you are stand for or against this decision please write you positions and reasons on the page comments please. It may be best to have two modes, but this literally means two parsers and this is out of current scope of development. There are more pressing issues ahead.&lt;br /&gt;
&lt;br /&gt;
===Looking for missing things===&lt;br /&gt;
Joseph Rezeau REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that would work when something is absent in the answer (and give appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect could be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. Regular expression to look for the missing word &#039;&#039;&#039;necessary&#039;&#039;&#039; would be&lt;br /&gt;
  ^(?!.*\bnecessary\b.*)&lt;br /&gt;
where&lt;br /&gt;
* &#039;&#039;&#039;(?!.*\bnecessary\b.*)&#039;&#039;&#039; is a &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039;, that allows matching only if there are no word &#039;&#039;&#039;necessary&#039;&#039;&#039; ahead of some point in the string;&lt;br /&gt;
* &#039;&#039;&#039;^&#039;&#039;&#039; is an assertion too, that anchoring the match to the start of response (otherwise there would be places in response after the word &amp;quot;necessary&amp;quot;, where matching is possible even if the word is present).&lt;br /&gt;
&lt;br /&gt;
In case the description is difficult to you, just surround regexp to be missing with &#039;&#039;&#039;^(?!&#039;&#039;&#039; and &#039;&#039;&#039;)&#039;&#039;&#039;. Don&#039;t try &#039;--&#039; syntax that is specific to Jospeh Rezeau REGEX question type!&lt;br /&gt;
&lt;br /&gt;
Sadly, no engine except PHP_preg_matcher is supportting complex assertions (i.e. &#039;&#039;&#039;(?!&#039;&#039;&#039; part). We are working on it, but it stil some time away, requiring further complex work.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (locale-aware, but not Unicode for performance reasons, as in standard regular expression functions)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
====Non-deterministing finite state automata(NFA)====&lt;br /&gt;
NFA engine, introduced in 2.1 release, is a custom matcher that basically could do anything DFA matcher could, with addition of subpattern capturing and backreferences. &lt;br /&gt;
&lt;br /&gt;
Now you don&#039;t have to choose between hiting and using captured subpatterns in you questions: NFA could do them both!&lt;br /&gt;
&lt;br /&gt;
===The ways to give back===&lt;br /&gt;
I am a high school teacher, researcher and programmer who must do much on his main paid job and have not much free time to spend on developing this question type. If you could help me in some ways, I may be able to spend more time and effort doing this thought. Some examples:&lt;br /&gt;
* publishing a thesis or paper describing you use of Preg question I could give reference for would improve rating of preg project there and my rating as researcher/developers, so please publish and let me know the reference if you feel grateful for the software;&lt;br /&gt;
* if you would take some more work and organise publishing a paper (or at least thesis) with me as co-author, that would &#039;&#039;&#039;help even more&#039;&#039;&#039; - please inform me immediately if you consider this;&lt;br /&gt;
* if publishing is hard, you could just write me what you organisation is and how you use preg - that&#039;ll help a little and I would be able to better determine what should be done next;&lt;br /&gt;
* join the testing efforts, either by performing manual test or by writing unit tests (it&#039;s easy to do even if you aren&#039;t great programmer, you just need to know regular expressions - contact me and I&#039;ll tell you how.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Improved simple assertions support&lt;br /&gt;
* Support for complex assertions&lt;br /&gt;
* Hinting not one character, but completion of the whole world&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add a set of authoring tools to make writing regular expressions easier&lt;br /&gt;
* Develop backtracking matching engine&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=94631</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=94631"/>
		<updated>2011-12-13T10:18:05Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code, behaviours code, regex parsing and error reporting - Oleg Sychev;&lt;br /&gt;
# regex parsing, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
# NFA regular expression matching engine - Valeriy Streltsov&lt;br /&gt;
We would gladly accept testers and contributors (see [[#Development plans|development plans]] section) - there is still more to be done than we have time. Thanks for Joseph Rezeau for been devoted tester of Preg question type releases and original authors of many ideas, that was implemented in preg question type.&lt;br /&gt;
&lt;br /&gt;
===Notations===&lt;br /&gt;
Starting from Preg 2.1, notation feature allows you to choose notation in which regular expressions for answers will be written. &#039;&#039;&#039;Regular expression&#039;&#039;&#039; is a default one.&lt;br /&gt;
&lt;br /&gt;
One exciting part of it is that you could use preg question type just as improved shortanswer, having access to the hinting facility without any need to understand regular expressions at all! Just choose &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you could just copy answers from you shortanswer questions. &#039;*&#039; wildcard is supported. Choosing NFA or DFA engine you could get access to the hinting. You could omit all that is said on regular expression topic there, but be sure to read [[#Hinting|hinting section]] below to understand various settings you could alter to configure you question hniting behaviour.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more d&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
If no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances - the placeholder syntax may become complex and prone to errors.&lt;br /&gt;
&lt;br /&gt;
===Error reporting===&lt;br /&gt;
Native PHP preg extension functions only report if there error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine couldn&#039;t tell you much about what is error .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DFA&#039;&#039;&#039; engine use custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so it supports advanced error reporting. The are several class of potential errors reported:&lt;br /&gt;
* unclosed square brackets of character class;&lt;br /&gt;
* unclosed opening parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* unopened closing parenthesis;&lt;br /&gt;
* empty parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* quantifiers without operand, i.e. at the start of (sub)expression with nothing to repeat;&lt;br /&gt;
* three or more top-level alternatives in the conditional subpattern.&lt;br /&gt;
PCRE (and preg functions) treat most of them as &#039;&#039;&#039;non-errors&#039;&#039;&#039;, making many characters meaning context-dependent. For example quantifier {2,4} placed at the start of regular expression lose it&#039;s meaning as quantifier and is treated as five-characters sequence instead (that matches with {2,4}). However such syntax is very prone to errors and make writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I&#039;m vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE/preg. If you are stand for or against this decision please write you positions and reasons on the page comments please. It may be best to have two modes, but this literally means two parsers and this is out of current scope of development. There are more pressing issues ahead.&lt;br /&gt;
&lt;br /&gt;
===Looking for missing things===&lt;br /&gt;
Joseph Rezeau REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that would work when something is absent in the answer (and give appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect could be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. Regular expression to look for the missing word &#039;&#039;&#039;necessary&#039;&#039;&#039; would be&lt;br /&gt;
  ^(?!.*\bnecessary\b.*)&lt;br /&gt;
where&lt;br /&gt;
* &#039;&#039;&#039;(?!.*\bnecessary\b.*)&#039;&#039;&#039; is a &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039;, that allows matching only if there are no word &#039;&#039;&#039;necessary&#039;&#039;&#039; ahead of some point in the string;&lt;br /&gt;
* &#039;&#039;&#039;^&#039;&#039;&#039; is an assertion too, that anchoring the match to the start of response (otherwise there would be places in response after the word &amp;quot;necessary&amp;quot;, where matching is possible even if the word is present).&lt;br /&gt;
&lt;br /&gt;
In case the description is difficult to you, just surround regexp to be missing with &#039;&#039;&#039;^(?!&#039;&#039;&#039; and &#039;&#039;&#039;)&#039;&#039;&#039;. Don&#039;t try &#039;--&#039; syntax that is specific to Jospeh Rezeau REGEX question type!&lt;br /&gt;
&lt;br /&gt;
Sadly, no engine except PHP_preg_matcher is supportting complex assertions (i.e. &#039;&#039;&#039;(?!&#039;&#039;&#039; part). We are working on it, but it stil some time away, requiring further complex work.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (locale-aware, but not Unicode for performance reasons, as in standard regular expression functions)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
====Non-deterministing finite state automata(NFA)====&lt;br /&gt;
NFA engine, introduced in 2.1 release, is a custom matcher that basically could do anything DFA matcher could, with addition of subpattern capturing and backreferences. &lt;br /&gt;
&lt;br /&gt;
Now you don&#039;t have to choose between hiting and using captured subpatterns in you questions: NFA could do them both!&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Improved simple assertions support&lt;br /&gt;
* Support for complex assertions&lt;br /&gt;
* Hinting not one character, but completion of the whole world&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add a set of authoring tools to make writing regular expressions easier&lt;br /&gt;
* Develop backtracking matching engine&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=File:Bulk_edit4.JPG&amp;diff=83233</id>
		<title>File:Bulk edit4.JPG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=File:Bulk_edit4.JPG&amp;diff=83233"/>
		<updated>2011-05-04T21:07:20Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: uploaded a new version of &amp;amp;quot;File:Bulk edit4.JPG&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bulk module editing step 4: edit resulting form&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:Linked_activities&amp;diff=83232</id>
		<title>Development:Linked activities</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:Linked_activities&amp;diff=83232"/>
		<updated>2011-05-04T20:31:54Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Interface */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In large courses we often find a sets of similar activities (quizzes, assignments and so on), which share many settings (i.e. module settings) - but not all, of course. Maintaining of such sets is quite a pain: imagine youself adding a new IP range on 6-10 quizzes. So we need an usable and robust way to handle such sets - linked activities.&lt;br /&gt;
&lt;br /&gt;
This page is to discuss and find a better way to do so.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s call the &#039;&#039;&#039;setting&#039;&#039;&#039; some parameter of course module instance. Setting isn&#039;t necessary one control on the form (it may be a group of related controls, check boxes for example). Setting may be a field in db, or not - needs thinking.&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;set&#039;&#039;&#039; is a group of course module instances, that share common settings.&lt;br /&gt;
&lt;br /&gt;
== Issues ==&lt;br /&gt;
* linked activities may need to response on editing one of them (and deleting), so they need an events on these occasion. Anyone interested please vote for MDL-16203.&lt;br /&gt;
* modules now handle settings (from form to db and from db to form) in a bulk, there is no way to say the module &amp;quot;please save in db (or show on the form) this setting and not leave others be&amp;quot;. It&#039;s possible to do this without module help, but if module can map individual settings to db/form(controls) it would help.&lt;br /&gt;
&lt;br /&gt;
== Interface ==&lt;br /&gt;
&lt;br /&gt;
=== Level 0: Save as another ===&lt;br /&gt;
Done in Moodle 2.1 as copy acitivity.&lt;br /&gt;
&lt;br /&gt;
=== Level 1: Bulk activities editing ===&lt;br /&gt;
This doesn&#039;t require DB change, and hopefully can be done in 1.9 too.&lt;br /&gt;
Block bulk_module_edit is intented to be placed in a course page.&lt;br /&gt;
&lt;br /&gt;
==== Step 1: Select a module type to edit ====&lt;br /&gt;
[[File:Bulk_edit1.JPG]]&lt;br /&gt;
&lt;br /&gt;
==== Step 2: Select instances to edit ====&lt;br /&gt;
[[File:Bulk_edit2.JPG]]&lt;br /&gt;
&lt;br /&gt;
==== Step 3: Select fields to edit ====&lt;br /&gt;
[[File:Bulk_edit3.JPG]]&lt;br /&gt;
Sorry, quiz has quite a long list of form fields which doesn&#039;t fit in screenshot.&lt;br /&gt;
&lt;br /&gt;
==== Step 4: Edit and save form ====&lt;br /&gt;
[[File:Bulk_edit4.JPG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Level 2: Sets of activities ===&lt;br /&gt;
On this level system would be able to store sets of related instances and provide a one-click link to edit them. This&#039;ll require new db tables, so it probably 2.0 only.&lt;br /&gt;
&lt;br /&gt;
There must be a block which displays a sets as a links to edit them, and new controls on index.php page to create (manage?) sets.&lt;br /&gt;
&lt;br /&gt;
If events on update instance would be fired, than it is possible to have sets which are automatically update some settings whenever one of the activities in set are updated.&lt;br /&gt;
&lt;br /&gt;
== Architecture ==&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=File:Bulk_edit4.JPG&amp;diff=83231</id>
		<title>File:Bulk edit4.JPG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=File:Bulk_edit4.JPG&amp;diff=83231"/>
		<updated>2011-05-04T20:30:58Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Bulk module editing step 4: edit resulting form&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bulk module editing step 4: edit resulting form&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=File:Bulk_edit3.JPG&amp;diff=83230</id>
		<title>File:Bulk edit3.JPG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=File:Bulk_edit3.JPG&amp;diff=83230"/>
		<updated>2011-05-04T20:30:36Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Bulk module editing step 3: selecting fields to bulk edit&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bulk module editing step 3: selecting fields to bulk edit&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=File:Bulk_edit2.JPG&amp;diff=83229</id>
		<title>File:Bulk edit2.JPG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=File:Bulk_edit2.JPG&amp;diff=83229"/>
		<updated>2011-05-04T20:29:09Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Bulk module editing step 2: selecting instances&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bulk module editing step 2: selecting instances&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=File:Bulk_edit1.JPG&amp;diff=83228</id>
		<title>File:Bulk edit1.JPG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=File:Bulk_edit1.JPG&amp;diff=83228"/>
		<updated>2011-05-04T20:26:32Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Bulk module editing block step 1: selecting module&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bulk module editing block step 1: selecting module&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development_talk:Process&amp;diff=79015</id>
		<title>Development talk:Process</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development_talk:Process&amp;diff=79015"/>
		<updated>2010-12-09T14:51:44Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* If the issue is a bug in the current stable version requiring database changes, assign &amp;quot;Fix version&amp;quot; to DEVBACKLOG */ new section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I think &#039;User&#039; should be described as both &#039;finding issues&#039; and &#039;suggesting improvements&#039; for Moodle, even if the detail of how things are implemented are left for the Product Owner. Just thinking that a good chunk of the things I add to the Tracker are suggestions for improvement rather than &#039;issues&#039; as such and that we should promote this idea as a general rule - maybe :)&lt;br /&gt;
&lt;br /&gt;
:I agree with Mark.  Bug tracker should be used for actual bugs.  Discussion about future roadmaps, enhcnements and improvements is a separate matter, and in actual fact it is hard to engage in this.  I don&#039;t have an answer at the moment.  Maybe a place to discuss, and notification of when discussions have heated up (like specs are being prepared and a new roadmap developed for a component)  ie owners manage the discussion, but there is a clear place to go to engage, some indication of timelines etc.  --[[User:Derek Chirnside|Derek Chirnside]] 19:39, 25 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
Well, &amp;quot;issues&amp;quot; there was meant to cover &amp;quot;bugs&amp;quot; and &amp;quot;suggestions&amp;quot;, as the tracker does both.  I&#039;ll make it more explicit though.  [[User:Martin Dougiamas|Martin Dougiamas]] 10:12, 30 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
OK - I could live with this.  But I still prefer this clearer distinction as suggested by Mark:&lt;br /&gt;
&lt;br /&gt;
===User role===&lt;br /&gt;
&lt;br /&gt;
# Uses Moodle&lt;br /&gt;
# Finds issues/bugs (report in tracker)&lt;br /&gt;
# Suggests improvements (report in tracker)&lt;br /&gt;
&lt;br /&gt;
Question: is there a way in the tracker to keep issues in two lists: bugs and suggestions?  Maybe even like Google: you can suggest anything, but at any given time there are a few suggestions up for vote?  In Moodle, you can discuss anything, but someone is highlighting at any given time a few topics for focused discussions. On reflection the answer may be No - on this basis I am back to my original suggestion.  &lt;br /&gt;
&lt;br /&gt;
A bug is a bug - it is not working as we know it should.  A suggestion for an enhancement is partly an invitation to dialogue, vote.  Voting for bugs is silly - all bugs need fixing, and priorities are best (IMO) determined centrally.  So:&lt;br /&gt;
&lt;br /&gt;
#Uses Moodle&lt;br /&gt;
#Finds and reports bugs (use tracker)&lt;br /&gt;
#Suggests improvements (use tracker)&lt;br /&gt;
#Takes part in dialogue around suggested improvements &lt;br /&gt;
&lt;br /&gt;
[[User:Derek Chirnside|Derek Chirnside]] 06:18, 4 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
:Derek, thanks for your comments. Regarding a way in the tracker to keep issues in two lists: bugs and suggestions, when you create an issue you have a choice of issue types - bug, new feature, task and improvement. Thus, a search for all new features and improvements should generate a list of suggestions. --[[User:Helen Foster|Helen Foster]] 14:31, 4 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Backlog naming == &lt;br /&gt;
&lt;br /&gt;
Regarding the latest change about naming backlog versions with &amp;quot;STABLEBACKLOG/DEVBACKLOG&amp;quot;, I&#039;d recommend to use instead something like: &amp;quot;1.9.x backlog/2.0.x backlog/2.1.x backlog&amp;quot; because:&lt;br /&gt;
&lt;br /&gt;
# It saves us to move things when a new major release happens (so it won&#039;t be necessary to move all the DEVBACKLOG =&amp;gt; STABLEBACKLOG&amp;quot;.&lt;br /&gt;
# It supports &#039;&#039;&#039;multiple&#039;&#039;&#039; stable branches, like we have now (1.9.x, 2.0.x...)&lt;br /&gt;
# It respects the format used by both the Affected Branches and Fixed Branches custom fields that are really useful for a lot of filters.&lt;br /&gt;
&lt;br /&gt;
Ciao, [[User:Eloy Lafuente (stronk7)|Eloy Lafuente (stronk7)]] 23:33, 6 December 2010 (UTC) :-)&lt;br /&gt;
&lt;br /&gt;
:Addenda: Finally it has been decided to go to 2 backlogs only (stable/dev). Fair enough so developer (team) will look to the real branches were solution needs to be implemented. [[User:Eloy Lafuente (stronk7)|Eloy Lafuente (stronk7)]] 10:23, 9 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
==This does not look like scrum to me at all==&lt;br /&gt;
&lt;br /&gt;
I think we need a certified scrum master. This proposal IMHO seems to break nearly all the good Scrum practises described in books.&lt;br /&gt;
&lt;br /&gt;
Ciao, [[User:Petr Škoda (škoďák)|Petr Škoda (škoďák)]] 10:03, 9 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
:Agree! some points (see point 3 especially, both in STABLE/DEV teams, break the thing. It&#039;s (scrum, by team) master responsibility to discuss with product owner, not team itself! Isolation is a MUST.&lt;br /&gt;
&lt;br /&gt;
: Ciao, [[User:Eloy Lafuente (stronk7)|Eloy Lafuente (stronk7)]] 10:12, 9 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== If the issue is a bug in the current stable version requiring database changes, assign &amp;quot;Fix version&amp;quot; to DEVBACKLOG ==&lt;br /&gt;
&lt;br /&gt;
Hmm, maybe this should be governed up by bug severity too? You, sure, aren&#039;t going to say that every bug requiring DB update, however severe it is, should be left up to the next major release? --[[User:Oleg Sychev|Oleg Sychev]] 14:51, 9 December 2010 (UTC)&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:VSTU_projects&amp;diff=77730</id>
		<title>Development:VSTU projects</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:VSTU_projects&amp;diff=77730"/>
		<updated>2010-11-13T18:51:26Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Blocks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Lead: Sychev Oleg Aleksandrovich==&lt;br /&gt;
Senior Lecturer of POAS (Software Engineering) department&lt;br /&gt;
&lt;br /&gt;
===Core patches===&lt;br /&gt;
# [[Development:Categories editing interface improvment|Improvment of editing interface for category list]]. Moving category anywhere should require only one page reload. Developers: Sychev Oleg, Shkarupa Alex. Status: waiting reviewing!&lt;br /&gt;
# [[Development:Javascript-interface for repeat_elements function|Javascript-interface for repeat_elements function]] - adding new blanks to the form shoudn&#039;t require page reloads.&lt;br /&gt;
# [[Development:Forum thread subscription|Forum thread subscription]] - a long standing request to be able to subscript only to the thread, not whole forum.&lt;br /&gt;
&lt;br /&gt;
===Activity modules===&lt;br /&gt;
# [[Development:Assignment development|New assignment-replacement module]] with support for individual tasks (with custom fields), several grading criterions, automatic graders etc. Developers: Sychev Oleg, Erofeev Anatolius.&lt;br /&gt;
# [[Development:Subcourse module improvments|Subcourse module improvments]] - improve Subcourse module to get rid of buggy Metacourses and make it actually useful.&lt;br /&gt;
&lt;br /&gt;
===Question types===&lt;br /&gt;
# [[Preg question type]] - regular expression question type, developed on more solid algorithmic basis than regex one. Developers: Sychev Oleg, Kolesov Dmitry.&lt;br /&gt;
# [[Development:Auto-feedback shortanswer question|Auto-feedback shortanswer question]] shortanswer question which could be able to detect typos and misplaced, missing or extra words and give appropriate feedback.&lt;br /&gt;
&lt;br /&gt;
===Blocks===&lt;br /&gt;
# [[Development:Linked activities|Linked activities]] to allow editing properties of multiple activities at once.&lt;br /&gt;
# [[Auto role assignment block]] is useful to temporary restrict (or enhance) user abilities while doing something.&lt;br /&gt;
# [[Supervised block]] is much better than IP filter+time control if you want students to be able to do something only under teacher supervision.&lt;br /&gt;
&lt;br /&gt;
===Libraries===&lt;br /&gt;
# [[Development:Auto-backup library|Auto-backup library]] - adding very small information to install.xml files it is possible to automate many tedious tasks of programming backup/restore for the plugins.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=77041</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=77041"/>
		<updated>2010-10-25T11:41:21Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Deterministic finite state automata (DFA) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code, regex parsing and error reporting - Oleg Sychev;&lt;br /&gt;
# regex parsing, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see [[#Development plans|development plans]] section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
For now new code with all this functionality located in the HEAD branch of preg question type (you could also download it from the [http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=1901&amp;amp;filter=1 Modules and Plugins database] using &#039;latest version&#039; link. It is works with Moodle 2.0. The code considered BETA quality - use it with care! If you find any bugs please report them on the tracker.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more d&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
If no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances - the placeholder syntax may become complex and prone to errors.&lt;br /&gt;
&lt;br /&gt;
===Error reporting===&lt;br /&gt;
Native PHP preg extension functions only report if there error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine couldn&#039;t tell you much about what is error .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DFA&#039;&#039;&#039; engine use custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so it supports advanced error reporting. The are several class of potential errors reported:&lt;br /&gt;
* unclosed square brackets of character class;&lt;br /&gt;
* unclosed opening parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* unopened closing parenthesis;&lt;br /&gt;
* empty parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* quantifiers without operand, i.e. at the start of (sub)expression with nothing to repeat;&lt;br /&gt;
* three or more top-level alternatives in the conditional subpattern.&lt;br /&gt;
PCRE (and preg functions) treat most of them as &#039;&#039;&#039;non-errors&#039;&#039;&#039;, making many characters meaning context-dependent. For example quantifier {2,4} placed at the start of regular expression lose it&#039;s meaning as quantifier and is treated as five-characters sequence instead (that matches with {2,4}). However such syntax is very prone to errors and make writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I&#039;m vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE/preg. If you are stand for or against this decision please write you positions and reasons on the page comments please. It may be best to have two modes, but this literally means two parsers and this is out of current scope of development. There are more pressing issues ahead.&lt;br /&gt;
&lt;br /&gt;
===Looking for missing things===&lt;br /&gt;
Joseph Rezeau REGEXP question type has a special syntax for &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that would work when something is absent in the answer (and give appropriate feedback to the student). So if you want to look out for the missing word &#039;&#039;&#039;necessary&#039;&#039;&#039; in the response, you&#039;ll add this answer (WARNING - REGEXP only syntax on the next line):&lt;br /&gt;
  --.*\bnecessary\b.*&lt;br /&gt;
where \b defines a word boundary, while .* ensures that this word could be anywhere in the response.&lt;br /&gt;
&lt;br /&gt;
There is no need to have such features in the PREG question type, since similar effect could be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. Equivalent regular expression to look for the missing word &#039;&#039;&#039;necessary&#039;&#039;&#039; would be&lt;br /&gt;
  ^(?!.*\bnecessary\b.*)&lt;br /&gt;
where&lt;br /&gt;
* &#039;&#039;&#039;(?!.*\bnecessary\b.*)&#039;&#039;&#039; is a &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039;, that allows matching only if there are no word &#039;&#039;&#039;necessary&#039;&#039;&#039; ahead of some point in the string;&lt;br /&gt;
* &#039;&#039;&#039;^&#039;&#039;&#039; is an assertion too, that anchoring the match to the start of response (otherwise there would be places in response after the word &amp;quot;necessary&amp;quot;, where matching is possible even if the word is present).&lt;br /&gt;
&lt;br /&gt;
In case the description is difficult to you, just surround regexp to be missing with &#039;&#039;&#039;^(?!&#039;&#039;&#039; and &#039;&#039;&#039;)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (locale-aware, but not Unicode for performance reasons, as in standard regular expression functions)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operators DFA algorithm could&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=77039</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=77039"/>
		<updated>2010-10-25T11:30:53Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Development plans */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code, regex parsing and error reporting - Oleg Sychev;&lt;br /&gt;
# regex parsing, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see [[#Development plans|development plans]] section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
For now new code with all this functionality located in the HEAD branch of preg question type (you could also download it from the [http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=1901&amp;amp;filter=1 Modules and Plugins database] using &#039;latest version&#039; link. It is works with Moodle 2.0. The code considered BETA quality - use it with care! If you find any bugs please report them on the tracker.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more d&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
If no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances - the placeholder syntax may become complex and prone to errors.&lt;br /&gt;
&lt;br /&gt;
===Error reporting===&lt;br /&gt;
Native PHP preg extension functions only report if there error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine couldn&#039;t tell you much about what is error .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DFA&#039;&#039;&#039; engine use custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so it supports advanced error reporting. The are several class of potential errors reported:&lt;br /&gt;
* unclosed square brackets of character class;&lt;br /&gt;
* unclosed opening parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* unopened closing parenthesis;&lt;br /&gt;
* empty parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* quantifiers without operand, i.e. at the start of (sub)expression with nothing to repeat;&lt;br /&gt;
* three or more top-level alternatives in the conditional subpattern.&lt;br /&gt;
PCRE (and preg functions) treat most of them as &#039;&#039;&#039;non-errors&#039;&#039;&#039;, making many characters meaning context-dependent. For example quantifier {2,4} placed at the start of regular expression lose it&#039;s meaning as quantifier and is treated as five-characters sequence instead (that matches with {2,4}). However such syntax is very prone to errors and make writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I&#039;m vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE/preg. If you are stand for or against this decision please write you positions and reasons on the page comments please. It may be best to have two modes, but this literally means two parsers and this is out of current scope of development. There are more pressing issues ahead.&lt;br /&gt;
&lt;br /&gt;
===Looking for missing things===&lt;br /&gt;
Joseph Rezeau REGEXP question type has a special syntax for &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that would work when something is absent in the answer (and give appropriate feedback to the student). So if you want to look out for the missing word &#039;&#039;&#039;necessary&#039;&#039;&#039; in the response, you&#039;ll add this answer (WARNING - REGEXP only syntax on the next line):&lt;br /&gt;
  --.*\bnecessary\b.*&lt;br /&gt;
where \b defines a word boundary, while .* ensures that this word could be anywhere in the response.&lt;br /&gt;
&lt;br /&gt;
There is no need to have such features in the PREG question type, since similar effect could be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. Equivalent regular expression to look for the missing word &#039;&#039;&#039;necessary&#039;&#039;&#039; would be&lt;br /&gt;
  ^(?!.*\bnecessary\b.*)&lt;br /&gt;
where&lt;br /&gt;
* &#039;&#039;&#039;(?!.*\bnecessary\b.*)&#039;&#039;&#039; is a &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039;, that allows matching only if there are no word &#039;&#039;&#039;necessary&#039;&#039;&#039; ahead of some point in the string;&lt;br /&gt;
* &#039;&#039;&#039;^&#039;&#039;&#039; is an assertion too, that anchoring the match to the start of response (otherwise there would be places in response after the word &amp;quot;necessary&amp;quot;, where matching is possible even if the word is present).&lt;br /&gt;
&lt;br /&gt;
In case the description is difficult to you, just surround regexp to be missing with &#039;&#039;&#039;^(?!&#039;&#039;&#039; and &#039;&#039;&#039;)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operators DFA algorithm could&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=77038</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=77038"/>
		<updated>2010-10-25T11:30:12Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Adding description how to look for missing words etc.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code, regex parsing and error reporting - Oleg Sychev;&lt;br /&gt;
# regex parsing, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see [[#Development plans|development plans]] section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
For now new code with all this functionality located in the HEAD branch of preg question type (you could also download it from the [http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=1901&amp;amp;filter=1 Modules and Plugins database] using &#039;latest version&#039; link. It is works with Moodle 2.0. The code considered BETA quality - use it with care! If you find any bugs please report them on the tracker.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more d&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
If no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances - the placeholder syntax may become complex and prone to errors.&lt;br /&gt;
&lt;br /&gt;
===Error reporting===&lt;br /&gt;
Native PHP preg extension functions only report if there error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine couldn&#039;t tell you much about what is error .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DFA&#039;&#039;&#039; engine use custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so it supports advanced error reporting. The are several class of potential errors reported:&lt;br /&gt;
* unclosed square brackets of character class;&lt;br /&gt;
* unclosed opening parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* unopened closing parenthesis;&lt;br /&gt;
* empty parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* quantifiers without operand, i.e. at the start of (sub)expression with nothing to repeat;&lt;br /&gt;
* three or more top-level alternatives in the conditional subpattern.&lt;br /&gt;
PCRE (and preg functions) treat most of them as &#039;&#039;&#039;non-errors&#039;&#039;&#039;, making many characters meaning context-dependent. For example quantifier {2,4} placed at the start of regular expression lose it&#039;s meaning as quantifier and is treated as five-characters sequence instead (that matches with {2,4}). However such syntax is very prone to errors and make writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I&#039;m vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE/preg. If you are stand for or against this decision please write you positions and reasons on the page comments please. It may be best to have two modes, but this literally means two parsers and this is out of current scope of development. There are more pressing issues ahead.&lt;br /&gt;
&lt;br /&gt;
===Looking for missing things===&lt;br /&gt;
Joseph Rezeau REGEXP question type has a special syntax for &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that would work when something is absent in the answer (and give appropriate feedback to the student). So if you want to look out for the missing word &#039;&#039;&#039;necessary&#039;&#039;&#039; in the response, you&#039;ll add this answer (WARNING - REGEXP only syntax on the next line):&lt;br /&gt;
  --.*\bnecessary\b.*&lt;br /&gt;
where \b defines a word boundary, while .* ensures that this word could be anywhere in the response.&lt;br /&gt;
&lt;br /&gt;
There is no need to have such features in the PREG question type, since similar effect could be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. Equivalent regular expression to look for the missing word &#039;&#039;&#039;necessary&#039;&#039;&#039; would be&lt;br /&gt;
  ^(?!.*\bnecessary\b.*)&lt;br /&gt;
where&lt;br /&gt;
* &#039;&#039;&#039;(?!.*\bnecessary\b.*)&#039;&#039;&#039; is a &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039;, that allows matching only if there are no word &#039;&#039;&#039;necessary&#039;&#039;&#039; ahead of some point in the string;&lt;br /&gt;
* &#039;&#039;&#039;^&#039;&#039;&#039; is an assertion too, that anchoring the match to the start of response (otherwise there would be places in response after the word &amp;quot;necessary&amp;quot;, where matching is possible even if the word is present).&lt;br /&gt;
&lt;br /&gt;
In case the description is difficult to you, just surround regexp to be missing with &#039;&#039;&#039;^(?!&#039;&#039;&#039; and &#039;&#039;&#039;)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development_talk:Forum_thread_subscription&amp;diff=76812</id>
		<title>Development talk:Forum thread subscription</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development_talk:Forum_thread_subscription&amp;diff=76812"/>
		<updated>2010-10-15T22:20:58Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I didn&#039;t think adding new option for discussion subscription is good. The options for forum and discussion subscription are not orthogonal. How should behave forum with disabled forum subscription and auto thread subscriptions? Or forced forum subscriptions and optional thread ones? It&#039;s better to add new subscription modes than adding separate option.&lt;br /&gt;
--[[User:Oleg Sychev|Oleg Sychev]] 22:20, 15 October 2010 (UTC)&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development_talk:Javascript-interface_for_repeat_elements_function&amp;diff=76778</id>
		<title>Development talk:Javascript-interface for repeat elements function</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development_talk:Javascript-interface_for_repeat_elements_function&amp;diff=76778"/>
		<updated>2010-10-13T21:43:15Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;1. Consider optionally sending AJAX request to the server to inform it of the adding blanks if it will be found really necessary. --[[User:Oleg Sychev|Oleg Sychev]] 21:43, 13 October 2010 (UTC)&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:Javascript-interface_for_repeat_elements_function&amp;diff=76777</id>
		<title>Development:Javascript-interface for repeat elements function</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:Javascript-interface_for_repeat_elements_function&amp;diff=76777"/>
		<updated>2010-10-13T21:41:47Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Moving blanks */ - clearifying example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;GOAL&#039;&#039;&#039;: improve usability by using dynamic form editing.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;EXAMPLE&#039;&#039;&#039;: let&#039;s imagine you have a multichoice question with 8 or more choices and the order of choices matters. Imagine you found you want add new choice at the start or delete first choice. With current interface you are in a BIG trouble.&lt;br /&gt;
&lt;br /&gt;
At this moment form editing, especially adding new blank answers, units, choices and fields requires page reloading. &lt;br /&gt;
Editing blanks order or blank removing is not possible without page reloading. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;TASK&#039;&#039;&#039;: The main task is development of JavaScript-interface for repeat_elemens function. Other task is make ADD, REMOVE and MOVE actions without	page reloading. If JavaScript is not available in user&#039;s browser this actions must work with page reloading, like it works now.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Where it will be. ==&lt;br /&gt;
Here, there are some pages, where this function uses, in the list below :&lt;br /&gt;
* Course editing -&amp;gt; Adding a new Choice - Adding fields to form;&lt;br /&gt;
* Course editing -&amp;gt; Adding a new Quiz -  Adding new feedback fields;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a calculated question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a calculated question - Adding Blanks for Units;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Calculated Multichoice question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Simple Calculated question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Simple Calculated question Adding Blanks for Units;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Matching question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Multiple choice question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Numerical question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Numerical question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Adding a short answer question - Adding Blanks for choices;&lt;br /&gt;
etc.&lt;br /&gt;
As repeat_elements is core function, it should work anywhere.&lt;br /&gt;
&lt;br /&gt;
This core patch will add new buttons to the form interface.&lt;br /&gt;
http://imglink.ru/pictures/21-09-10/f85258328f02d65164e6a15644e5d8a9.jpg&lt;br /&gt;
[[Image:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Adding new blanks ==&lt;br /&gt;
&lt;br /&gt;
Adding new blanks realize by pushing &amp;quot;Add blanks for SMTH&amp;quot; button. &lt;br /&gt;
There will be a hidden empty blank. When user will push the button, it&#039;s duplicate will be added in the end of blank&#039;s list.&lt;br /&gt;
&lt;br /&gt;
== Removing blanks. ==&lt;br /&gt;
&lt;br /&gt;
It&#039;s possible to remove only &#039;Empty&#039; blanks. Empty blank is that blank, where all key-fields are empty or have default values.&lt;br /&gt;
List of elements, which will be check:&lt;br /&gt;
*FILE.&lt;br /&gt;
File upload input box with browse button. Empty, when filepath is empty.&lt;br /&gt;
*FILEPICKER&lt;br /&gt;
General replacement of file element in Moodle 2.0 . Empty, when filepath is empty.&lt;br /&gt;
*HTMLEDITOR&lt;br /&gt;
Empty when it doesn&#039;t contains eny content.&lt;br /&gt;
*PASSWORD and PASSWORDUNMASK&lt;br /&gt;
A password element. Empty, when it doesn&#039;t contains any text.&lt;br /&gt;
TEXT&lt;br /&gt;
Simple text input element. Empty, when it doesn&#039;t contains any text.&lt;br /&gt;
*TEXTAREA&lt;br /&gt;
A textarea element. Empty, when it doesn&#039;t contains any text.&lt;br /&gt;
		&lt;br /&gt;
Other form elements like DATEPICKER, RECAPTCHA, SELECT, HIDDEN and others can contain&#039;s any values, becuse they won&#039;t be check.&lt;br /&gt;
	&lt;br /&gt;
Blank&#039;s removing realize by pushind remove button(see screenshot) and confirming your choise.&lt;br /&gt;
If blank&#039;s removing isn&#039;t possible user will get allert with reason.&lt;br /&gt;
&lt;br /&gt;
== Moving blanks ==&lt;br /&gt;
&lt;br /&gt;
Blanks moving is available by dragging them with move button.&lt;br /&gt;
Blank&#039;s original order will be reported on saving , by using hidden elements, named &amp;quot;ord&amp;quot;, which will save order number of each blank. You could use it to determine where each element is go (even if edited). This is useful, for example, when editing question with existing attempts: saving procedure would know new indexes of all previous answers even if they are moved AND their content edited, so it could ajust student&#039;s responses accordingly.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
Creation:&lt;br /&gt;
  We&#039;ve got 3 empty blanks. Values of their &amp;quot;ords&amp;quot; are -1,-1,-1.&lt;br /&gt;
Editing:&lt;br /&gt;
  We edit all blanks. We&#039;ve got 3 blanks with ords -1,-1,-1.&lt;br /&gt;
Saving:&lt;br /&gt;
  We save question(or smth else), Ords: -1,-1,-1&lt;br /&gt;
Loading:&lt;br /&gt;
  Ords: 0,1,2&lt;br /&gt;
Removing:&lt;br /&gt;
  We remove blank, from the middle of blank-list. We&#039;ve got 2 blanks with ords 0,2.&lt;br /&gt;
Adding:&lt;br /&gt;
  We add two blanks. We&#039;ve got ords: 0,2,-1,-1&lt;br /&gt;
Moving:&lt;br /&gt;
  We move last blank to be first. Ords: -1,0,2,-1&lt;br /&gt;
&lt;br /&gt;
So, the saving procedure would know that answer number 1 is deleted and answers number 0 and 2 changed their places.&lt;br /&gt;
&lt;br /&gt;
== Function Interface ==&lt;br /&gt;
&lt;br /&gt;
Interface of repeat_elements function will receive 7 params instead of 8 in original version.&lt;br /&gt;
New param $prefix gets prefix for additional form elements, added by the repeat_elements function. &lt;br /&gt;
In original vewrsion this names were separated and saved in two params: $repeathiddenname and $addfieldsname.&lt;br /&gt;
This preliminary change, simplifying the interface and helps to solve the basic problem without changing it.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:Javascript-interface_for_repeat_elements_function&amp;diff=76776</id>
		<title>Development:Javascript-interface for repeat elements function</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:Javascript-interface_for_repeat_elements_function&amp;diff=76776"/>
		<updated>2010-10-13T21:35:40Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Example where it useful set&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;GOAL&#039;&#039;&#039;: improve usability by using dynamic form editing.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;EXAMPLE&#039;&#039;&#039;: let&#039;s imagine you have a multichoice question with 8 or more choices and the order of choices matters. Imagine you found you want add new choice at the start or delete first choice. With current interface you are in a BIG trouble.&lt;br /&gt;
&lt;br /&gt;
At this moment form editing, especially adding new blank answers, units, choices and fields requires page reloading. &lt;br /&gt;
Editing blanks order or blank removing is not possible without page reloading. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;TASK&#039;&#039;&#039;: The main task is development of JavaScript-interface for repeat_elemens function. Other task is make ADD, REMOVE and MOVE actions without	page reloading. If JavaScript is not available in user&#039;s browser this actions must work with page reloading, like it works now.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Where it will be. ==&lt;br /&gt;
Here, there are some pages, where this function uses, in the list below :&lt;br /&gt;
* Course editing -&amp;gt; Adding a new Choice - Adding fields to form;&lt;br /&gt;
* Course editing -&amp;gt; Adding a new Quiz -  Adding new feedback fields;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a calculated question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a calculated question - Adding Blanks for Units;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Calculated Multichoice question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Simple Calculated question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Simple Calculated question Adding Blanks for Units;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Matching question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Multiple choice question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Numerical question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Editing a Numerical question - Adding Blanks for choices;&lt;br /&gt;
* Course editing -&amp;gt; Editing Quiz-&amp;gt; Adding a short answer question - Adding Blanks for choices;&lt;br /&gt;
etc.&lt;br /&gt;
As repeat_elements is core function, it should work anywhere.&lt;br /&gt;
&lt;br /&gt;
This core patch will add new buttons to the form interface.&lt;br /&gt;
http://imglink.ru/pictures/21-09-10/f85258328f02d65164e6a15644e5d8a9.jpg&lt;br /&gt;
[[Image:7.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Adding new blanks ==&lt;br /&gt;
&lt;br /&gt;
Adding new blanks realize by pushing &amp;quot;Add blanks for SMTH&amp;quot; button. &lt;br /&gt;
There will be a hidden empty blank. When user will push the button, it&#039;s duplicate will be added in the end of blank&#039;s list.&lt;br /&gt;
&lt;br /&gt;
== Removing blanks. ==&lt;br /&gt;
&lt;br /&gt;
It&#039;s possible to remove only &#039;Empty&#039; blanks. Empty blank is that blank, where all key-fields are empty or have default values.&lt;br /&gt;
List of elements, which will be check:&lt;br /&gt;
*FILE.&lt;br /&gt;
File upload input box with browse button. Empty, when filepath is empty.&lt;br /&gt;
*FILEPICKER&lt;br /&gt;
General replacement of file element in Moodle 2.0 . Empty, when filepath is empty.&lt;br /&gt;
*HTMLEDITOR&lt;br /&gt;
Empty when it doesn&#039;t contains eny content.&lt;br /&gt;
*PASSWORD and PASSWORDUNMASK&lt;br /&gt;
A password element. Empty, when it doesn&#039;t contains any text.&lt;br /&gt;
TEXT&lt;br /&gt;
Simple text input element. Empty, when it doesn&#039;t contains any text.&lt;br /&gt;
*TEXTAREA&lt;br /&gt;
A textarea element. Empty, when it doesn&#039;t contains any text.&lt;br /&gt;
		&lt;br /&gt;
Other form elements like DATEPICKER, RECAPTCHA, SELECT, HIDDEN and others can contain&#039;s any values, becuse they won&#039;t be check.&lt;br /&gt;
	&lt;br /&gt;
Blank&#039;s removing realize by pushind remove button(see screenshot) and confirming your choise.&lt;br /&gt;
If blank&#039;s removing isn&#039;t possible user will get allert with reason.&lt;br /&gt;
&lt;br /&gt;
== Moving blanks ==&lt;br /&gt;
&lt;br /&gt;
Blanks moving is available by dragging them with move button.&lt;br /&gt;
Blank&#039;s original order will be preserved , by using hidden elements, named &amp;quot;ord&amp;quot;, which will save order number of each blank.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
Creation:&lt;br /&gt;
  We&#039;ve got 3 empty blanks. Values of their &amp;quot;ords&amp;quot; are -1,-1,-1.&lt;br /&gt;
Editing:&lt;br /&gt;
  We edit all blanks. We&#039;ve got 3 blanks with ords -1,-1,-1.&lt;br /&gt;
Saving:&lt;br /&gt;
  We save question(or smth else), Ords: -1,-1,-1&lt;br /&gt;
Loading:&lt;br /&gt;
  Ords: 0,1,2&lt;br /&gt;
Removing:&lt;br /&gt;
  We remove blank, from the middle of blank-list. We&#039;ve got 2 blanks with ords 0,2.&lt;br /&gt;
Adding:&lt;br /&gt;
  We add two blanks. We&#039;ve got ords: 0,2,-1,-1&lt;br /&gt;
Moving:&lt;br /&gt;
  We move last blank betwen first and second. Ords: 0,-1,2,-1&lt;br /&gt;
&lt;br /&gt;
So, when we create or add new blanks, initial values of their ords are -1. When we load blanks, which were saved earlier, their ord&#039;s values are 0,1,2,3...,n .&lt;br /&gt;
&lt;br /&gt;
== Function Interface ==&lt;br /&gt;
&lt;br /&gt;
Interface of repeat_elements function will receive 7 params instead of 8 in original version.&lt;br /&gt;
New param $prefix gets prefix for additional form elements, added by the repeat_elements function. &lt;br /&gt;
In original vewrsion this names were separated and saved in two params: $repeathiddenname and $addfieldsname.&lt;br /&gt;
This preliminary change, simplifying the interface and helps to solve the basic problem without changing it.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development_talk:Javascript-interface_for_repeat_elements_function&amp;diff=76422</id>
		<title>Development talk:Javascript-interface for repeat elements function</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development_talk:Javascript-interface_for_repeat_elements_function&amp;diff=76422"/>
		<updated>2010-10-02T21:27:16Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;# &amp;quot;Where it will be&amp;quot; - don&#039;t think full list is necessary, you may just list some places. Core function should work anywhere it called, calling isn&#039;t you problems.&lt;br /&gt;
# I don&#039;t think using AJAX to add new blanks is a good idea. Why don&#039;t just add them using javascript on the page, creating new HTML-elements (you&#039;ll need a hidden empty blank somewhere ready to copying) and changing hidden element with the number of blanks? The server is stateless, it won&#039;t find anything strange when the modified form will be submitted.&lt;br /&gt;
# Last sentences about deleting/moving isn&#039;t necessary, you are talking with the smart people, they sure know what those icon do. At least make pictures smaller - or just delete all this.&lt;br /&gt;
# Still nothing about preserving original numbers of the blanks and proposed changes in the repeat_elements interface to set $prefix instead of two existing (and one new) element names...&lt;br /&gt;
# I don&#039;t see any &amp;quot;restore order&amp;quot; button on the screenshots, I also don&#039;t sure why it is necessary while it definitely wouldn&#039;t be easy thing to implement. If you implement all other things that will be quite enought.&lt;br /&gt;
--[[User:Oleg Sychev|Oleg Sychev]] 21:27, 2 October 2010 (UTC)&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development_talk:Forum_thread_subscription&amp;diff=76344</id>
		<title>Development talk:Forum thread subscription</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development_talk:Forum_thread_subscription&amp;diff=76344"/>
		<updated>2010-10-01T13:30:01Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: New page: # when showing interface screens please don&amp;#039;t show full screen - cut a part you are changing and area about them for context (when changing block insert just this block) - look at [[Develo...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;# when showing interface screens please don&#039;t show full screen - cut a part you are changing and area about them for context (when changing block insert just this block) - look at [[Development:Categories_editing_interface_improvment]] for examples&lt;br /&gt;
# write about what changes will be made in forum options form (new options for subscriptions modes, for user auto-subscription mode too)&lt;br /&gt;
# write about how you patch will work when subscription mode for the forum changes&lt;br /&gt;
--[[User:Oleg Sychev|Oleg Sychev]] 13:30, 1 October 2010 (UTC)&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development_talk:Javascript-interface_for_repeat_elements_function&amp;diff=76343</id>
		<title>Development talk:Javascript-interface for repeat elements function</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development_talk:Javascript-interface_for_repeat_elements_function&amp;diff=76343"/>
		<updated>2010-10-01T13:21:35Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: New page: I already told you what I think about this page: # one screenshot is quite enought for what you done, it isn&amp;#039;t necessary to add screenshot to every alert message box - this is not Microsof...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I already told you what I think about this page:&lt;br /&gt;
# one screenshot is quite enought for what you done, it isn&#039;t necessary to add screenshot to every alert message box - this is not Microsoft User Manual, just a sentence that this will be done is enought;&lt;br /&gt;
# one more screenshot needed to show how it would look when blank contains exactly one string, without any fieldsets around it&lt;br /&gt;
# write out thoroughtly, which field types you would check for been non-empty when deleting&lt;br /&gt;
# write how you would remember initial positions of the blanks and return them after editing&lt;br /&gt;
# and mind you spelling, correct &amp;quot;choise&amp;quot; all over the place.&lt;br /&gt;
Now I am wondering why it still not done...&lt;br /&gt;
--[[User:Oleg Sychev|Oleg Sychev]] 13:21, 1 October 2010 (UTC)&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=76316</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=76316"/>
		<updated>2010-09-29T20:46:37Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Precedence and order of evaluation */ - correcting a typo in the answer&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code, regex parsing and error reporting - Oleg Sychev;&lt;br /&gt;
# regex parsing, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see [[#Development plans|development plans]] section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
For now new code with all this functionality located in the HEAD branch of preg question type (you could also download it from the [http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=1901&amp;amp;filter=1 Modules and Plugins database] using &#039;latest version&#039; link. It is works with Moodle 2.0. The code considered BETA quality - use it with care! If you find any bugs please report them on the tracker.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more d&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
If no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances - the placeholder syntax may become complex and prone to errors.&lt;br /&gt;
&lt;br /&gt;
===Error reporting===&lt;br /&gt;
Native PHP preg extension functions only report if there error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine couldn&#039;t tell you much about what is error .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DFA&#039;&#039;&#039; engine use custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so it supports advanced error reporting. The are several class of potential errors reported:&lt;br /&gt;
* unclosed square brackets of character class;&lt;br /&gt;
* unclosed opening parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* unopened closing parenthesis;&lt;br /&gt;
* empty parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* quantifiers without operand, i.e. at the start of (sub)expression with nothing to repeat;&lt;br /&gt;
* three or more top-level alternatives in the conditional subpattern.&lt;br /&gt;
PCRE (and preg functions) treat most of them as &#039;&#039;&#039;non-errors&#039;&#039;&#039;, making many characters meaning context-dependent. For example quantifier {2,4} placed at the start of regular expression lose it&#039;s meaning as quantifier and is treated as five-characters sequence instead (that matches with {2,4}). However such syntax is very prone to errors and make writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I&#039;m vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE/preg. If you are stand for or against this decision please write you positions and reasons on the page comments please. It may be best to have two modes, but this literally means two parsers and this is out of current scope of development. There are more pressing issues ahead.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:VSTU_projects&amp;diff=76019</id>
		<title>Development:VSTU projects</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:VSTU_projects&amp;diff=76019"/>
		<updated>2010-09-21T16:20:36Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Blocks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Lead: Sychev Oleg Aleksandrovich==&lt;br /&gt;
Senior Lecturer of POAS (Software Engineering) department&lt;br /&gt;
&lt;br /&gt;
===Core patches===&lt;br /&gt;
# [[Development:Categories editing interface improvment|Improvment of editing interface for category list]]. Moving category anywhere should require only one page reload. Developers: Sychev Oleg, Shkarupa Alex. Status: waiting reviewing!&lt;br /&gt;
# [[Development:Javascript-interface for repeat_elements function|Javascript-interface for repeat_elements function]] - adding new blanks to the form shoudn&#039;t require page reloads.&lt;br /&gt;
# [[Development:Forum thread subscription|Forum thread subscription]] - a long standing request to be able to subscript only to the thread, not whole forum.&lt;br /&gt;
&lt;br /&gt;
===Activity modules===&lt;br /&gt;
# [[Development:Assignment development|New assignment-replacement module]] with support for individual tasks (with custom fields), several grading criterions, automatic graders etc. Developers: Sychev Oleg, Erofeev Anatolius.&lt;br /&gt;
# [[Development:Subcourse module improvments|Subcourse module improvments]] - improve Subcourse module to get rid of buggy Metacourses and make it actually useful.&lt;br /&gt;
&lt;br /&gt;
===Question types===&lt;br /&gt;
# [[Preg question type]] - regular expression question type, developed on more solid algorithmic basis than regex one. Developers: Sychev Oleg, Kolesov Dmitry.&lt;br /&gt;
# [[Development:Auto-feedback shortanswer question|Auto-feedback shortanswer question]] shortanswer question which could be able to detect typos and misplaced, missing or extra words and give appropriate feedback.&lt;br /&gt;
&lt;br /&gt;
===Blocks===&lt;br /&gt;
# [[Development:Linked activities|Linked activities]] to allow editing properties of multiple activities at once.&lt;br /&gt;
# [[Auto role assignment block]] is useful to temporary restrict (or enhance) user abilities while doing something.&lt;br /&gt;
# [[Under supervision block]] is much better than IP filter+time control if you want students to be able to do something only under teacher supervision.&lt;br /&gt;
&lt;br /&gt;
===Libraries===&lt;br /&gt;
# [[Development:Auto-backup library|Auto-backup library]] - adding very small information to install.xml files it is possible to automate many tedious tasks of programming backup/restore for the plugins.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=76018</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=76018"/>
		<updated>2010-09-21T16:06:07Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code, regex parsing and error reporting - Oleg Sychev;&lt;br /&gt;
# regex parsing, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see [[#Development plans|development plans]] section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
For now new code with all this functionality located in the HEAD branch of preg question type (you could also download it from the [http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=1901&amp;amp;filter=1 Modules and Plugins database] using &#039;latest version&#039; link. It is works with Moodle 2.0. The code considered BETA quality - use it with care! If you find any bugs please report them on the tracker.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more b&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
If no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances - the placeholder syntax may become complex and prone to errors.&lt;br /&gt;
&lt;br /&gt;
===Error reporting===&lt;br /&gt;
Native PHP preg extension functions only report if there error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine couldn&#039;t tell you much about what is error .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DFA&#039;&#039;&#039; engine use custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so it supports advanced error reporting. The are several class of potential errors reported:&lt;br /&gt;
* unclosed square brackets of character class;&lt;br /&gt;
* unclosed opening parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* unopened closing parenthesis;&lt;br /&gt;
* empty parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* quantifiers without operand, i.e. at the start of (sub)expression with nothing to repeat;&lt;br /&gt;
* three or more top-level alternatives in the conditional subpattern.&lt;br /&gt;
PCRE (and preg functions) treat most of them as &#039;&#039;&#039;non-errors&#039;&#039;&#039;, making many characters meaning context-dependent. For example quantifier {2,4} placed at the start of regular expression lose it&#039;s meaning as quantifier and is treated as five-characters sequence instead (that matches with {2,4}). However such syntax is very prone to errors and make writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I&#039;m vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE/preg. If you are stand for or against this decision please write you positions and reasons on the page comments please. It may be best to have two modes, but this literally means two parsers and this is out of current scope of development. There are more pressing issues ahead.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=76017</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=76017"/>
		<updated>2010-09-21T15:57:22Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Subpattern capturing and feedback */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see [[#Development plans|development plans]] section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more b&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
If no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances - the placeholder syntax may become complex and prone to errors.&lt;br /&gt;
&lt;br /&gt;
===Error reporting===&lt;br /&gt;
Native PHP preg extension functions only report if there error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine couldn&#039;t tell you much about what is error .&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;DFA&#039;&#039;&#039; engine use custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so it supports advanced error reporting. The are several class of potential errors reported:&lt;br /&gt;
* unclosed square brackets of character class;&lt;br /&gt;
* unclosed opening parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* unopened closing parenthesis;&lt;br /&gt;
* empty parenthesis of any sort (different forms of subpatterns and assertions);&lt;br /&gt;
* quantifiers without operand, i.e. at the start of (sub)expression with nothing to repeat;&lt;br /&gt;
* three or more top-level alternatives in the conditional subpattern.&lt;br /&gt;
PCRE (and preg functions) treat most of them as &#039;&#039;&#039;non-errors&#039;&#039;&#039;, making many characters meaning context-dependent. For example quantifier {2,4} placed at the start of regular expression lose it&#039;s meaning as quantifier and is treated as five-characters sequence instead (that matches with {2,4}). However such syntax is very prone to errors and make writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I&#039;m vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE/preg. If you are stand for or against this decision please write you positions and reasons on the page comments please. It may be best to have two modes, but this literally means two parsers and this is out of current scope of development. There are more pressing issues ahead.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75960</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75960"/>
		<updated>2010-09-18T20:27:36Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see [[#Development plans|development plans]] section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more b&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
The only downside is if no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75959</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75959"/>
		<updated>2010-09-18T20:26:35Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* PHP preg extension */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see Development plans section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more b&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
The only downside is if no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. However it will support subpattern capturing. Choose it when you need complex regexp features other engines don&#039;t support, subpattern capturing or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75958</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75958"/>
		<updated>2010-09-18T20:25:34Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Development plans */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see Development plans section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more b&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
The only downside is if no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. Choose it when you need complex regexp features other engines don&#039;t support or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Improve Unicode support of custom matching engines&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75957</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75957"/>
		<updated>2010-09-18T20:24:27Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Hinting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see Development plans section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more b&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Subpattern capturing and feedback===&lt;br /&gt;
Any pair of round brackets in the regular expressions are considered a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when doing matching engine (supporting subpatterns) remember (&#039;&#039;&#039;capture&#039;&#039;&#039;) not only whole match, but it&#039;s parts corresponding to all subpatterns. Subpatterns can be nested. If subpattern is repeated (i.e. have quantifier), than only last match of all repeats will be captured. If you want to change order of evaluation without defining a subpattern to capture (which will speed up processing), you should use (?:  ) instead of just (  ). Asserts don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening brackets. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole match, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You could insert them in the &#039;&#039;answer&#039;s feedback&#039;&#039; using simple placeholders: &#039;&#039;&#039;{$0}&#039;&#039;&#039; is replaced by the whole match, &#039;&#039;&#039;{$1}&#039;&#039;&#039; by first subpattern value etc. That can improve the quality of you feedback. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers could have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine coudn&#039;t do it, so you could use only {$0} placeholder working with DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at regex defining an decimal number with optional integral part:&lt;br /&gt;
 [+\-]?([0-9]+)?\.([0-9]+)&lt;br /&gt;
It has two subpatterns: first capturing integral part, second - fractional part of the number.&lt;br /&gt;
You writed feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {2}&lt;br /&gt;
Then entering&lt;br /&gt;
 123.34&lt;br /&gt;
the student will see&lt;br /&gt;
 The number is: 123.34 Integral part is 123 and fractional part is 34&lt;br /&gt;
The only downside is if no integral part is given, {$1} will be replaced by empty string. There is no way (for now) to erase &amp;quot;Integral part is&amp;quot; under that circumstances.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. Choose it when you need complex regexp features other engines don&#039;t support or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Add possibility to insert match from response (or subpattern match) in the feedback&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75906</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75906"/>
		<updated>2010-09-16T11:14:09Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Precedence and order of evaluation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see Development plans section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd*&#039;&#039;&#039; matches with ab or c followed with zero or more b&#039;s&lt;br /&gt;
#* &#039;&#039;&#039;(ab|cd)*&#039;&#039;&#039; matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. Choose it when you need complex regexp features other engines don&#039;t support or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Add possibility to insert match from response (or subpattern match) in the feedback&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75903</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75903"/>
		<updated>2010-09-16T11:00:26Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Regular expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see Development plans section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
The goal of a regular expressions is a pattern matching in the strings. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or characters set. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions too and it matches with single character &#039;A&#039;. There are several way to define a character set, described below. Special characters, used to write operators,must be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by backslash. Math expressions never had escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but setting pattern for matching you should be able to use any character as operand.&lt;br /&gt;
&lt;br /&gt;
Still, pattern that match only with one character isn&#039;t very useful. So there comes &#039;&#039;&#039;operators&#039;&#039;&#039; that allows us to define expression matching with string of a several characters.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could use those operands in you expressions:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \, other characters lost their special meaning inside character class and shoudn&#039;t be escaped, but if you want to include ^ in the character class it should not be first&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;concatenation&#039;&#039; - so simple &#039;&#039;&#039;binary&#039;&#039;&#039; operator that is doesn&#039;t have any character at all. Still it is an operator and has it&#039;s precedence, which is important if you want to understand where to use brackets. Concatenation allows you to write several operands in sequence:&lt;br /&gt;
#* &#039;&#039;&#039;ab&#039;&#039;&#039; matches with ab&lt;br /&gt;
#* &#039;&#039;&#039;a[0-9]&#039;&#039;&#039; matches with a followed by any digit&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; - &#039;&#039;&#039;binary&#039;&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* empty alternative: &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions)&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; - &#039;&#039;&#039;unary&#039;&#039;&#039; operator that lets you define repetition of a character (or regular expression) used as it&#039;s operands:&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
&#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence over &#039;&#039;&#039;concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence over &#039;&#039;&#039;alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifier over concatenation&#039;&#039; means quantifiers are executed first and without brackets would repeat only single character:&lt;br /&gt;
#* &#039;&#039;&#039;ab*&#039;&#039;&#039; matches with a followed with zero or more b&#039;s&lt;br /&gt;
#* changing this using brackets allows us define a string repetition: &#039;&#039;&#039;(ab)*&#039;&#039;&#039; matches with ab zero or more times&lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means you could define multi-character alternatives without brackets (for single character alternatives use character classes, not alternative operators) but should use brackets when you need to add something to the alternative set:&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; matches with ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; matches with aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; matches with aac or bbc or c - typical use of an empty alternative&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means you should use brackets to repeat an alternative set (not the last character in it):&lt;br /&gt;
#* ab|cd* matches with ab or c followed with zero or more b&#039;s&lt;br /&gt;
#* (ab|cd)* matches with ab or cd, repeated zero or more time in any order, like ababcdabcdcd etc&lt;br /&gt;
#* note that quantifiers repeats alternative, not the definite selection from it, i.e.:&lt;br /&gt;
#*# &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; matches with aa or ab or ba or bb, not just aa or bb&lt;br /&gt;
#*# use &#039;&#039;&#039;a{2}|b{2}&#039;&#039;&#039; to match aa or bb only&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. Choose it when you need complex regexp features other engines don&#039;t support or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Add possibility to insert match from response (or subpattern match) in the feedback&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75902</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75902"/>
		<updated>2010-09-16T10:32:05Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Matching */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see Development plans section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
The regular expressions - as any &#039;&#039;&#039;expressions&#039;&#039;&#039; - are just a bunch of &#039;&#039;&#039;operators&#039;&#039;&#039; with their &#039;&#039;&#039;operands&#039;&#039;&#039;. Don&#039;t worry - you all learned to master arithmetic expressions from chilhood and regular ones are just as easy - if you look on them from the right angle. Learn (or recall) only 4 new words - and you are a master of regexes with very wide possibilities. Don&#039;t find that angle, and regular expressions could forever remain vast menace where only a few steps are sure. Let&#039;s go?&lt;br /&gt;
&lt;br /&gt;
Look at a simple math expression: &#039;&#039;&#039;x+y*2&#039;&#039;&#039;. There are two &#039;&#039;&#039;operators&#039;&#039;&#039; there: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; is &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; is &#039;x&#039; and result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we could found, that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039; there, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You could change order of evaluation using brackets: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply it&#039;s results on the 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators: their &#039;&#039;&#039;arity&#039;&#039;&#039;, which is just the number of operands required. In example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most arithmetic operators are binary, but minus has &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just lego game, where you set a sequence of &#039;&#039;&#039;operators&#039;&#039;&#039; with correct number of &#039;&#039;&#039;operands&#039;&#039;&#039; for each (&#039;&#039;&#039;arity&#039;&#039;&#039;), taking heed of their order of evaluation using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and brackets. Arithmetics expressions are for evaluating numbers. Regular expressions are for finding pattern matches in the strings, so they naturally use another operands and operators - but they are governed by the same rules of precedence and arity.&lt;br /&gt;
&lt;br /&gt;
===Regular expressions===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could construct you expression from these operands:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; let you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; let you define repetition of a character (or regular expression):&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
&#039;&#039;Assertions&#039;&#039; are assertions about some part of the string that doesn&#039;t actually goes into matching text, but affects whether matching occur or not.&lt;br /&gt;
* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. Choose it when you need complex regexp features other engines don&#039;t support or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Add possibility to insert match from response (or subpattern match) in the feedback&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75889</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75889"/>
		<updated>2010-09-15T21:04:17Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Matching */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see Development plans section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Matching===&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student answer (or a whole answer) that suited the regular expression. This part called a &#039;&#039;&#039;match&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question would get use all regular expressions in order to find first full match (full for expression, but not necessary all response - see [[#Anchoring|anchoring]]) and give a grade from it. If there is no full match and engine supports partial matching (see [[#Hinting|hinting]]) than partial match that is shortest to complete would be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine coudn&#039;t tell which one would be shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
You could construct you expression from these operands:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \&lt;br /&gt;
# &#039;&#039;dot meta-character&#039;&#039; &#039;&#039;&#039;.&#039;&#039;&#039; match with any possible character (except newline, but student coudn&#039;t enter it anywhere), you should escape dot &#039;&#039;&#039;\.&#039;&#039;&#039; if you need to match single dot.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; let you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; let you define repetition of a character (or regular expression):&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
# &#039;&#039;asserts&#039;&#039; - these are assertions about some part of the string that doesn&#039;t actually goes into matching text&lt;br /&gt;
#* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
#* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
#* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
#* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring sets restrictions on the matching process:&lt;br /&gt;
* if a regular expression starts with &#039;&#039;&#039;^&#039;&#039;&#039; the match should start at the start of the student&#039;s response;&lt;br /&gt;
* if a regular exhression ends with &#039;&#039;&#039;$&#039;&#039;&#039; the match should ends at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise regular expression match could be contained anywhere inside student response.&lt;br /&gt;
&lt;br /&gt;
If you set &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to yes (default setting), the question would add ^ and $ in each regular expression for you. However, you may prefer to use some non-anchored regexes to catch common errors and give feedback while using manually anchored expression for grading.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. Choose it when you need complex regexp features other engines don&#039;t support or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Add possibility to insert match from response (or subpattern match) in the feedback&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75888</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75888"/>
		<updated>2010-09-15T20:46:22Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Matching engines */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see Development plans section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Matching===&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could construct you expression from these components:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \&lt;br /&gt;
&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; let you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; let you define repetition of a character (or regular expression):&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
# &#039;&#039;asserts&#039;&#039; - these are assertions about some part of the string that doesn&#039;t actually goes into matching text&lt;br /&gt;
#* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
#* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
#* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
#* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
The matching process work like any regular expression matching. I.e. if you don&#039;t set &#039;&#039;&#039;exact matching&#039;&#039;&#039; to yes, than the match could be found &#039;&#039;inside&#039;&#039; student&#039;s response (not necessary the &#039;&#039;whole&#039;&#039; 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.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
Matching engines means different program code that do matching (either by different methods or written by different people). There are no single &#039;best&#039; matching engine - it depends on the features you want to use and regular expressions engine should handle. They have a different degree of stability and offer different features to use.&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
It is based on native PHP preg functions (which is in turn based on the PCRE library). It is supporting 100% perl-compatible regular expression features, been very stable and thoroughly tested. Sadly, PHP functions doesn&#039;t support partial matching (while PCRE could), so (unless we storm PHP developers to add support for partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039; there. Choose it when you need complex regexp features other engines don&#039;t support or better performace.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
This is a custom PHP code using DFA matching algorithm. It is heavily unit-tested, but considered beta-quality for now. Not all PHP operands and operators are supported, and for some (more exotic) ones support could still differs from standard (especially for non-latin characters). On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands (there would be more):&lt;br /&gt;
* single characters&lt;br /&gt;
* escaped special characters&lt;br /&gt;
* character classes, including ranges and negative classes&lt;br /&gt;
* escape sequences  \w\W\s\S\t\d\D (WARNING: they probably will not work for Unicode (non-latin) characters for now)&lt;br /&gt;
* octal and hexadecimal character codes preceeded by \o and \x&lt;br /&gt;
* meta-character . (any character)&lt;br /&gt;
&lt;br /&gt;
Currently supported operators (there would be more):&lt;br /&gt;
* concatenation&lt;br /&gt;
* alternative |&lt;br /&gt;
* quantifiers * + ? {2,3} {2,} {,2} {2}&lt;br /&gt;
* positive lookahead assertions&lt;br /&gt;
* changing operator precedence (  )  (without subpattern capturing) or (?:  )&lt;br /&gt;
&lt;br /&gt;
Features that couldn&#039;t be supported by DFA matching at all:&lt;br /&gt;
* subpattern capturing&lt;br /&gt;
* backreferences&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Add possibility to insert match from response (or subpattern match) in the feedback&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75887</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75887"/>
		<updated>2010-09-15T20:25:17Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Hinting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see Development plans section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Matching===&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could construct you expression from these components:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \&lt;br /&gt;
&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; let you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; let you define repetition of a character (or regular expression):&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
# &#039;&#039;asserts&#039;&#039; - these are assertions about some part of the string that doesn&#039;t actually goes into matching text&lt;br /&gt;
#* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
#* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
#* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
#* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
The matching process work like any regular expression matching. I.e. if you don&#039;t set &#039;&#039;&#039;exact matching&#039;&#039;&#039; to yes, than the match could be found &#039;&#039;inside&#039;&#039; student&#039;s response (not necessary the &#039;&#039;whole&#039;&#039; 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.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Some matching engines could support hinting (not easy thing to do on the PHP at all) in adaptive mode.&lt;br /&gt;
&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. When a student enters a partially correct answer, partial matching could find that response starts matching and on some character broke it. Consider you enter expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and student answered&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that partial match is&lt;br /&gt;
  are blue, &lt;br /&gt;
Remember, the regular expresion in unanchored so the match shouldn&#039;t start with the start of the student response. While using just partial matching the student will be shown correct and incorrect parts:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
When hinting is available, student will have &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receive a hint with one next correct character, highlighted by background coloring:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;they &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, &amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;w&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;vhite and red&amp;lt;/span&amp;gt;&lt;br /&gt;
You should typically set hint &#039;&#039;&#039;penalty&#039;&#039;&#039; more than usual question &#039;&#039;&#039;penalty&#039;&#039;&#039;, because they are applied separately: usual penalty for an attempt without hinting, while hint penalty for an attempt with hinting.&lt;br /&gt;
Preg question doesn&#039;t add hint character to the student&#039;s response (like regex question do it), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he want to add hinted character to the his response (and some more possibly);&lt;br /&gt;
# it slightly facilitates thinking about hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not a desirable behavour usually.&lt;br /&gt;
When possible (if question engine supports it), hinting choosing a character that leads to shortest path to complete a match. Consider this response to the previous regular expression:&lt;br /&gt;
  &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are blue, white&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;text-decoration:line-through; color:#FF0000;&amp;quot;&amp;gt;; red&amp;lt;/span&amp;gt;&lt;br /&gt;
There are two possible hint characters: &#039;,&#039; or &#039; &#039;. The question will choose &#039;,&#039; since it leads to the shortest path to complete a match, while &#039; &#039; leads to a path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you add an expression for the students with bad memory:&lt;br /&gt;
  &#039;&#039;&#039;are white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
with 60% grade and feedback about forgetting &#039;&#039;blue&#039;&#039;. You may not want hinting to lead student to the response&lt;br /&gt;
   are white, red&lt;br /&gt;
if he entered&lt;br /&gt;
   are white, oh I forgot other colors.&lt;br /&gt;
&#039;&#039;&#039;Hint grade border&#039;&#039;&#039; controls this. Only regular expressions with grade greater or equal than hint grade border would be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression would be used to hinting, if you set it to 0,5 regular expressions with 50%-100% grades would be used and 0%-49% would not. Regular expressions not used for hinting works only when they have a full match in the student response.&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata====&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Add possibility to insert match from response (or subpattern match) in the feedback&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75886</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75886"/>
		<updated>2010-09-15T19:50:55Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Matching */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see Development plans section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Matching===&lt;br /&gt;
You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to the question without modifiers or enclosing characters (modifiers would be added for you by question - &#039;&#039;&#039;u&#039;&#039;&#039; added always and &#039;&#039;&#039;i&#039;&#039;&#039; 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 &#039;&#039;&#039;correct answer&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could construct you expression from these components:&lt;br /&gt;
# &#039;&#039;simple characters&#039;&#039; match with themselves&lt;br /&gt;
# &#039;&#039;escaped special characters&#039;&#039; if you need to use character with special meaning (like |, * or bracket) just as usual character to match you should preceed it by backslash: &#039;&#039;&#039;a\*&#039;&#039;&#039; matches with a* (while &#039;&#039;&#039;a*&#039;&#039;&#039; matches with a zero or more times), backslash is a special character too and should be escaped &#039;&#039;&#039;\\&#039;&#039;&#039; matches with \&lt;br /&gt;
# &#039;&#039;character classes&#039;&#039; you could specify a number of possible characters in one place in square brackets:&lt;br /&gt;
#* &#039;&#039;&#039;[ab,!]&#039;&#039;&#039; matches with a or b or , or !&lt;br /&gt;
#* &#039;&#039;ranges&#039;&#039;: &#039;&#039;&#039;[a-szC-F0-9]&#039;&#039;&#039; you could specify ranges for letters and digits in character classes, mixing them with single characters&lt;br /&gt;
#* &#039;&#039;negative character classes&#039;&#039; starts with ^ &#039;&#039;&#039;[^ab]&#039;&#039;&#039; means any characters except a and b&lt;br /&gt;
#* &#039;&#039;escaping inside character classes&#039;&#039;: &#039;&#039;&#039;[\-\]\\]&#039;&#039;&#039; match with - or ] or \&lt;br /&gt;
&lt;br /&gt;
Most common regular expression operators used (could anyone help expand descriptions and examples please?):&lt;br /&gt;
# &#039;&#039;alternative&#039;&#039; let you define a set of alternatives:&lt;br /&gt;
#* &#039;&#039;&#039;a|b&#039;&#039;&#039; mean a or b&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|de&#039;&#039;&#039; mean ab or cd or de&lt;br /&gt;
#* &#039;&#039;&#039;ab|cd|&#039;&#039;&#039; mean ab or cd or emptiness (useful as a part more complex expressions&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb)c&#039;&#039;&#039; mean aac or bbc - use brackets to outline alternative set&lt;br /&gt;
#* &#039;&#039;&#039;(aa|bb|)c&#039;&#039;&#039; mean aac or bbc or c - typical use of emptiness&lt;br /&gt;
# &#039;&#039;quantifiers&#039;&#039; let you define repetition of a character (or regular expression):&lt;br /&gt;
#* &#039;&#039;&#039;x*&#039;&#039;&#039; mean x zero or more times&lt;br /&gt;
#* &#039;&#039;&#039;x+&#039;&#039;&#039; mean x one or more times&lt;br /&gt;
#* &#039;&#039;&#039;x?&#039;&#039;&#039; mean x zero or one times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,4}&#039;&#039;&#039; mean x from 2 to 4 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2,}&#039;&#039;&#039; mean x two or more times&lt;br /&gt;
#* &#039;&#039;&#039;x{,2}&#039;&#039;&#039; mean x from 0 to 2 times&lt;br /&gt;
#* &#039;&#039;&#039;x{2}&#039;&#039;&#039; mean x exactly 2 times&lt;br /&gt;
#* &#039;&#039;&#039;(ab)*&#039;&#039;&#039; mean ab zero or more times, i.e. if you want to use quantifier on more than one character, you should use brackets&lt;br /&gt;
#* &#039;&#039;&#039;(a|b){2}&#039;&#039;&#039; mean aa or ab or ba or bb, i.e. it is repeated alternative, not selection one alternative and repeating it&lt;br /&gt;
# &#039;&#039;asserts&#039;&#039; - these are assertions about some part of the string that doesn&#039;t actually goes into matching text&lt;br /&gt;
#* &#039;&#039;positive lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?=b)&#039;&#039;&#039; matches with any number of a ending with b without including b in the match&lt;br /&gt;
#* &#039;&#039;negative lookahead assert&#039;&#039; &#039;&#039;&#039;a+(?!b)&#039;&#039;&#039; matches with any number of a that is not followed by b&lt;br /&gt;
#* &#039;&#039;positive lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;=b)a+&#039;&#039;&#039; matches with any number of a preceeded by b&lt;br /&gt;
#* &#039;&#039;negative lookbehind assert&#039;&#039; &#039;&#039;&#039;(?&amp;lt;!b)a+&#039;&#039;&#039; matches with any number of a that is not preceeded by b&lt;br /&gt;
&lt;br /&gt;
The matching process work like any regular expression matching. I.e. if you don&#039;t set &#039;&#039;&#039;exact matching&#039;&#039;&#039; to yes, than the match could be found &#039;&#039;inside&#039;&#039; student&#039;s response (not necessary the &#039;&#039;whole&#039;&#039; 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.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata====&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Add possibility to insert match from response (or subpattern match) in the feedback&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75885</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Preg_question_type&amp;diff=75885"/>
		<updated>2010-09-15T19:04:35Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: 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...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# idea, design, question type code - Oleg Sychev;&lt;br /&gt;
# parsing regular expression, DFA regular expression matching engine - Dmitriy Kolesov.&lt;br /&gt;
We would gladly accept testers and contributors (see Development plans section) - there is still more to be done than we have time.&lt;br /&gt;
&lt;br /&gt;
===Matching===&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
&lt;br /&gt;
===Matching engines===&lt;br /&gt;
&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata====&lt;br /&gt;
&lt;br /&gt;
===Development plans===&lt;br /&gt;
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.&lt;br /&gt;
* Update DFA matching engine to support all operations DFA algorithm could&lt;br /&gt;
* Develop NFA and backtracking matching engines&lt;br /&gt;
* Add possibility to insert match from response (or subpattern match) in the feedback&lt;br /&gt;
* Add automatic generation of shortest possible correct answer in user-readable form&lt;br /&gt;
* Add negative matching answers (AKA &amp;quot;missing words&amp;quot; from Joseph Rezeau question) - depends on developing and checking in extra_answer_fields() code&lt;br /&gt;
* Add generation of &#039;description&#039; for regular expression to facilitate it&#039;s editing&lt;br /&gt;
* Develop more help and examples for the people that don&#039;t know much about regular expressions.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:VSTU_projects&amp;diff=75657</id>
		<title>Development:VSTU projects</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:VSTU_projects&amp;diff=75657"/>
		<updated>2010-09-12T20:56:29Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Libraries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Lead: Sychev Oleg Aleksandrovich==&lt;br /&gt;
Senior Lecturer of POAS (Software Engineering) department&lt;br /&gt;
&lt;br /&gt;
===Core patches===&lt;br /&gt;
# [[Development:Categories editing interface improvment|Improvment of editing interface for category list]]. Moving category anywhere should require only one page reload. Developers: Sychev Oleg, Shkarupa Alex. Status: waiting reviewing!&lt;br /&gt;
# [[Development:Javascript-interface for repeat_elements function|Javascript-interface for repeat_elements function]] - adding new blanks to the form shoudn&#039;t require page reloads.&lt;br /&gt;
# [[Development:Forum thread subscription|Forum thread subscription]] - a long standing request to be able to subscript only to the thread, not whole forum.&lt;br /&gt;
&lt;br /&gt;
===Activity modules===&lt;br /&gt;
# [[Development:Assignment development|New assignment-replacement module]] with support for individual tasks (with custom fields), several grading criterions, automatic graders etc. Developers: Sychev Oleg, Erofeev Anatolius.&lt;br /&gt;
# [[Development:Subcourse module improvments|Subcourse module improvments]] - improve Subcourse module to get rid of buggy Metacourses and make it actually useful.&lt;br /&gt;
&lt;br /&gt;
===Question types===&lt;br /&gt;
# [[Preg question type]] - regular expression question type, developed on more solid algorithmic basis than regex one. Developers: Sychev Oleg, Kolesov Dmitry.&lt;br /&gt;
# [[Development:Auto-feedback shortanswer question|Auto-feedback shortanswer question]] shortanswer question which could be able to detect typos and misplaced, missing or extra words and give appropriate feedback.&lt;br /&gt;
&lt;br /&gt;
===Blocks===&lt;br /&gt;
# [[Development:Linked activities|Linked activities]] to allow editing properties of multiple activities at once.&lt;br /&gt;
# [[Auto role assignment block]] is useful to temporary restrict (or enhance) user abilities while doing something.&lt;br /&gt;
# [[Under suprevision block]] is much better than IP filter+time control if you want students to be able to do something only under teacher supervision.&lt;br /&gt;
&lt;br /&gt;
===Libraries===&lt;br /&gt;
# [[Development:Auto-backup library|Auto-backup library]] - adding very small information to install.xml files it is possible to automate many tedious tasks of programming backup/restore for the plugins.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:VSTU_projects&amp;diff=75656</id>
		<title>Development:VSTU projects</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:VSTU_projects&amp;diff=75656"/>
		<updated>2010-09-12T20:54:38Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Created a page with a list of our projects&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Lead: Sychev Oleg Aleksandrovich==&lt;br /&gt;
Senior Lecturer of POAS (Software Engineering) department&lt;br /&gt;
&lt;br /&gt;
===Core patches===&lt;br /&gt;
# [[Development:Categories editing interface improvment|Improvment of editing interface for category list]]. Moving category anywhere should require only one page reload. Developers: Sychev Oleg, Shkarupa Alex. Status: waiting reviewing!&lt;br /&gt;
# [[Development:Javascript-interface for repeat_elements function|Javascript-interface for repeat_elements function]] - adding new blanks to the form shoudn&#039;t require page reloads.&lt;br /&gt;
# [[Development:Forum thread subscription|Forum thread subscription]] - a long standing request to be able to subscript only to the thread, not whole forum.&lt;br /&gt;
&lt;br /&gt;
===Activity modules===&lt;br /&gt;
# [[Development:Assignment development|New assignment-replacement module]] with support for individual tasks (with custom fields), several grading criterions, automatic graders etc. Developers: Sychev Oleg, Erofeev Anatolius.&lt;br /&gt;
# [[Development:Subcourse module improvments|Subcourse module improvments]] - improve Subcourse module to get rid of buggy Metacourses and make it actually useful.&lt;br /&gt;
&lt;br /&gt;
===Question types===&lt;br /&gt;
# [[Preg question type]] - regular expression question type, developed on more solid algorithmic basis than regex one. Developers: Sychev Oleg, Kolesov Dmitry.&lt;br /&gt;
# [[Development:Auto-feedback shortanswer question|Auto-feedback shortanswer question]] shortanswer question which could be able to detect typos and misplaced, missing or extra words and give appropriate feedback.&lt;br /&gt;
&lt;br /&gt;
===Blocks===&lt;br /&gt;
# [[Development:Linked activities|Linked activities]] to allow editing properties of multiple activities at once.&lt;br /&gt;
# [[Auto role assignment block]] is useful to temporary restrict (or enhance) user abilities while doing something.&lt;br /&gt;
# [[Under suprevision block]] is much better than IP filter+time control if you want students to be able to do something only under teacher supervision.&lt;br /&gt;
&lt;br /&gt;
===Libraries===&lt;br /&gt;
# [[Development:Auto-backup library]] - adding very small information to install.xml files it is possible to automate many tedious tasks of programming backup/restore for the plugins.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:Developer_notes&amp;diff=75655</id>
		<title>Development:Developer notes</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:Developer_notes&amp;diff=75655"/>
		<updated>2010-09-12T20:46:16Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Modules and other recognised plugin types */ - separated projects developed by VSTU&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p class=&amp;quot;note&amp;quot;&amp;gt;&#039;&#039;&#039;Note:&#039;&#039;&#039; New developer documentation pages should be added to the &#039;&#039;Development namespace&#039;&#039; by typing &amp;lt;code&amp;gt;Development:&amp;lt;/code&amp;gt; before the new page name i.e. &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;[[Development:New page name]]&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This area is for developers to work on various bits of code and documentation as necessary. Once material has matured it should be linked to from the main [[Development:Developer documentation|Developer documentation]] page.&lt;br /&gt;
&lt;br /&gt;
==Core code==&lt;br /&gt;
&lt;br /&gt;
*[[Other lang issues|Language issues]] &lt;br /&gt;
*[[Datalib Notes]]&lt;br /&gt;
*[[Application/session variables]]&lt;br /&gt;
*[[Filters schema]]&lt;br /&gt;
*[[Filterall support]]&lt;br /&gt;
*[[Groups documentation for module developers]]&lt;br /&gt;
*[[Development:Plan to improve ability to delegate administrator tasks to course category level|Plan to improve ability to delegate administrator tasks to course category level]]&lt;br /&gt;
*[[Development:Stealth mode and nested activities|Stealth mode and nested activities]]&lt;br /&gt;
*[[Development:Enrolment_plugins_2.0|How enrolments should work in Moodle 2.0]]&lt;br /&gt;
*[[Development:Redesigning the override roles page|Redesigning the override roles page]]&lt;br /&gt;
&lt;br /&gt;
===Gradebook===&lt;br /&gt;
&lt;br /&gt;
*[[Development:Gradebook_improvements|Gradebook improvements]] - ideas from Petr.&lt;br /&gt;
*[[Development:Tim&#039;s_Gradebook_thoughts|Tim&#039;s Gradebook thoughts]]&lt;br /&gt;
&lt;br /&gt;
==Modules and other recognised plugin types==&lt;br /&gt;
&lt;br /&gt;
===Lesson===&lt;br /&gt;
&lt;br /&gt;
*[[Adding_question_types_to_lesson|Adding question types to lesson module]]&lt;br /&gt;
*[[Development:New lesson navigation | New lesson navigation]]&lt;br /&gt;
*[[Development:Adding blocks to lesson | Adding blocks to lesson]]&lt;br /&gt;
&lt;br /&gt;
===Quiz &amp;amp; question types===&lt;br /&gt;
&lt;br /&gt;
* [[Development:File storage conversion Quiz and Questions|File storage conversion Quiz and Questions]] - notes for MDL-16094.&lt;br /&gt;
* [[Development:How the quiz navigation should work in Moodle 2.0|How the quiz navigation should work in Moodle 2.0]] - MDL-20276.&lt;br /&gt;
* [[Development:Moodle 2.0 question bank improvements|Moodle 2.0 question bank improvements]] - including question tagging and improved searchability.&lt;br /&gt;
* [[Development:Question_Engine_2|Proposal to change the Moodle Question Engine]] This supersedes &lt;br /&gt;
** [[Development:Plans for adaptive mode|Plans for adaptive mode]]&lt;br /&gt;
** [[Development:Implementation of true negative marks in MC-type questions|Implementation of true negative marks in MC-type questions]]&lt;br /&gt;
* [[Calculated question development]]&lt;br /&gt;
* [[Numerical question units and intervals]]&lt;br /&gt;
* [[Development:Future question bank/sharing/versioning requirements|Future question bank/sharing/versioning requirements]]&lt;br /&gt;
* [[Development:States of a quiz attempt]]&lt;br /&gt;
* [[Development:Optional checkbox &amp;quot;This is all my own work&amp;quot; before submitting a quiz]]&lt;br /&gt;
&lt;br /&gt;
===Other activity modules===&lt;br /&gt;
&lt;br /&gt;
*[[Forum development|Forum functional upgrade]]&lt;br /&gt;
*[[Wiki development|Wiki module development]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Gradebook Plugins===&lt;br /&gt;
&lt;br /&gt;
*[[Development:External Database Plugin]]&lt;br /&gt;
&lt;br /&gt;
===Other===&lt;br /&gt;
&lt;br /&gt;
*[[Blogs and forums|Blogs, forums and the nature of discussion]]&lt;br /&gt;
*[[Conditional activities]]&lt;br /&gt;
*[[Improved Payment Plugin]]&lt;br /&gt;
*[[Development:Moodle-specific customisations to the HTML editor]]&lt;br /&gt;
*[[Development:Moodle Automatic Regression Test and Inspection Nit-picker]]&lt;br /&gt;
*[[Development: VSTU projects|Projects developed in Volgograd State Technical University]] becomes to many to simple list them on that page.&lt;br /&gt;
&lt;br /&gt;
== Markup &amp;amp; Theming ==&lt;br /&gt;
* [[Moodle CSS System]], idea from forum discussion on [http://moodle.org/mod/forum/discuss.php?d=126009 Object Oriented CSS]&lt;br /&gt;
&lt;br /&gt;
==Other==&lt;br /&gt;
&lt;br /&gt;
*[[MoodleDocs development]]&lt;br /&gt;
*[[Usability]]&lt;br /&gt;
*[[Document Management API]]&lt;br /&gt;
*[[wikindx|Possible integration of WIKINDX with Moodle]]&lt;br /&gt;
*[[Development:Snapshot - flat HTML export of complete course]]&lt;br /&gt;
*[[Development:Offline Moodle | Development:Offline Moodle - A Moodle that runs on a mobile device with the internet]]&lt;br /&gt;
*[[Development:A standard set of fixtures for unit tests?|A standard set of fixtures for unit tests?]]&lt;br /&gt;
&lt;br /&gt;
==Archive: past proposals==&lt;br /&gt;
&lt;br /&gt;
===Things that got implemented===&lt;br /&gt;
&lt;br /&gt;
====Core code====&lt;br /&gt;
&lt;br /&gt;
*[[Development:Ajax user selector|Ajax user selector]]&lt;br /&gt;
*[[Roles]] - in Moodle 1.7&lt;br /&gt;
*[[Moodle forms library]] - in Moodle 1.8&lt;br /&gt;
**[[Martin form notes]]&lt;br /&gt;
&lt;br /&gt;
====Quiz &amp;amp; question types====&lt;br /&gt;
&lt;br /&gt;
*[[Development:Quiz UI redesign | Quiz UI redesign]]&lt;br /&gt;
*[[Development:Administration page for question types|Administration page for question types]]&lt;br /&gt;
*[[Development:Plan_to_Improve_Flexibility_of_Question_Category_Sharing_and_Permissions|Plans for enhancing the question bank]] - in Moodle 1.9&lt;br /&gt;
*[[Development:Email notification when a quiz is submitted|Email notification when a quiz is submitted]] - in Moodle 1.9&lt;br /&gt;
*[[Development:Plans for enhancing import/export in questiontype plugins]] - in Moodle 1.9&lt;br /&gt;
*[[Development:quiz_navigation | Quiz navigation]]&lt;br /&gt;
*[[Development:Open_protocol_for_accessing_question_engines|Open protocol for accessing question engines]]&lt;br /&gt;
*[[Development:Quiz report enhancements|Quiz report enhancements]]&lt;br /&gt;
*[[Development:Flagging questions during a quiz attempt|Flagging questions during a quiz attempt]]&lt;br /&gt;
&lt;br /&gt;
====Other====&lt;br /&gt;
&lt;br /&gt;
*[[Quiz and quesions community testing day]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:Developer_notes&amp;diff=75654</id>
		<title>Development:Developer notes</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:Developer_notes&amp;diff=75654"/>
		<updated>2010-09-12T20:31:47Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Other */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p class=&amp;quot;note&amp;quot;&amp;gt;&#039;&#039;&#039;Note:&#039;&#039;&#039; New developer documentation pages should be added to the &#039;&#039;Development namespace&#039;&#039; by typing &amp;lt;code&amp;gt;Development:&amp;lt;/code&amp;gt; before the new page name i.e. &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;[[Development:New page name]]&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This area is for developers to work on various bits of code and documentation as necessary. Once material has matured it should be linked to from the main [[Development:Developer documentation|Developer documentation]] page.&lt;br /&gt;
&lt;br /&gt;
==Core code==&lt;br /&gt;
&lt;br /&gt;
*[[Other lang issues|Language issues]] &lt;br /&gt;
*[[Datalib Notes]]&lt;br /&gt;
*[[Application/session variables]]&lt;br /&gt;
*[[Filters schema]]&lt;br /&gt;
*[[Filterall support]]&lt;br /&gt;
*[[Groups documentation for module developers]]&lt;br /&gt;
*[[Development:Plan to improve ability to delegate administrator tasks to course category level|Plan to improve ability to delegate administrator tasks to course category level]]&lt;br /&gt;
*[[Development:Stealth mode and nested activities|Stealth mode and nested activities]]&lt;br /&gt;
*[[Development:Enrolment_plugins_2.0|How enrolments should work in Moodle 2.0]]&lt;br /&gt;
*[[Development:Redesigning the override roles page|Redesigning the override roles page]]&lt;br /&gt;
&lt;br /&gt;
===Gradebook===&lt;br /&gt;
&lt;br /&gt;
*[[Development:Gradebook_improvements|Gradebook improvements]] - ideas from Petr.&lt;br /&gt;
*[[Development:Tim&#039;s_Gradebook_thoughts|Tim&#039;s Gradebook thoughts]]&lt;br /&gt;
&lt;br /&gt;
==Modules and other recognised plugin types==&lt;br /&gt;
&lt;br /&gt;
===Lesson===&lt;br /&gt;
&lt;br /&gt;
*[[Adding_question_types_to_lesson|Adding question types to lesson module]]&lt;br /&gt;
*[[Development:New lesson navigation | New lesson navigation]]&lt;br /&gt;
*[[Development:Adding blocks to lesson | Adding blocks to lesson]]&lt;br /&gt;
&lt;br /&gt;
===Quiz &amp;amp; question types===&lt;br /&gt;
&lt;br /&gt;
* [[Development:File storage conversion Quiz and Questions|File storage conversion Quiz and Questions]] - notes for MDL-16094.&lt;br /&gt;
* [[Development:How the quiz navigation should work in Moodle 2.0|How the quiz navigation should work in Moodle 2.0]] - MDL-20276.&lt;br /&gt;
* [[Development:Moodle 2.0 question bank improvements|Moodle 2.0 question bank improvements]] - including question tagging and improved searchability.&lt;br /&gt;
* [[Development:Question_Engine_2|Proposal to change the Moodle Question Engine]] This supersedes &lt;br /&gt;
** [[Development:Plans for adaptive mode|Plans for adaptive mode]]&lt;br /&gt;
** [[Development:Implementation of true negative marks in MC-type questions|Implementation of true negative marks in MC-type questions]]&lt;br /&gt;
* [[Calculated question development]]&lt;br /&gt;
* [[Numerical question units and intervals]]&lt;br /&gt;
* [[Development:Categories editing interface improvment|Improvment of editing interface for category list]]&lt;br /&gt;
* [[Development:Future question bank/sharing/versioning requirements|Future question bank/sharing/versioning requirements]]&lt;br /&gt;
* [[Development:States of a quiz attempt]]&lt;br /&gt;
* [[Development:Optional checkbox &amp;quot;This is all my own work&amp;quot; before submitting a quiz]]&lt;br /&gt;
&lt;br /&gt;
===Other activity modules===&lt;br /&gt;
&lt;br /&gt;
*[[Forum development|Forum functional upgrade]]&lt;br /&gt;
*[[Wiki development|Wiki module development]]&lt;br /&gt;
*[[Development:Assignment development|New assignment-replacement module]]&lt;br /&gt;
&lt;br /&gt;
===Gradebook Plugins===&lt;br /&gt;
&lt;br /&gt;
*[[Development:External Database Plugin]]&lt;br /&gt;
&lt;br /&gt;
===Other===&lt;br /&gt;
&lt;br /&gt;
*[[Blogs and forums|Blogs, forums and the nature of discussion]]&lt;br /&gt;
*[[Conditional activities]]&lt;br /&gt;
*[[Improved Payment Plugin]]&lt;br /&gt;
*[[Development:Moodle-specific customisations to the HTML editor]]&lt;br /&gt;
*[[Development:Linked activities|Linked activities]]&lt;br /&gt;
*[[Development:Moodle Automatic Regression Test and Inspection Nit-picker]]&lt;br /&gt;
*[[Development: VSTU projects|Projects developed in Volgograd State Technical University]] becomes to many to simple list them on that page&lt;br /&gt;
&lt;br /&gt;
== Markup &amp;amp; Theming ==&lt;br /&gt;
* [[Moodle CSS System]], idea from forum discussion on [http://moodle.org/mod/forum/discuss.php?d=126009 Object Oriented CSS]&lt;br /&gt;
&lt;br /&gt;
==Other==&lt;br /&gt;
&lt;br /&gt;
*[[MoodleDocs development]]&lt;br /&gt;
*[[Usability]]&lt;br /&gt;
*[[Document Management API]]&lt;br /&gt;
*[[wikindx|Possible integration of WIKINDX with Moodle]]&lt;br /&gt;
*[[Development:Snapshot - flat HTML export of complete course]]&lt;br /&gt;
*[[Development:Offline Moodle | Development:Offline Moodle - A Moodle that runs on a mobile device with the internet]]&lt;br /&gt;
*[[Development:A standard set of fixtures for unit tests?|A standard set of fixtures for unit tests?]]&lt;br /&gt;
&lt;br /&gt;
==Archive: past proposals==&lt;br /&gt;
&lt;br /&gt;
===Things that got implemented===&lt;br /&gt;
&lt;br /&gt;
====Core code====&lt;br /&gt;
&lt;br /&gt;
*[[Development:Ajax user selector|Ajax user selector]]&lt;br /&gt;
*[[Roles]] - in Moodle 1.7&lt;br /&gt;
*[[Moodle forms library]] - in Moodle 1.8&lt;br /&gt;
**[[Martin form notes]]&lt;br /&gt;
&lt;br /&gt;
====Quiz &amp;amp; question types====&lt;br /&gt;
&lt;br /&gt;
*[[Development:Quiz UI redesign | Quiz UI redesign]]&lt;br /&gt;
*[[Development:Administration page for question types|Administration page for question types]]&lt;br /&gt;
*[[Development:Plan_to_Improve_Flexibility_of_Question_Category_Sharing_and_Permissions|Plans for enhancing the question bank]] - in Moodle 1.9&lt;br /&gt;
*[[Development:Email notification when a quiz is submitted|Email notification when a quiz is submitted]] - in Moodle 1.9&lt;br /&gt;
*[[Development:Plans for enhancing import/export in questiontype plugins]] - in Moodle 1.9&lt;br /&gt;
*[[Development:quiz_navigation | Quiz navigation]]&lt;br /&gt;
*[[Development:Open_protocol_for_accessing_question_engines|Open protocol for accessing question engines]]&lt;br /&gt;
*[[Development:Quiz report enhancements|Quiz report enhancements]]&lt;br /&gt;
*[[Development:Flagging questions during a quiz attempt|Flagging questions during a quiz attempt]]&lt;br /&gt;
&lt;br /&gt;
====Other====&lt;br /&gt;
&lt;br /&gt;
*[[Quiz and quesions community testing day]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:Auto-feedback_shortanswer_question&amp;diff=66549</id>
		<title>Development:Auto-feedback shortanswer question</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:Auto-feedback_shortanswer_question&amp;diff=66549"/>
		<updated>2009-12-17T09:31:19Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Examples of error reporting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Goal==&lt;br /&gt;
Develop a question type, that was similar to shortanswer (except that no initial wildcard support is planned, thought it can be added later) which will be able to analyze student answer and automatically generate feedback messages describing errors, which could be useful to the student without revealing correct answer (if necessary).&lt;br /&gt;
&lt;br /&gt;
Such question is very useful in adaptive mode (and in non-adaptive mode too for courses with minimal teacher involvement) whenever you try to teach some language (eithern natural or artificial). If someone knows about works or papers that trying to do similar things please let me know there or on page comments.&lt;br /&gt;
&lt;br /&gt;
Question still will be based (at least from the beginning) on entering a number of correct answers.&lt;br /&gt;
&lt;br /&gt;
Question will be able to handle correct answer with several identical words, but that is discouraged (unless absolutely necessary) beacuse it will lower accuracy of feedback.&lt;br /&gt;
&lt;br /&gt;
==External specification draft==&lt;br /&gt;
&lt;br /&gt;
===Creating a question===&lt;br /&gt;
Teacher input one or several correct answers (with possible feedback describing particular answer). These answers are tokenised (this process will be called &#039;&#039;&#039;scanning&#039;&#039;&#039; later) to break them down to smaller parts (&#039;&#039;&#039;lexems&#039;&#039;&#039;): words, numbers, separators, operators etc. &lt;br /&gt;
:Since I couldn&#039;t start this work from writing universal scanner similar to the flex etc, it would be good to have pluginable scanners for the first time (e.g. natural language scanner, C++ scanner etc).&lt;br /&gt;
&lt;br /&gt;
Teacher than asked for a names for all lexems, that could be shown to the user without revealing correct answer. Examples of tokenised answer with named lexems: &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function header in C language&#039;&#039;&#039;:   void function(int abc, char def) &lt;br /&gt;
; void : type of returned value&lt;br /&gt;
; function : function name&lt;br /&gt;
; ( : bracket (or opening bracket for function arguments)&lt;br /&gt;
; int : first argument type&lt;br /&gt;
; abc : first argument name&lt;br /&gt;
; , : argument list separator&lt;br /&gt;
; char : second argument type&lt;br /&gt;
; def : second argument name&lt;br /&gt;
; ) : bracket (or closing bracket for function arguments)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;English language sentence&#039;&#039;&#039;:  The cat ate the mouse.&lt;br /&gt;
; The : definite article&lt;br /&gt;
; cat : subject&lt;br /&gt;
; ate : verb&lt;br /&gt;
; the : definite article&lt;br /&gt;
; mouse : complement&lt;br /&gt;
; . : sentence ending point&lt;br /&gt;
&lt;br /&gt;
:Could anyone write a natural language example please? I don&#039;t know well grammatical categories of English language in English to do so.&lt;br /&gt;
&lt;br /&gt;
There will be probably some settings to determine how a grade should be calculated in case of errors, but they could be defined later.&lt;br /&gt;
&lt;br /&gt;
===Types of errors to report===&lt;br /&gt;
# misspelled word (number, another complex lexem)&lt;br /&gt;
# missing separator&lt;br /&gt;
# invalid separator&lt;br /&gt;
# misplaced word (number, etc)&lt;br /&gt;
# missing word (number, etc)&lt;br /&gt;
# unwanted word (number, separator, etc)&lt;br /&gt;
&lt;br /&gt;
===Examples of error reporting===&lt;br /&gt;
&#039;&#039;&#039;Correct answer&#039;&#039;&#039;: void function(int abc, char def)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Student&#039;s response&#039;&#039;&#039;: funcion (int, chardef)void&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Feedback from the question&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* please check function name, you are probably misspelled it&lt;br /&gt;
* first argument name is missing&lt;br /&gt;
* missing separator between second argument type and second argument name&lt;br /&gt;
* type of returned value is probably misplaced&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Correct answer&#039;&#039;&#039;: The cat ate the mouse.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Student&#039;s response&#039;&#039;&#039;: The mouse eat a cat.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Feedback from the question&#039;&#039;&#039;:&lt;br /&gt;
* complement misplaced&lt;br /&gt;
* verb is in wrong tense&lt;br /&gt;
* wrong article used before the subject&lt;br /&gt;
* subject misplaced&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:Auto-feedback_shortanswer_question&amp;diff=66548</id>
		<title>Development:Auto-feedback shortanswer question</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:Auto-feedback_shortanswer_question&amp;diff=66548"/>
		<updated>2009-12-17T09:28:26Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Creating a question */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Goal==&lt;br /&gt;
Develop a question type, that was similar to shortanswer (except that no initial wildcard support is planned, thought it can be added later) which will be able to analyze student answer and automatically generate feedback messages describing errors, which could be useful to the student without revealing correct answer (if necessary).&lt;br /&gt;
&lt;br /&gt;
Such question is very useful in adaptive mode (and in non-adaptive mode too for courses with minimal teacher involvement) whenever you try to teach some language (eithern natural or artificial). If someone knows about works or papers that trying to do similar things please let me know there or on page comments.&lt;br /&gt;
&lt;br /&gt;
Question still will be based (at least from the beginning) on entering a number of correct answers.&lt;br /&gt;
&lt;br /&gt;
Question will be able to handle correct answer with several identical words, but that is discouraged (unless absolutely necessary) beacuse it will lower accuracy of feedback.&lt;br /&gt;
&lt;br /&gt;
==External specification draft==&lt;br /&gt;
&lt;br /&gt;
===Creating a question===&lt;br /&gt;
Teacher input one or several correct answers (with possible feedback describing particular answer). These answers are tokenised (this process will be called &#039;&#039;&#039;scanning&#039;&#039;&#039; later) to break them down to smaller parts (&#039;&#039;&#039;lexems&#039;&#039;&#039;): words, numbers, separators, operators etc. &lt;br /&gt;
:Since I couldn&#039;t start this work from writing universal scanner similar to the flex etc, it would be good to have pluginable scanners for the first time (e.g. natural language scanner, C++ scanner etc).&lt;br /&gt;
&lt;br /&gt;
Teacher than asked for a names for all lexems, that could be shown to the user without revealing correct answer. Examples of tokenised answer with named lexems: &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function header in C language&#039;&#039;&#039;:   void function(int abc, char def) &lt;br /&gt;
; void : type of returned value&lt;br /&gt;
; function : function name&lt;br /&gt;
; ( : bracket (or opening bracket for function arguments)&lt;br /&gt;
; int : first argument type&lt;br /&gt;
; abc : first argument name&lt;br /&gt;
; , : argument list separator&lt;br /&gt;
; char : second argument type&lt;br /&gt;
; def : second argument name&lt;br /&gt;
; ) : bracket (or closing bracket for function arguments)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;English language sentence&#039;&#039;&#039;:  The cat ate the mouse.&lt;br /&gt;
; The : definite article&lt;br /&gt;
; cat : subject&lt;br /&gt;
; ate : verb&lt;br /&gt;
; the : definite article&lt;br /&gt;
; mouse : complement&lt;br /&gt;
; . : sentence ending point&lt;br /&gt;
&lt;br /&gt;
:Could anyone write a natural language example please? I don&#039;t know well grammatical categories of English language in English to do so.&lt;br /&gt;
&lt;br /&gt;
There will be probably some settings to determine how a grade should be calculated in case of errors, but they could be defined later.&lt;br /&gt;
&lt;br /&gt;
===Types of errors to report===&lt;br /&gt;
# misspelled word (number, another complex lexem)&lt;br /&gt;
# missing separator&lt;br /&gt;
# invalid separator&lt;br /&gt;
# misplaced word (number, etc)&lt;br /&gt;
# missing word (number, etc)&lt;br /&gt;
# unwanted word (number, separator, etc)&lt;br /&gt;
&lt;br /&gt;
===Examples of error reporting===&lt;br /&gt;
&#039;&#039;&#039;Correct answer&#039;&#039;&#039;: void function(int abc, char def)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Student&#039;s response&#039;&#039;&#039;: funcion (int, chardef)void&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Feedback from the question&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* please check function name, you are probably misspelled it&lt;br /&gt;
* first argument name is missing&lt;br /&gt;
* missing separator between second argument type and second argument name&lt;br /&gt;
* type of returned value is probably misplaced&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:Auto-feedback_shortanswer_question&amp;diff=66522</id>
		<title>Development:Auto-feedback shortanswer question</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:Auto-feedback_shortanswer_question&amp;diff=66522"/>
		<updated>2009-12-15T12:44:35Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Creating a question */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Goal==&lt;br /&gt;
Develop a question type, that was similar to shortanswer (except that no initial wildcard support is planned, thought it can be added later) which will be able to analyze student answer and automatically generate feedback messages describing errors, which could be useful to the student without revealing correct answer (if necessary).&lt;br /&gt;
&lt;br /&gt;
Such question is very useful in adaptive mode (and in non-adaptive mode too for courses with minimal teacher involvement) whenever you try to teach some language (eithern natural or artificial). If someone knows about works or papers that trying to do similar things please let me know there or on page comments.&lt;br /&gt;
&lt;br /&gt;
Question still will be based (at least from the beginning) on entering a number of correct answers.&lt;br /&gt;
&lt;br /&gt;
Question will be able to handle correct answer with several identical words, but that is discouraged (unless absolutely necessary) beacuse it will lower accuracy of feedback.&lt;br /&gt;
&lt;br /&gt;
==External specification draft==&lt;br /&gt;
&lt;br /&gt;
===Creating a question===&lt;br /&gt;
Teacher input one or several correct answers (with possible feedback describing particular answer). These answers are tokenised (this process will be called &#039;&#039;&#039;scanning&#039;&#039;&#039; later) to break them down to smaller parts (&#039;&#039;&#039;lexems&#039;&#039;&#039;): words, numbers, separators, operators etc. &lt;br /&gt;
:Since I couldn&#039;t start this work from writing universal scanner similar to the flex etc, it would be good to have pluginable scanners for the first time (e.g. natural language scanner, C++ scanner etc).&lt;br /&gt;
&lt;br /&gt;
Teacher than asked for a names for all lexems, that could be shown to the user without revealing correct answer. Examples of tokenised answer with named lexems: &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function header in C language&#039;&#039;&#039;:   void function(int abc, char def) &lt;br /&gt;
; void : type of returned value&lt;br /&gt;
; function : function name&lt;br /&gt;
; ( : bracket (or opening bracket for function arguments)&lt;br /&gt;
; int : first argument type&lt;br /&gt;
; abc : first argument name&lt;br /&gt;
; , : argument list separator&lt;br /&gt;
; char : second argument type&lt;br /&gt;
; def : second argument name&lt;br /&gt;
; ) : bracket (or closing bracket for function arguments)&lt;br /&gt;
&lt;br /&gt;
:Could anyone write a natural language example please? I don&#039;t know well grammatical categories of English language in English to do so.&lt;br /&gt;
&lt;br /&gt;
There will be probably some settings to determine how a grade should be calculated in case of errors, but they could be defined later.&lt;br /&gt;
&lt;br /&gt;
===Types of errors to report===&lt;br /&gt;
# misspelled word (number, another complex lexem)&lt;br /&gt;
# missing separator&lt;br /&gt;
# invalid separator&lt;br /&gt;
# misplaced word (number, etc)&lt;br /&gt;
# missing word (number, etc)&lt;br /&gt;
# unwanted word (number, separator, etc)&lt;br /&gt;
&lt;br /&gt;
===Examples of error reporting===&lt;br /&gt;
&#039;&#039;&#039;Correct answer&#039;&#039;&#039;: void function(int abc, char def)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Student&#039;s response&#039;&#039;&#039;: funcion (int, chardef)void&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Feedback from the question&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* please check function name, you are probably misspelled it&lt;br /&gt;
* first argument name is missing&lt;br /&gt;
* missing separator between second argument type and second argument name&lt;br /&gt;
* type of returned value is probably misplaced&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:Auto-feedback_shortanswer_question&amp;diff=66521</id>
		<title>Development:Auto-feedback shortanswer question</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:Auto-feedback_shortanswer_question&amp;diff=66521"/>
		<updated>2009-12-15T12:42:09Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Examples of error reporting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Goal==&lt;br /&gt;
Develop a question type, that was similar to shortanswer (except that no initial wildcard support is planned, thought it can be added later) which will be able to analyze student answer and automatically generate feedback messages describing errors, which could be useful to the student without revealing correct answer (if necessary).&lt;br /&gt;
&lt;br /&gt;
Such question is very useful in adaptive mode (and in non-adaptive mode too for courses with minimal teacher involvement) whenever you try to teach some language (eithern natural or artificial). If someone knows about works or papers that trying to do similar things please let me know there or on page comments.&lt;br /&gt;
&lt;br /&gt;
Question still will be based (at least from the beginning) on entering a number of correct answers.&lt;br /&gt;
&lt;br /&gt;
Question will be able to handle correct answer with several identical words, but that is discouraged (unless absolutely necessary) beacuse it will lower accuracy of feedback.&lt;br /&gt;
&lt;br /&gt;
==External specification draft==&lt;br /&gt;
&lt;br /&gt;
===Creating a question===&lt;br /&gt;
Teacher input one or several correct answers (with possible feedback describing particular answer). These answers are tokenised (this process will be called &#039;&#039;&#039;scanning&#039;&#039;&#039; later) to break them down to smaller parts (&#039;&#039;&#039;lexems&#039;&#039;&#039;): words, numbers, separators, operators etc. &lt;br /&gt;
:Since I couldn&#039;t start this work from writing universal scanner similar to the flex etc, it would be good to have pluginable scanners for the first time (e.g. natural language scanner, C++ scanner etc).&lt;br /&gt;
&lt;br /&gt;
Teacher than asked for a names for all lexems, that could be shown to the user without revealing correct answer. Examples of tokenised answer with named lexems: &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function header in C language&#039;&#039;&#039;:   void function(int abc, char def) &lt;br /&gt;
* void - type of returned value&lt;br /&gt;
* function - function name&lt;br /&gt;
* ( - bracket (or opening bracket for function arguments)&lt;br /&gt;
* int - type of the first argument&lt;br /&gt;
* abc - name of the first argument&lt;br /&gt;
* , - argument list separator&lt;br /&gt;
* char - type of the second argument&lt;br /&gt;
* def - name of the second argument&lt;br /&gt;
* ) - bracket (or closing bracket for function arguments)&lt;br /&gt;
&lt;br /&gt;
:Could anyone write a natural language example please? I don&#039;t know well grammatical categories of English language in English to do so.&lt;br /&gt;
&lt;br /&gt;
There will be probably some settings to determine how a grade should be calculated in case of errors, but they could be defined later.&lt;br /&gt;
&lt;br /&gt;
===Types of errors to report===&lt;br /&gt;
# misspelled word (number, another complex lexem)&lt;br /&gt;
# missing separator&lt;br /&gt;
# invalid separator&lt;br /&gt;
# misplaced word (number, etc)&lt;br /&gt;
# missing word (number, etc)&lt;br /&gt;
# unwanted word (number, separator, etc)&lt;br /&gt;
&lt;br /&gt;
===Examples of error reporting===&lt;br /&gt;
&#039;&#039;&#039;Correct answer&#039;&#039;&#039;: void function(int abc, char def)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Student&#039;s response&#039;&#039;&#039;: funcion (int, chardef)void&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Feedback from the question&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
* please check function name, you are probably misspelled it&lt;br /&gt;
* first argument name is missing&lt;br /&gt;
* missing separator between second argument type and second argument name&lt;br /&gt;
* type of returned value is probably misplaced&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:Auto-feedback_shortanswer_question&amp;diff=66520</id>
		<title>Development:Auto-feedback shortanswer question</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:Auto-feedback_shortanswer_question&amp;diff=66520"/>
		<updated>2009-12-15T12:34:56Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Types of errors to report */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Goal==&lt;br /&gt;
Develop a question type, that was similar to shortanswer (except that no initial wildcard support is planned, thought it can be added later) which will be able to analyze student answer and automatically generate feedback messages describing errors, which could be useful to the student without revealing correct answer (if necessary).&lt;br /&gt;
&lt;br /&gt;
Such question is very useful in adaptive mode (and in non-adaptive mode too for courses with minimal teacher involvement) whenever you try to teach some language (eithern natural or artificial). If someone knows about works or papers that trying to do similar things please let me know there or on page comments.&lt;br /&gt;
&lt;br /&gt;
Question still will be based (at least from the beginning) on entering a number of correct answers.&lt;br /&gt;
&lt;br /&gt;
Question will be able to handle correct answer with several identical words, but that is discouraged (unless absolutely necessary) beacuse it will lower accuracy of feedback.&lt;br /&gt;
&lt;br /&gt;
==External specification draft==&lt;br /&gt;
&lt;br /&gt;
===Creating a question===&lt;br /&gt;
Teacher input one or several correct answers (with possible feedback describing particular answer). These answers are tokenised (this process will be called &#039;&#039;&#039;scanning&#039;&#039;&#039; later) to break them down to smaller parts (&#039;&#039;&#039;lexems&#039;&#039;&#039;): words, numbers, separators, operators etc. &lt;br /&gt;
:Since I couldn&#039;t start this work from writing universal scanner similar to the flex etc, it would be good to have pluginable scanners for the first time (e.g. natural language scanner, C++ scanner etc).&lt;br /&gt;
&lt;br /&gt;
Teacher than asked for a names for all lexems, that could be shown to the user without revealing correct answer. Examples of tokenised answer with named lexems: &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function header in C language&#039;&#039;&#039;:   void function(int abc, char def) &lt;br /&gt;
* void - type of returned value&lt;br /&gt;
* function - function name&lt;br /&gt;
* ( - bracket (or opening bracket for function arguments)&lt;br /&gt;
* int - type of the first argument&lt;br /&gt;
* abc - name of the first argument&lt;br /&gt;
* , - argument list separator&lt;br /&gt;
* char - type of the second argument&lt;br /&gt;
* def - name of the second argument&lt;br /&gt;
* ) - bracket (or closing bracket for function arguments)&lt;br /&gt;
&lt;br /&gt;
:Could anyone write a natural language example please? I don&#039;t know well grammatical categories of English language in English to do so.&lt;br /&gt;
&lt;br /&gt;
There will be probably some settings to determine how a grade should be calculated in case of errors, but they could be defined later.&lt;br /&gt;
&lt;br /&gt;
===Types of errors to report===&lt;br /&gt;
# misspelled word (number, another complex lexem)&lt;br /&gt;
# missing separator&lt;br /&gt;
# invalid separator&lt;br /&gt;
# misplaced word (number, etc)&lt;br /&gt;
# missing word (number, etc)&lt;br /&gt;
# unwanted word (number, separator, etc)&lt;br /&gt;
&lt;br /&gt;
===Examples of error reporting===&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/21/en/index.php?title=Development:Auto-feedback_shortanswer_question&amp;diff=66519</id>
		<title>Development:Auto-feedback shortanswer question</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/21/en/index.php?title=Development:Auto-feedback_shortanswer_question&amp;diff=66519"/>
		<updated>2009-12-15T12:31:19Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: New page: ==Goal== Develop a question type, that was similar to shortanswer (except that no initial wildcard support is planned, thought it can be added later) which will be able to analyze student ...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Goal==&lt;br /&gt;
Develop a question type, that was similar to shortanswer (except that no initial wildcard support is planned, thought it can be added later) which will be able to analyze student answer and automatically generate feedback messages describing errors, which could be useful to the student without revealing correct answer (if necessary).&lt;br /&gt;
&lt;br /&gt;
Such question is very useful in adaptive mode (and in non-adaptive mode too for courses with minimal teacher involvement) whenever you try to teach some language (eithern natural or artificial). If someone knows about works or papers that trying to do similar things please let me know there or on page comments.&lt;br /&gt;
&lt;br /&gt;
Question still will be based (at least from the beginning) on entering a number of correct answers.&lt;br /&gt;
&lt;br /&gt;
Question will be able to handle correct answer with several identical words, but that is discouraged (unless absolutely necessary) beacuse it will lower accuracy of feedback.&lt;br /&gt;
&lt;br /&gt;
==External specification draft==&lt;br /&gt;
&lt;br /&gt;
===Creating a question===&lt;br /&gt;
Teacher input one or several correct answers (with possible feedback describing particular answer). These answers are tokenised (this process will be called &#039;&#039;&#039;scanning&#039;&#039;&#039; later) to break them down to smaller parts (&#039;&#039;&#039;lexems&#039;&#039;&#039;): words, numbers, separators, operators etc. &lt;br /&gt;
:Since I couldn&#039;t start this work from writing universal scanner similar to the flex etc, it would be good to have pluginable scanners for the first time (e.g. natural language scanner, C++ scanner etc).&lt;br /&gt;
&lt;br /&gt;
Teacher than asked for a names for all lexems, that could be shown to the user without revealing correct answer. Examples of tokenised answer with named lexems: &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function header in C language&#039;&#039;&#039;:   void function(int abc, char def) &lt;br /&gt;
* void - type of returned value&lt;br /&gt;
* function - function name&lt;br /&gt;
* ( - bracket (or opening bracket for function arguments)&lt;br /&gt;
* int - type of the first argument&lt;br /&gt;
* abc - name of the first argument&lt;br /&gt;
* , - argument list separator&lt;br /&gt;
* char - type of the second argument&lt;br /&gt;
* def - name of the second argument&lt;br /&gt;
* ) - bracket (or closing bracket for function arguments)&lt;br /&gt;
&lt;br /&gt;
:Could anyone write a natural language example please? I don&#039;t know well grammatical categories of English language in English to do so.&lt;br /&gt;
&lt;br /&gt;
There will be probably some settings to determine how a grade should be calculated in case of errors, but they could be defined later.&lt;br /&gt;
&lt;br /&gt;
===Types of errors to report===&lt;br /&gt;
&lt;br /&gt;
===Examples of error reporting===&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
</feed>