<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ca">
	<id>https://docs.moodle.org/2x/ca/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Oasychev</id>
	<title>MoodleDocs - Contribucions de l&amp;#039;usuari [ca]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/2x/ca/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Oasychev"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/Especial:Contribucions/Oasychev"/>
	<updated>2026-04-14T19:13:39Z</updated>
	<subtitle>Contribucions de l&amp;#039;usuari</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Fitxer:CorrectWritingStudentAnswer.PNG&amp;diff=103452</id>
		<title>Fitxer:CorrectWritingStudentAnswer.PNG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Fitxer:CorrectWritingStudentAnswer.PNG&amp;diff=103452"/>
		<updated>2013-03-02T22:41:38Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Fitxer:CorrectWritingEditForm.PNG&amp;diff=103451</id>
		<title>Fitxer:CorrectWritingEditForm.PNG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Fitxer:CorrectWritingEditForm.PNG&amp;diff=103451"/>
		<updated>2013-03-02T22:41:22Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Fitxer:CorrectWritingAnswerStep2.PNG&amp;diff=103450</id>
		<title>Fitxer:CorrectWritingAnswerStep2.PNG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Fitxer:CorrectWritingAnswerStep2.PNG&amp;diff=103450"/>
		<updated>2013-03-02T22:41:05Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Fitxer:CorrectWritingAnswerStep1.PNG&amp;diff=103449</id>
		<title>Fitxer:CorrectWritingAnswerStep1.PNG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Fitxer:CorrectWritingAnswerStep1.PNG&amp;diff=103449"/>
		<updated>2013-03-02T22:40:46Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=question/type/correctwriting&amp;diff=103448</id>
		<title>question/type/correctwriting</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=question/type/correctwriting&amp;diff=103448"/>
		<updated>2013-03-02T22:39:37Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Created page with &amp;quot;{{Questions}}===Goal===  This question type aims to automatically report of student mistakes in a shortanswer question, when student is learning programming language or in simple...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}===Goal===&lt;br /&gt;
&lt;br /&gt;
This question type aims to automatically report of student mistakes in a shortanswer question, when student is learning programming language or in simple cases of natural language learning, when student must write correct string (statement, sentence), and we should give him information about skipping some words or placing words in incorrect order. This also could be useful, when we don&#039;t give actual information about what words (or some kinds of symbols) was moved or absent in answer, but their meaning, thus forcing student to know what does this part of symbol means. This allows for some training without direct teacher supervision.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===How input is splitted===&lt;br /&gt;
Teacher should enter one or several correct answers (with possible feedback describing particular answer). These answers are &#039;&#039;tokenized&#039;&#039; (this process will be called &#039;&#039;scanning&#039;&#039; later) to break them down to smallest meaningful parts (&#039;&#039;tokens&#039;&#039; or &#039;&#039;lexems&#039;&#039;): words, numbers, punctuation marks, operators etc. These parts depend on the language used.&lt;br /&gt;
&lt;br /&gt;
The question will later analyze and print errors in the placement of these tokens. But when we are telling student, that &#039;&#039;function name&#039;&#039; (or a &#039;&#039;subject&#039;&#039;) in his answer is misplaced (or absent), we often don&#039;t want to disclose to the student information about exact word for that function name or subject. If student will see the word he misplaced, he could just start trying to move it. If the student will see grammatical role of this word (e.g. &amp;quot;function name&amp;quot; or &amp;quot;subject&amp;quot; misplaced), that will stimulate him to think in the grammatical categories (i.e. what is a subject in my response and where it should be?). So the teacher asked to enter grammatical descriptions for each token in the correct answer. That is done as a two-step process.&lt;br /&gt;
&lt;br /&gt;
In answer form, you can enter answer and supply a specific feedback for it:&lt;br /&gt;
&lt;br /&gt;
[[File:CorrectWritingAnswerStep1.PNG]]&lt;br /&gt;
&lt;br /&gt;
Than hit &amp;quot;Save changes&amp;quot;. The answer form changes, to allow you to enter a description for each token. You can skip descriptions for some token, just hitting enter, making an empty line in text field, then the student will the literal representation of the token instead:&lt;br /&gt;
&lt;br /&gt;
[[File:CorrectWritingAnswerStep2.PNG]]&lt;br /&gt;
&lt;br /&gt;
Than press &amp;quot;Save changes&amp;quot; button again.&lt;br /&gt;
&lt;br /&gt;
===Question type settings===&lt;br /&gt;
&lt;br /&gt;
When creating new CorrectWriting question type you will see the form with following settings:&lt;br /&gt;
&lt;br /&gt;
[[File:CorrectWritingEditForm.PNG]]&lt;br /&gt;
&lt;br /&gt;
The single most important setting therre is an &#039;&#039;&#039;answers language&#039;&#039;&#039;. It defines a way, in which you answer will be breaked down to lexemes (tokens). Other settings are much less important and you may not need to worry about them first. Most of them are used to fine tune question grading and are advanced.&lt;br /&gt;
 &lt;br /&gt;
You can see the following parameters:&lt;br /&gt;
&lt;br /&gt;
  ; Lexical error threshold : a treshold, which will be used when comparing tokens from student response to a teacher answer will be used to match with errors with Levenstein distance. If amount of errors when comparing two tokens is lesser than product of this treshold and length of teacher token - than tokens are the same with errors. Since search for misspellings is not implementend this parameter is &#039;&#039;&#039;unused and hidden in form&#039;&#039;&#039;.&lt;br /&gt;
  ; Penalty for lexical error : a penalty, that will be substracted from grade, when found one misspelling. Since search for misspellings is not implementend this parameter is &#039;&#039;&#039;unused and hidden in form&#039;&#039;&#039;.&lt;br /&gt;
; Penalty for missing token: a penalty, that will be substracted from grade for each absent token in student response.&lt;br /&gt;
; Penalty for extra token: a penalty, that will be substracted from grade for each odd token in student response.&lt;br /&gt;
; Penalty for misplaced token: a penalty, that will be substracted from grade for each misplaced token in student response, that is placed in incorrect order, which is taken from teacher&#039;s answer.&lt;br /&gt;
; Minimum grade for answer to find and display mistakes : sometimes, teacher may point out for student some hints for especially bad answers, and their comments must be viewed, when student&#039;s and teacher&#039;s answers are the same. Teacher can define an answer with grade, lower than that border for this answer and point out some bad answer with custom error message&lt;br /&gt;
; Maximum percent of mistakes in students response: when student writes response with lots of mistakes, we can reject his answer in favor of matching with another teacher&#039;s answer, when count of mistakes is bigger than product of this parameter and count of parts in answer. &lt;br /&gt;
; Language of answer : a language, which will be used, when analizing some answer and student response. Currently supported languages are &#039;&#039;english language&#039;&#039; and &#039;&#039;C programming language&#039;&#039;.&lt;br /&gt;
; Hinting settings : allows to enable particular hints for multi-stage behaviours and setting penalties for their use. See next sections for details about hints.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
The CorrectWriting question type uses hinting behaviours and is able to do special hints - for now in adaptive behaviour - for a penalty in the grade (may be set to 0). You could enable them setting penalty for the hint below 1.&lt;br /&gt;
&lt;br /&gt;
; What is hint : tells student a token text instead of description. I.e. &#039;&#039;&#039;The subject is &amp;quot;cat&amp;quot;.&#039;&#039;&#039; Used for misplaced token and absent token mistakes. For absent token mistake the penalty is multiplied to the &#039;&#039;absent hint penalty factor&#039;&#039;,  since it discloses exact text of the word student omitted.&lt;br /&gt;
; Where is text hint : shows a message where a token should be placed, based on you answer, using descriptions where possible. I.e. &#039;&#039;&#039;The subject should be placed between definite article and verb.&#039;&#039;&#039; Used for misplaced token and absent token mistakes.&lt;br /&gt;
  ; Where is picture hint : Under development.&lt;br /&gt;
&lt;br /&gt;
===Example of grading a student response===&lt;br /&gt;
&lt;br /&gt;
Please consider the following example, for question, partially described above:&lt;br /&gt;
&lt;br /&gt;
[[File:CorrectWritingStudentAnswer.PNG]]&lt;br /&gt;
&lt;br /&gt;
You see the examples of student mistakes. You can toggle various options of quiz, hiding some picture of mistakes or hiding some mistakes sentences for your own purpose.&lt;br /&gt;
&lt;br /&gt;
===Installing CorrectWriting question type===&lt;br /&gt;
&lt;br /&gt;
To install this question type, you need [http://code.google.com/r/oasychev-formallangs-block/ formal languages block] and [http://code.google.com/r/oasychev-correctwriting/ question type]. Also you need a  [http://code.google.com/p/oasychev-moodle-plugins poas question type].&lt;br /&gt;
&lt;br /&gt;
Just copy all of them in your Moodle installation folder, overwriting if need to and go to Site administration in web-interface, proceeding with the installation and you&#039;re done.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
Please, consider the following examples.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Function header in C language&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Student must write simple function header for function, that is described naturally: void function(int abc, char def), with following descriptions of parts: &lt;br /&gt;
&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;
If student submits the following answer, like: &#039;&#039;function(abc, def) void &#039;&#039;, question type will produce  the following output:&lt;br /&gt;
&lt;br /&gt;
; type of returned value is misplaced&lt;br /&gt;
; first argument type is missing&lt;br /&gt;
; second argument type is missing&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;English language sentence&#039;&#039;&#039;:  &lt;br /&gt;
&lt;br /&gt;
Student must write simple sentence, while learning foreign language: The cat ate the mouse, with following descriptions of parts&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;
If student submits the following answer, like:  The cat eat the mouse, question type will produce the following output:&lt;br /&gt;
&lt;br /&gt;
; &amp;quot;eat&amp;quot; should not be in response&lt;br /&gt;
; verb  is missing&lt;br /&gt;
; sententence ending point is missing.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=102665</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=102665"/>
		<updated>2013-01-06T21:46:59Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &lt;br /&gt;
#*&#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
#* If you have too many characters that need escaping in some fragment, you can use &#039;&#039;&#039;\Q ... \E&#039;&#039;&#039; sequence instead. Anything between \Q and \E is treated literally as characters:&lt;br /&gt;
#** &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#** &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot. Loses it&#039;s special meaning inside character class.&lt;br /&gt;
# &#039;&#039;&#039;Character classes&#039;&#039;&#039; match any character defined in them. Character classes are defined by square brackets. The particular ways to define a character class are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; or &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets (can be used both inside or outside character classes):&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Unicode properties&#039;&#039;&#039; are special escape-sequences &amp;quot;\p{xx}&amp;quot; (positive) or &amp;quot;\P{xx}&amp;quot; (negative) for matching specific unicode characters which could be used both inside or outside character classes (the complete list of &amp;quot;xx&amp;quot; variations can be found at found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm):&lt;br /&gt;
#* &amp;quot;\p{Ll}&amp;quot; matches any lowercase letter;&lt;br /&gt;
#* &amp;quot;\P{Lu}&amp;quot; matches any non-uppercase letter.&lt;br /&gt;
# &#039;&#039;&#039;POSIX character classes&#039;&#039;&#039; are used for the same purpose as unicode properties (and complete list of them can be found on the Internet too), but may not work with non-ASCII characters. They are allowed only inside character classes:&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:alnum:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any alpha-numeric character;&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:^digit:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any non-digit chararcter.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands (have those meaning only outside character classes):&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;&#039;remember&#039;&#039; substrings captured by the regex. The simplest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplеst syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
# &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
# &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
# &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
# &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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;
&lt;br /&gt;
=====Next character hinting=====&lt;br /&gt;
When next character hinting is available, student will have the &#039;&#039;&#039;hint next character&#039;&#039;&#039; button by pressing which he receives 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;
&lt;br /&gt;
=====Next lexem hinting=====&lt;br /&gt;
&#039;&#039;&#039;Lexem&#039;&#039;&#039; means an atomic part of a language. For natural language a &#039;&#039;word&#039;&#039;, a &#039;&#039;number&#039;&#039;, a &#039;&#039;punctuation mark&#039;&#039; (or group of marks like &#039;?!&#039; or &#039;...&#039;) are lexemes. For a programming language it&#039;s a &#039;&#039;keyword&#039;&#039;, a &#039;&#039;variable name&#039;&#039;, a &#039;&#039;constant&#039;&#039;, an &#039;&#039;operator&#039;&#039;. Note that spaces are usually not considered to be lexems, but separators between them, since they don&#039;t have any particular meaning.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Next lexem hint&#039;&#039;&#039; will show to the student either completion of current lexem (if partial match ends inside it) or next one (if student just complete current lexem). Like &lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are bl&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;ue&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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;,&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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; white&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Preg question type, since the 2.3 release, allows usage of next lexem hinting using the &#039;&#039;formal languages block&#039;&#039;. You should choose the language in which you expect a response for you question, since lexem borders are different for different languages. For now it supports only two languages (but there will be more):&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - a simple lexer, that recognize words, numbers and punctuation;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++).&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t a word you would like you students to see on the hinting button. You can enter another word in the question description.&lt;br /&gt;
&lt;br /&gt;
=====General hinting rules=====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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; (leading to the &amp;quot; and&amp;quot; path). The question will choose &#039;,&#039; since it leads to the shortest path to complete the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;lt;nowiki&amp;gt;&amp;quot;[[:hamster:]]&amp;quot;&amp;lt;/nowiki&amp;gt;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect character set range &amp;quot;[z-a]&amp;quot;;&lt;br /&gt;
* incorrect quantifier ranges &amp;quot;{5,3}&amp;quot;;&lt;br /&gt;
* \ at end of pattern &amp;quot;ab\&amp;quot;;&lt;br /&gt;
* \c at end of pattern &amp;quot;ab\c&amp;quot;;&lt;br /&gt;
* invalid escape sequence;&lt;br /&gt;
* POSIX class ouside of a character set &amp;quot;[:digit:]&amp;quot;;&lt;br /&gt;
* reference to unexisting subpattern (abc)\2;&lt;br /&gt;
* unknown, wrong or unsupported modifier &amp;quot;(?z)&amp;quot;;&lt;br /&gt;
* missing ) after comment &amp;quot;(?#comment&amp;quot;;&lt;br /&gt;
* missing conditional subpattern name ending;&lt;br /&gt;
* missing ) after (?C;&lt;br /&gt;
* missing subpattern name ending;&lt;br /&gt;
* missing backreference name ending;&lt;br /&gt;
* missing backreference name beginning;&lt;br /&gt;
* missing ) after control sequence;&lt;br /&gt;
* wrong conditional subpattern number, digits expected;&lt;br /&gt;
* assertion or condition expected &amp;quot;(?()a|b)&amp;quot;;&lt;br /&gt;
* character code too big &amp;quot;\x{ffffffff}&amp;quot;;&lt;br /&gt;
* character code disallowed &amp;quot;\x{d800}&amp;quot;;&lt;br /&gt;
* invalid condition (?(0);&lt;br /&gt;
* too big number in (?C...) &amp;quot;(?C256)&amp;quot;;&lt;br /&gt;
* two named subpatterns have the same name &amp;quot;(?&amp;lt;name&amp;gt;a)(?&amp;lt;name&amp;gt;b)&amp;quot;;&lt;br /&gt;
* backreference to the whole expression &amp;quot;abc\g{0}&amp;quot;;&lt;br /&gt;
* different subpattern names for subpatterns of the same number &amp;quot;(?|(?&amp;lt;name1&amp;gt;a)|(?&amp;lt;name2&amp;gt;b))&amp;quot;;&lt;br /&gt;
* subpattern name expected &amp;quot;(?&amp;lt;&amp;gt;abc)&amp;quot;;&lt;br /&gt;
* \c should be followed by an ascii character &amp;quot;\cй&amp;quot;;&lt;br /&gt;
* \L, \l, \N{name}, \U, and \u are unsupported;&lt;br /&gt;
* unrecognized character after (?&amp;lt;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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 and misplaced things====&lt;br /&gt;
Joseph Rezeau&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s REGEX question type!&lt;br /&gt;
&lt;br /&gt;
You can also have a rough search for &#039;&#039;&#039;misplaced words&#039;&#039;&#039; (it will actually work only if anything else is correct) using syntax like this:&lt;br /&gt;
   (?!&amp;lt;I\s+)\bam\b(?!\s+victor)&lt;br /&gt;
This expression catches misplaced &amp;quot;am&amp;quot; in the sentence &amp;quot;I am victor&amp;quot; by first looking for &amp;quot;am&amp;quot; doens&#039;t have &amp;quot;I&amp;quot; before it (&amp;quot;(?!&amp;lt;I\s+)&amp;quot; part) and then &amp;quot;victor&amp;quot; after it (&amp;quot;(?!\s+victor)&amp;quot; part). &amp;quot;\s+&amp;quot; allows any number of spaces between words. If you want to catch the first (last) word (punctuation mark, etc) - then you should place simple assertions for start/end of string (&amp;quot;^&amp;quot; or &amp;quot;$&amp;quot;) instead of words in related assertions. For instance to look for misplaced &amp;quot;I&amp;quot; you should write something like&lt;br /&gt;
   (?!&amp;lt;^)\bI\b(?!\s+am)&lt;br /&gt;
which looks for &amp;quot;I&amp;quot; that is not preceded by start of the string and not followed by &amp;quot;am&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Note, that if you have several answers to catch missing and misplaced things, only one will actually work for any given student response.&lt;br /&gt;
&lt;br /&gt;
NFA and DFA matchers for now don&#039;t supports complex assertions, used by these regexes. Since the Preg 2.3 release you can combine hints and catching missing words. But you should be sure that the answers that look for missing things (and other to give specific feedback) have a &#039;&#039;&#039;fraction&#039;&#039;&#039; (grade) lower, that &#039;&#039;&#039;hint grade border&#039;&#039;&#039; (see [[#Hinting]]). You actually don&#039;t want to generate hints for these answers, as they don&#039;t define a correct situation, so it&#039;s no restriction actually.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 can&#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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions in main (hinting) regular expressions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve simple assertions support&lt;br /&gt;
* Support for complex assertions&lt;br /&gt;
* Support for regular expresison recursion&lt;br /&gt;
* Support for approximate matching to catch typos in answers&lt;br /&gt;
* Add a set of authoring tools to make writing regular expressions easier&lt;br /&gt;
* Add more languages for next lexem hinting&lt;br /&gt;
* Develop the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=question/type/preg&amp;diff=102664</id>
		<title>question/type/preg</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=question/type/preg&amp;diff=102664"/>
		<updated>2013-01-06T21:45:52Z</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/2x/ca/index.php?title=Preg_question_type&amp;diff=99850</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99850"/>
		<updated>2012-08-06T20:32:21Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Development plans */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &lt;br /&gt;
#*&#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
#* If you have too many characters that need escaping in some fragment, you can use &#039;&#039;&#039;\Q ... \E&#039;&#039;&#039; sequence instead. Anything between \Q and \E is treated literally as characters:&lt;br /&gt;
#** &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#** &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot. Loses it&#039;s special meaning inside character class.&lt;br /&gt;
# &#039;&#039;&#039;Character classes&#039;&#039;&#039; match any character defined in them. Character classes are defined by square brackets. The particular ways to define a character class are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; or &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets (can be used both inside or outside character classes):&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Unicode properties&#039;&#039;&#039; are special escape-sequences &amp;quot;\p{xx}&amp;quot; (positive) or &amp;quot;\P{xx}&amp;quot; (negative) for matching specific unicode characters which could be used both inside or outside character classes (the complete list of &amp;quot;xx&amp;quot; variations can be found at found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm):&lt;br /&gt;
#* &amp;quot;\p{Ll}&amp;quot; matches any lowercase letter;&lt;br /&gt;
#* &amp;quot;\P{Lu}&amp;quot; matches any non-uppercase letter.&lt;br /&gt;
# &#039;&#039;&#039;POSIX character classes&#039;&#039;&#039; are used for the same purpose as unicode properties (and complete list of them can be found on the Internet too), but may not work with non-ASCII characters. They are allowed only inside character classes:&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:alnum:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any alpha-numeric character;&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:^digit:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any non-digit chararcter.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands (have those meaning only outside character classes):&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;&#039;remember&#039;&#039; substrings captured by the regex. The simplest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplеst syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
# &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
# &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
# &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
# &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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;
&lt;br /&gt;
=====Next character hinting=====&lt;br /&gt;
When next character hinting is available, student will have the &#039;&#039;&#039;hint next character&#039;&#039;&#039; button by pressing which he receives 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;
&lt;br /&gt;
=====Next lexem hinting=====&lt;br /&gt;
&#039;&#039;&#039;Lexem&#039;&#039;&#039; means an atomic part of a language. For natural language a &#039;&#039;word&#039;&#039;, a &#039;&#039;number&#039;&#039;, a &#039;&#039;punctuation mark&#039;&#039; (or group of marks like &#039;?!&#039; or &#039;...&#039;) are lexemes. For a programming language it&#039;s a &#039;&#039;keyword&#039;&#039;, a &#039;&#039;variable name&#039;&#039;, a &#039;&#039;constant&#039;&#039;, an &#039;&#039;operator&#039;&#039;. Note that spaces are usually not considered to be lexems, but separators between them, since they don&#039;t have any particular meaning.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Next lexem hint&#039;&#039;&#039; will show to the student either completion of current lexem (if partial match ends inside it) or next one (if student just complete current lexem). Like &lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are bl&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;ue&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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;,&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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; white&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Preg question type, since the 2.3 release, allows usage of next lexem hinting using the &#039;&#039;formal languages block&#039;&#039;. You should choose the language in which you expect a response for you question, since lexem borders are different for different languages. For now it supports only two languages (but there will be more):&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - a simple lexer, that recognize words, numbers and punctuation;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++).&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t a word you would like you students to see on the hinting button. You can enter another word in the question description.&lt;br /&gt;
&lt;br /&gt;
=====General hinting rules=====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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; (leading to the &amp;quot; and&amp;quot; path). The question will choose &#039;,&#039; since it leads to the shortest path to complete the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;quot;[[:hamster:]]&amp;quot;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect ranges for quantifiers &amp;quot;a{5,4}&amp;quot; or character sets &amp;quot;[z-a]&amp;quot;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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 and misplaced things====&lt;br /&gt;
Joseph Rezeau&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s REGEX question type!&lt;br /&gt;
&lt;br /&gt;
You can also have a rough search for &#039;&#039;&#039;misplaced words&#039;&#039;&#039; (it will actually work only if anything else is correct) using syntax like this:&lt;br /&gt;
   (?!&amp;lt;I\s+)\bam\b(?!\s+victor)&lt;br /&gt;
This expression catches misplaced &amp;quot;am&amp;quot; in the sentence &amp;quot;I am victor&amp;quot; by first looking for &amp;quot;am&amp;quot; doens&#039;t have &amp;quot;I&amp;quot; before it (&amp;quot;(?!&amp;lt;I\s+)&amp;quot; part) and then &amp;quot;victor&amp;quot; after it (&amp;quot;(?!\s+victor)&amp;quot; part). &amp;quot;\s+&amp;quot; allows any number of spaces between words. If you want to catch the first (last) word (punctuation mark, etc) - then you should place simple assertions for start/end of string (&amp;quot;^&amp;quot; or &amp;quot;$&amp;quot;) instead of words in related assertions. For instance to look for misplaced &amp;quot;I&amp;quot; you should write something like&lt;br /&gt;
   (?!&amp;lt;^)\bI\b(?!\s+am)&lt;br /&gt;
which looks for &amp;quot;I&amp;quot; that is not preceded by start of the string and not followed by &amp;quot;am&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Note, that if you have several answers to catch missing and misplaced things, only one will actually work for any given student response.&lt;br /&gt;
&lt;br /&gt;
NFA and DFA matchers for now don&#039;t supports complex assertions, used by these regexes. Since the Preg 2.3 release you can combine hints and catching missing words. But you should be sure that the answers that look for missing things (and other to give specific feedback) have a &#039;&#039;&#039;fraction&#039;&#039;&#039; (grade) lower, that &#039;&#039;&#039;hint grade border&#039;&#039;&#039; (see [[#Hinting]]). You actually don&#039;t want to generate hints for these answers, as they don&#039;t define a correct situation, so it&#039;s no restriction actually.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 can&#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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions in main (hinting) regular expressions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve simple assertions support&lt;br /&gt;
* Support for complex assertions&lt;br /&gt;
* Support for regular expresison recursion&lt;br /&gt;
* Support for approximate matching to catch typos in answers&lt;br /&gt;
* Add a set of authoring tools to make writing regular expressions easier&lt;br /&gt;
* Add more languages for next lexem hinting&lt;br /&gt;
* Develop the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99849</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99849"/>
		<updated>2012-08-06T20:06:41Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Error reporting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &lt;br /&gt;
#*&#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
#* If you have too many characters that need escaping in some fragment, you can use &#039;&#039;&#039;\Q ... \E&#039;&#039;&#039; sequence instead. Anything between \Q and \E is treated literally as characters:&lt;br /&gt;
#** &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#** &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot. Loses it&#039;s special meaning inside character class.&lt;br /&gt;
# &#039;&#039;&#039;Character classes&#039;&#039;&#039; match any character defined in them. Character classes are defined by square brackets. The particular ways to define a character class are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; or &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets (can be used both inside or outside character classes):&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Unicode properties&#039;&#039;&#039; are special escape-sequences &amp;quot;\p{xx}&amp;quot; (positive) or &amp;quot;\P{xx}&amp;quot; (negative) for matching specific unicode characters which could be used both inside or outside character classes (the complete list of &amp;quot;xx&amp;quot; variations can be found at found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm):&lt;br /&gt;
#* &amp;quot;\p{Ll}&amp;quot; matches any lowercase letter;&lt;br /&gt;
#* &amp;quot;\P{Lu}&amp;quot; matches any non-uppercase letter.&lt;br /&gt;
# &#039;&#039;&#039;POSIX character classes&#039;&#039;&#039; are used for the same purpose as unicode properties (and complete list of them can be found on the Internet too), but may not work with non-ASCII characters. They are allowed only inside character classes:&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:alnum:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any alpha-numeric character;&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:^digit:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any non-digit chararcter.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands (have those meaning only outside character classes):&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;&#039;remember&#039;&#039; substrings captured by the regex. The simplest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplеst syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
# &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
# &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
# &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
# &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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;
&lt;br /&gt;
=====Next character hinting=====&lt;br /&gt;
When next character hinting is available, student will have the &#039;&#039;&#039;hint next character&#039;&#039;&#039; button by pressing which he receives 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;
&lt;br /&gt;
=====Next lexem hinting=====&lt;br /&gt;
&#039;&#039;&#039;Lexem&#039;&#039;&#039; means an atomic part of a language. For natural language a &#039;&#039;word&#039;&#039;, a &#039;&#039;number&#039;&#039;, a &#039;&#039;punctuation mark&#039;&#039; (or group of marks like &#039;?!&#039; or &#039;...&#039;) are lexemes. For a programming language it&#039;s a &#039;&#039;keyword&#039;&#039;, a &#039;&#039;variable name&#039;&#039;, a &#039;&#039;constant&#039;&#039;, an &#039;&#039;operator&#039;&#039;. Note that spaces are usually not considered to be lexems, but separators between them, since they don&#039;t have any particular meaning.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Next lexem hint&#039;&#039;&#039; will show to the student either completion of current lexem (if partial match ends inside it) or next one (if student just complete current lexem). Like &lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are bl&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;ue&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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;,&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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; white&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Preg question type, since the 2.3 release, allows usage of next lexem hinting using the &#039;&#039;formal languages block&#039;&#039;. You should choose the language in which you expect a response for you question, since lexem borders are different for different languages. For now it supports only two languages (but there will be more):&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - a simple lexer, that recognize words, numbers and punctuation;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++).&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t a word you would like you students to see on the hinting button. You can enter another word in the question description.&lt;br /&gt;
&lt;br /&gt;
=====General hinting rules=====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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; (leading to the &amp;quot; and&amp;quot; path). The question will choose &#039;,&#039; since it leads to the shortest path to complete the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;quot;[[:hamster:]]&amp;quot;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect ranges for quantifiers &amp;quot;a{5,4}&amp;quot; or character sets &amp;quot;[z-a]&amp;quot;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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 and misplaced things====&lt;br /&gt;
Joseph Rezeau&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s REGEX question type!&lt;br /&gt;
&lt;br /&gt;
You can also have a rough search for &#039;&#039;&#039;misplaced words&#039;&#039;&#039; (it will actually work only if anything else is correct) using syntax like this:&lt;br /&gt;
   (?!&amp;lt;I\s+)\bam\b(?!\s+victor)&lt;br /&gt;
This expression catches misplaced &amp;quot;am&amp;quot; in the sentence &amp;quot;I am victor&amp;quot; by first looking for &amp;quot;am&amp;quot; doens&#039;t have &amp;quot;I&amp;quot; before it (&amp;quot;(?!&amp;lt;I\s+)&amp;quot; part) and then &amp;quot;victor&amp;quot; after it (&amp;quot;(?!\s+victor)&amp;quot; part). &amp;quot;\s+&amp;quot; allows any number of spaces between words. If you want to catch the first (last) word (punctuation mark, etc) - then you should place simple assertions for start/end of string (&amp;quot;^&amp;quot; or &amp;quot;$&amp;quot;) instead of words in related assertions. For instance to look for misplaced &amp;quot;I&amp;quot; you should write something like&lt;br /&gt;
   (?!&amp;lt;^)\bI\b(?!\s+am)&lt;br /&gt;
which looks for &amp;quot;I&amp;quot; that is not preceded by start of the string and not followed by &amp;quot;am&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Note, that if you have several answers to catch missing and misplaced things, only one will actually work for any given student response.&lt;br /&gt;
&lt;br /&gt;
NFA and DFA matchers for now don&#039;t supports complex assertions, used by these regexes. Since the Preg 2.3 release you can combine hints and catching missing words. But you should be sure that the answers that look for missing things (and other to give specific feedback) have a &#039;&#039;&#039;fraction&#039;&#039;&#039; (grade) lower, that &#039;&#039;&#039;hint grade border&#039;&#039;&#039; (see [[#Hinting]]). You actually don&#039;t want to generate hints for these answers, as they don&#039;t define a correct situation, so it&#039;s no restriction actually.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 can&#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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions in main (hinting) regular expressions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve 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 the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99847</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99847"/>
		<updated>2012-08-06T19:46:01Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Next lexem hinting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &lt;br /&gt;
#*&#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
#* If you have too many characters that need escaping in some fragment, you could use &#039;&#039;&#039;\Q ... \E&#039;&#039;&#039; sequence instead. Anything between \Q and \E is treated literally as characters:&lt;br /&gt;
#** &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#** &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot. Loses it&#039;s special meaning inside character class.&lt;br /&gt;
# &#039;&#039;&#039;Character classes&#039;&#039;&#039; match any character defined in them. Character classes are defined by square brackets. The particular ways to define a character class are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; or &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets (could be used both inside or outside character classes):&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Unicode properties&#039;&#039;&#039; are special escape-sequences &amp;quot;\p{xx}&amp;quot; (positive) or &amp;quot;\P{xx}&amp;quot; (negative) for matching specific unicode characters which could be used both inside or outside character classes (the complete list of &amp;quot;xx&amp;quot; variations can be found at found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm):&lt;br /&gt;
#* &amp;quot;\p{Ll}&amp;quot; matches any lowercase letter;&lt;br /&gt;
#* &amp;quot;\P{Lu}&amp;quot; matches any non-uppercase letter.&lt;br /&gt;
# &#039;&#039;&#039;POSIX character classes&#039;&#039;&#039; are used for the same purpose as unicode properties (and complete list of them can be found on the Internet too), but may not work with non-ASCII characters. They are allowed only inside character classes:&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:alnum:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any alpha-numeric character;&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:^digit:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any non-digit chararcter.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands (have those meaning only outside character classes):&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;&#039;remember&#039;&#039; substrings captured by the regex. The simplest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplеst syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
# &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
# &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
# &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
# &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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;
&lt;br /&gt;
=====Next character hinting=====&lt;br /&gt;
When next character hinting is available, student will have the &#039;&#039;&#039;hint next character&#039;&#039;&#039; button by pressing which he receives 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;
&lt;br /&gt;
=====Next lexem hinting=====&lt;br /&gt;
&#039;&#039;&#039;Lexem&#039;&#039;&#039; means an atomic part of a language. For natural language a &#039;&#039;word&#039;&#039;, a &#039;&#039;number&#039;&#039;, a &#039;&#039;punctuation mark&#039;&#039; (or group of marks like &#039;?!&#039; or &#039;...&#039;) are lexemes. For a programming language it&#039;s a &#039;&#039;keyword&#039;&#039;, a &#039;&#039;variable name&#039;&#039;, a &#039;&#039;constant&#039;&#039;, an &#039;&#039;operator&#039;&#039;. Note that spaces are usually not considered to be lexems, but separators between them, since they don&#039;t have any particular meaning.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Next lexem hint&#039;&#039;&#039; will show to the student either completion of current lexem (if partial match ends inside it) or next one (if student just complete current lexem). Like &lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are bl&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;ue&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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;,&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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; white&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Preg question type, since the 2.3 release, allows usage of next lexem hinting using the &#039;&#039;formal languages block&#039;&#039;. You should choose the language in which you expect a response for you question, since lexem borders are different for different languages. For now it supports only two languages (but there will be more):&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - a simple lexer, that recognize words, numbers and punctuation;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++).&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t a word you would like you students to see on the hinting button. You can enter another word in the question description.&lt;br /&gt;
&lt;br /&gt;
=====General hinting rules=====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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; (leading to the &amp;quot; and&amp;quot; path). The question will choose &#039;,&#039; since it leads to the shortest path to complete the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* empty parentheses of any sort &amp;quot;(?=)&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;quot;[[:hamster:]]&amp;quot;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect ranges for quantifiers &amp;quot;a{5,4}&amp;quot; or character sets &amp;quot;[z-a]&amp;quot;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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 and misplaced things====&lt;br /&gt;
Joseph Rezeau&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s REGEX question type!&lt;br /&gt;
&lt;br /&gt;
You can also have a rough search for &#039;&#039;&#039;misplaced words&#039;&#039;&#039; (it will actually work only if anything else is correct) using syntax like this:&lt;br /&gt;
   (?!&amp;lt;I\s+)\bam\b(?!\s+victor)&lt;br /&gt;
This expression catches misplaced &amp;quot;am&amp;quot; in the sentence &amp;quot;I am victor&amp;quot; by first looking for &amp;quot;am&amp;quot; doens&#039;t have &amp;quot;I&amp;quot; before it (&amp;quot;(?!&amp;lt;I\s+)&amp;quot; part) and then &amp;quot;victor&amp;quot; after it (&amp;quot;(?!\s+victor)&amp;quot; part). &amp;quot;\s+&amp;quot; allows any number of spaces between words. If you want to catch the first (last) word (punctuation mark, etc) - then you should place simple assertions for start/end of string (&amp;quot;^&amp;quot; or &amp;quot;$&amp;quot;) instead of words in related assertions. For instance to look for misplaced &amp;quot;I&amp;quot; you should write something like&lt;br /&gt;
   (?!&amp;lt;^)\bI\b(?!\s+am)&lt;br /&gt;
which looks for &amp;quot;I&amp;quot; that is not preceded by start of the string and not followed by &amp;quot;am&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Note, that if you have several answers to catch missing and misplaced things, only one will actually work for any given student response.&lt;br /&gt;
&lt;br /&gt;
NFA and DFA matchers for now don&#039;t supports complex assertions, used by these regexes. Since the Preg 2.3 release you can combine hints and catching missing words. But you should be sure that the answers that look for missing things (and other to give specific feedback) have a &#039;&#039;&#039;fraction&#039;&#039;&#039; (grade) lower, that &#039;&#039;&#039;hint grade border&#039;&#039;&#039; (see [[#Hinting]]). You actually don&#039;t want to generate hints for these answers, as they don&#039;t define a correct situation, so it&#039;s no restriction actually.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions in main (hinting) regular expressions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve 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 the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99846</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99846"/>
		<updated>2012-08-06T19:44:02Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Operands */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &lt;br /&gt;
#*&#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
#* If you have too many characters that need escaping in some fragment, you could use &#039;&#039;&#039;\Q ... \E&#039;&#039;&#039; sequence instead. Anything between \Q and \E is treated literally as characters:&lt;br /&gt;
#** &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#** &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot. Loses it&#039;s special meaning inside character class.&lt;br /&gt;
# &#039;&#039;&#039;Character classes&#039;&#039;&#039; match any character defined in them. Character classes are defined by square brackets. The particular ways to define a character class are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; or &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets (could be used both inside or outside character classes):&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Unicode properties&#039;&#039;&#039; are special escape-sequences &amp;quot;\p{xx}&amp;quot; (positive) or &amp;quot;\P{xx}&amp;quot; (negative) for matching specific unicode characters which could be used both inside or outside character classes (the complete list of &amp;quot;xx&amp;quot; variations can be found at found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm):&lt;br /&gt;
#* &amp;quot;\p{Ll}&amp;quot; matches any lowercase letter;&lt;br /&gt;
#* &amp;quot;\P{Lu}&amp;quot; matches any non-uppercase letter.&lt;br /&gt;
# &#039;&#039;&#039;POSIX character classes&#039;&#039;&#039; are used for the same purpose as unicode properties (and complete list of them can be found on the Internet too), but may not work with non-ASCII characters. They are allowed only inside character classes:&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:alnum:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any alpha-numeric character;&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:^digit:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any non-digit chararcter.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands (have those meaning only outside character classes):&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;&#039;remember&#039;&#039; substrings captured by the regex. The simplest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplеst syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
# &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
# &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
# &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
# &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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;
&lt;br /&gt;
=====Next character hinting=====&lt;br /&gt;
When next character hinting is available, student will have the &#039;&#039;&#039;hint next character&#039;&#039;&#039; button by pressing which he receives 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;
&lt;br /&gt;
=====Next lexem hinting=====&lt;br /&gt;
&#039;&#039;&#039;Lexem&#039;&#039;&#039; means an atomic part of a language. For natural language a &#039;&#039;word&#039;&#039;, a &#039;&#039;number&#039;&#039;, a &#039;&#039;punctuation mark&#039;&#039; (or group of marks like &#039;?!&#039; or &#039;...&#039;) are lexemes. For a programming language it&#039;s a &#039;&#039;keyword&#039;&#039;, a &#039;&#039;variable name&#039;&#039;, a &#039;&#039;constant&#039;&#039;, an &#039;&#039;operator&#039;&#039;. Note that spaces are usually not considered to be lexems, but separators between them, since they don&#039;t have any particular meaning.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Next lexem hint&#039;&#039;&#039; will show to the student either completion of current lexem (if partial match ends inside it) or next one (if student just complete current lexem). Like &lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are bl&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;ue&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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;,&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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; white&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Preg question type, since the 2.3 release, allows usage of next lexem hinting using the &#039;&#039;formal languages block&#039;&#039;. You should choose the language you want to use, since lexem borders are different for different languages. For now it supports only two languages (but there will be more):&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - a simple lexer, that recognize words, numbers and punctuation;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++).&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t a word you would like you students to see on the hinting button. You can enter another word in the question description.&lt;br /&gt;
&lt;br /&gt;
=====General hinting rules=====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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; (leading to the &amp;quot; and&amp;quot; path). The question will choose &#039;,&#039; since it leads to the shortest path to complete the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* empty parentheses of any sort &amp;quot;(?=)&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;quot;[[:hamster:]]&amp;quot;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect ranges for quantifiers &amp;quot;a{5,4}&amp;quot; or character sets &amp;quot;[z-a]&amp;quot;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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 and misplaced things====&lt;br /&gt;
Joseph Rezeau&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s REGEX question type!&lt;br /&gt;
&lt;br /&gt;
You can also have a rough search for &#039;&#039;&#039;misplaced words&#039;&#039;&#039; (it will actually work only if anything else is correct) using syntax like this:&lt;br /&gt;
   (?!&amp;lt;I\s+)\bam\b(?!\s+victor)&lt;br /&gt;
This expression catches misplaced &amp;quot;am&amp;quot; in the sentence &amp;quot;I am victor&amp;quot; by first looking for &amp;quot;am&amp;quot; doens&#039;t have &amp;quot;I&amp;quot; before it (&amp;quot;(?!&amp;lt;I\s+)&amp;quot; part) and then &amp;quot;victor&amp;quot; after it (&amp;quot;(?!\s+victor)&amp;quot; part). &amp;quot;\s+&amp;quot; allows any number of spaces between words. If you want to catch the first (last) word (punctuation mark, etc) - then you should place simple assertions for start/end of string (&amp;quot;^&amp;quot; or &amp;quot;$&amp;quot;) instead of words in related assertions. For instance to look for misplaced &amp;quot;I&amp;quot; you should write something like&lt;br /&gt;
   (?!&amp;lt;^)\bI\b(?!\s+am)&lt;br /&gt;
which looks for &amp;quot;I&amp;quot; that is not preceded by start of the string and not followed by &amp;quot;am&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Note, that if you have several answers to catch missing and misplaced things, only one will actually work for any given student response.&lt;br /&gt;
&lt;br /&gt;
NFA and DFA matchers for now don&#039;t supports complex assertions, used by these regexes. Since the Preg 2.3 release you can combine hints and catching missing words. But you should be sure that the answers that look for missing things (and other to give specific feedback) have a &#039;&#039;&#039;fraction&#039;&#039;&#039; (grade) lower, that &#039;&#039;&#039;hint grade border&#039;&#039;&#039; (see [[#Hinting]]). You actually don&#039;t want to generate hints for these answers, as they don&#039;t define a correct situation, so it&#039;s no restriction actually.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions in main (hinting) regular expressions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve 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 the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99844</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99844"/>
		<updated>2012-08-06T19:38:34Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Next lexem hinting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &lt;br /&gt;
#*&#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
#* If you have too many characters that need escaping in some fragment, you could use &#039;&#039;&#039;\Q ... \E&#039;&#039;&#039; sequence instead. Anything between \Q and \E is treated literally as characters:&lt;br /&gt;
#** &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#** &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
# &#039;&#039;&#039;Character sets&#039;&#039;&#039; match any character defined in them. Character sets are defined by brackets. The particular ways to define a character set are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; and &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot.&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets:&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands:&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
# &#039;&#039;&#039;Unicode properties&#039;&#039;&#039; are special escape-sequences &amp;quot;\p{xx}&amp;quot; (positive) or &amp;quot;\P{xx}&amp;quot; (negative) for matching specific unicode characters (the complete list of &amp;quot;xx&amp;quot; variations can be found at found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm):&lt;br /&gt;
#* &amp;quot;\p{Ll}&amp;quot; matches any lowercase letter;&lt;br /&gt;
#* &amp;quot;\P{Lu}&amp;quot; matches any non-uppercase letter.&lt;br /&gt;
# &#039;&#039;&#039;POSIX character classes&#039;&#039;&#039; are used for the same purpose as unicode properties (and complete list of them can be found on the Internet too), but may not work with non-ASCII characters. They are allowed only inside character sets:&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:alnum:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any alpha-numeric character;&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:^digit:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any non-digit chararcter.&lt;br /&gt;
All characters and escape sequences work both inside and outside character sets, but POSIX classes are allowed only inside character sets [...]. Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;&#039;remember&#039;&#039; substrings captured by the regex. The simplest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplеst syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
# &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
# &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
# &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
# &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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;
&lt;br /&gt;
=====Next character hinting=====&lt;br /&gt;
When next character hinting is available, student will have the &#039;&#039;&#039;hint next character&#039;&#039;&#039; button by pressing which he receives 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;
&lt;br /&gt;
=====Next lexem hinting=====&lt;br /&gt;
&#039;&#039;&#039;Lexem&#039;&#039;&#039; means an atomic part of the language. For natural language a &#039;&#039;word&#039;&#039;, a &#039;&#039;number&#039;&#039;, a &#039;&#039;punctuation mark&#039;&#039; (or group of marks like &#039;?!&#039; or &#039;...&#039;) are lexemes. For a programming language it&#039;s a &#039;&#039;keyword&#039;&#039;, a &#039;&#039;variable name&#039;&#039;, a &#039;&#039;constant&#039;&#039;, an &#039;&#039;operator&#039;&#039;. Note that spaces are usually don&#039;t considered to be lexems, but a separators between them, since they don&#039;t have any particular meaning.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Next lexem hint&#039;&#039;&#039; will show to the student either completion of current lexem (if partial match ends inside it) or next one (if student just complete current lexem). Like &lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are bl&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;ue&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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;,&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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; white&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Preg question type since 2.3 version allows use of next lexem hinting using &#039;&#039;formal languages block&#039;&#039;. You should choose the language you use, since lexem borders are different for different languages. For now it supports only two languages, but there will be more:&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - a simple lexer, that recognize words, numbers and punctuation;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++).&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t a word you would like you students to see on the hinting button. You could enter another word in the question description.&lt;br /&gt;
&lt;br /&gt;
=====General hinting rules=====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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; (leading to the &amp;quot; and&amp;quot; path). The question will choose &#039;,&#039; since it leads to the shortest path to complete the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* empty parentheses of any sort &amp;quot;(?=)&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;quot;[[:hamster:]]&amp;quot;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect ranges for quantifiers &amp;quot;a{5,4}&amp;quot; or character sets &amp;quot;[z-a]&amp;quot;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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 and misplaced things====&lt;br /&gt;
Joseph Rezeau&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s REGEX question type!&lt;br /&gt;
&lt;br /&gt;
You can also have a rough search for &#039;&#039;&#039;misplaced words&#039;&#039;&#039; (it will actually work only if anything else is correct) using syntax like this:&lt;br /&gt;
   (?!&amp;lt;I\s+)\bam\b(?!\s+victor)&lt;br /&gt;
This expression catches misplaced &amp;quot;am&amp;quot; in the sentence &amp;quot;I am victor&amp;quot; by first looking for &amp;quot;am&amp;quot; doens&#039;t have &amp;quot;I&amp;quot; before it (&amp;quot;(?!&amp;lt;I\s+)&amp;quot; part) and then &amp;quot;victor&amp;quot; after it (&amp;quot;(?!\s+victor)&amp;quot; part). &amp;quot;\s+&amp;quot; allows any number of spaces between words. If you want to catch the first (last) word (punctuation mark, etc) - then you should place simple assertions for start/end of string (&amp;quot;^&amp;quot; or &amp;quot;$&amp;quot;) instead of words in related assertions. For instance to look for misplaced &amp;quot;I&amp;quot; you should write something like&lt;br /&gt;
   (?!&amp;lt;^)\bI\b(?!\s+am)&lt;br /&gt;
which looks for &amp;quot;I&amp;quot; that is not preceded by start of the string and not followed by &amp;quot;am&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Note, that if you have several answers to catch missing and misplaced things, only one will actually work for any given student response.&lt;br /&gt;
&lt;br /&gt;
NFA and DFA matchers for now don&#039;t supports complex assertions, used by these regexes. Since the Preg 2.3 release you can combine hints and catching missing words. But you should be sure that the answers that look for missing things (and other to give specific feedback) have a &#039;&#039;&#039;fraction&#039;&#039;&#039; (grade) lower, that &#039;&#039;&#039;hint grade border&#039;&#039;&#039; (see [[#Hinting]]). You actually don&#039;t want to generate hints for these answers, as they don&#039;t define a correct situation, so it&#039;s no restriction actually.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions in main (hinting) regular expressions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve 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 the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99843</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99843"/>
		<updated>2012-08-06T19:36:28Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Hinting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &lt;br /&gt;
#*&#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
#* If you have too many characters that need escaping in some fragment, you could use &#039;&#039;&#039;\Q ... \E&#039;&#039;&#039; sequence instead. Anything between \Q and \E is treated literally as characters:&lt;br /&gt;
#** &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#** &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
# &#039;&#039;&#039;Character sets&#039;&#039;&#039; match any character defined in them. Character sets are defined by brackets. The particular ways to define a character set are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; and &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot.&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets:&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands:&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
# &#039;&#039;&#039;Unicode properties&#039;&#039;&#039; are special escape-sequences &amp;quot;\p{xx}&amp;quot; (positive) or &amp;quot;\P{xx}&amp;quot; (negative) for matching specific unicode characters (the complete list of &amp;quot;xx&amp;quot; variations can be found at found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm):&lt;br /&gt;
#* &amp;quot;\p{Ll}&amp;quot; matches any lowercase letter;&lt;br /&gt;
#* &amp;quot;\P{Lu}&amp;quot; matches any non-uppercase letter.&lt;br /&gt;
# &#039;&#039;&#039;POSIX character classes&#039;&#039;&#039; are used for the same purpose as unicode properties (and complete list of them can be found on the Internet too), but may not work with non-ASCII characters. They are allowed only inside character sets:&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:alnum:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any alpha-numeric character;&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:^digit:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any non-digit chararcter.&lt;br /&gt;
All characters and escape sequences work both inside and outside character sets, but POSIX classes are allowed only inside character sets [...]. Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;&#039;remember&#039;&#039; substrings captured by the regex. The simplest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplеst syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
# &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
# &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
# &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
# &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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;
&lt;br /&gt;
=====Next character hinting=====&lt;br /&gt;
When next character hinting is available, student will have the &#039;&#039;&#039;hint next character&#039;&#039;&#039; button by pressing which he receives 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;
&lt;br /&gt;
=====Next lexem hinting=====&lt;br /&gt;
&#039;&#039;&#039;Lexem&#039;&#039;&#039; means an atomic part of the language. For natural language a &#039;&#039;word&#039;&#039;, a &#039;&#039;number&#039;&#039;, a &#039;&#039;punctuation mark&#039;&#039; (or group of marks like &#039;?!&#039; or &#039;...&#039;) are lexemes. For a programming language it&#039;s a &#039;&#039;keyword&#039;&#039;, a &#039;&#039;variable name&#039;&#039;, a &#039;&#039;constant&#039;&#039;, an &#039;&#039;operator&#039;&#039;. Note that spaces are usually don&#039;t considered to be lexems, but a separators between them, since they don&#039;t have any particular meaning.&lt;br /&gt;
&lt;br /&gt;
Next lexem hint will show to the student either completion of current lexem (if partial match ends inside it) or next one (if student just complete current lexem). Like &lt;br /&gt;
   &amp;lt;span style=&amp;quot;color:#0000FF;&amp;quot;&amp;gt;are bl&amp;lt;/span&amp;gt;&amp;lt;span style=&amp;quot;background-color:#00FF00&amp;quot;&amp;gt;ue&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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;,&amp;lt;/span&amp;gt;&lt;br /&gt;
or&lt;br /&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; white&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Preg question type since 2.3 version allows use of next lexem hinting using&#039;&#039; formal languages block&#039;&#039;. You should choose the language you use, since lexem borders are different for different languages. For now it supports only two languages, but there will be more:&lt;br /&gt;
* simple english - a simple lexer, that recognize words, numbers and punctuation;&lt;br /&gt;
* C/C++ language - a programming language C (or C++).&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t a word you would like you students to see on the hinting button. You could enter another&lt;br /&gt;
&lt;br /&gt;
=====General hinting rules=====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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; (leading to the &amp;quot; and&amp;quot; path). The question will choose &#039;,&#039; since it leads to the shortest path to complete the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* empty parentheses of any sort &amp;quot;(?=)&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;quot;[[:hamster:]]&amp;quot;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect ranges for quantifiers &amp;quot;a{5,4}&amp;quot; or character sets &amp;quot;[z-a]&amp;quot;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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 and misplaced things====&lt;br /&gt;
Joseph Rezeau&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s REGEX question type!&lt;br /&gt;
&lt;br /&gt;
You can also have a rough search for &#039;&#039;&#039;misplaced words&#039;&#039;&#039; (it will actually work only if anything else is correct) using syntax like this:&lt;br /&gt;
   (?!&amp;lt;I\s+)\bam\b(?!\s+victor)&lt;br /&gt;
This expression catches misplaced &amp;quot;am&amp;quot; in the sentence &amp;quot;I am victor&amp;quot; by first looking for &amp;quot;am&amp;quot; doens&#039;t have &amp;quot;I&amp;quot; before it (&amp;quot;(?!&amp;lt;I\s+)&amp;quot; part) and then &amp;quot;victor&amp;quot; after it (&amp;quot;(?!\s+victor)&amp;quot; part). &amp;quot;\s+&amp;quot; allows any number of spaces between words. If you want to catch the first (last) word (punctuation mark, etc) - then you should place simple assertions for start/end of string (&amp;quot;^&amp;quot; or &amp;quot;$&amp;quot;) instead of words in related assertions. For instance to look for misplaced &amp;quot;I&amp;quot; you should write something like&lt;br /&gt;
   (?!&amp;lt;^)\bI\b(?!\s+am)&lt;br /&gt;
which looks for &amp;quot;I&amp;quot; that is not preceded by start of the string and not followed by &amp;quot;am&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Note, that if you have several answers to catch missing and misplaced things, only one will actually work for any given student response.&lt;br /&gt;
&lt;br /&gt;
NFA and DFA matchers for now don&#039;t supports complex assertions, used by these regexes. Since the Preg 2.3 release you can combine hints and catching missing words. But you should be sure that the answers that look for missing things (and other to give specific feedback) have a &#039;&#039;&#039;fraction&#039;&#039;&#039; (grade) lower, that &#039;&#039;&#039;hint grade border&#039;&#039;&#039; (see [[#Hinting]]). You actually don&#039;t want to generate hints for these answers, as they don&#039;t define a correct situation, so it&#039;s no restriction actually.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions in main (hinting) regular expressions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve 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 the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99840</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99840"/>
		<updated>2012-08-06T19:04:26Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Non-deterministing finite state automata(NFA) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &lt;br /&gt;
#*&#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
#* If you have too many characters that need escaping in some fragment, you could use &#039;&#039;&#039;\Q ... \E&#039;&#039;&#039; sequence instead. Anything between \Q and \E is treated literally as characters:&lt;br /&gt;
#** &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#** &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
# &#039;&#039;&#039;Character sets&#039;&#039;&#039; match any character defined in them. Character sets are defined by brackets. The particular ways to define a character set are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; and &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot.&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets:&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands:&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
# &#039;&#039;&#039;Unicode properties&#039;&#039;&#039; are special escape-sequences &amp;quot;\p{xx}&amp;quot; (positive) or &amp;quot;\P{xx}&amp;quot; (negative) for matching specific unicode characters (the complete list of &amp;quot;xx&amp;quot; variations can be found at found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm):&lt;br /&gt;
#* &amp;quot;\p{Ll}&amp;quot; matches any lowercase letter;&lt;br /&gt;
#* &amp;quot;\P{Lu}&amp;quot; matches any non-uppercase letter.&lt;br /&gt;
# &#039;&#039;&#039;POSIX character classes&#039;&#039;&#039; are used for the same purpose as unicode properties (and complete list of them can be found on the Internet too), but may not work with non-ASCII characters. They are allowed only inside character sets:&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:alnum:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any alpha-numeric character;&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:^digit:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any non-digit chararcter.&lt;br /&gt;
All characters and escape sequences work both inside and outside character sets, but POSIX classes are allowed only inside character sets [...]. Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;&#039;remember&#039;&#039; substrings captured by the regex. The simplest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplеst syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
# &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
# &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
# &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
# &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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 the &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receives 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 type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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 the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* empty parentheses of any sort &amp;quot;(?=)&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;quot;[[:hamster:]]&amp;quot;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect ranges for quantifiers &amp;quot;a{5,4}&amp;quot; or character sets &amp;quot;[z-a]&amp;quot;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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 and misplaced things====&lt;br /&gt;
Joseph Rezeau&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s REGEX question type!&lt;br /&gt;
&lt;br /&gt;
You could also have a rough search for &#039;&#039;&#039;misplaced words&#039;&#039;&#039; (it will actually work only if anything else would be correct) using syntax like this:&lt;br /&gt;
   (?!&amp;lt;I\s+)\bam\b(?!\s+victor)&lt;br /&gt;
This expression catches misplaced &amp;quot;am&amp;quot; in the sentence &amp;quot;I am victor&amp;quot;, looking for &amp;quot;am&amp;quot; doens&#039;t have &amp;quot;I&amp;quot; before (&amp;quot;(?!&amp;lt;I\s+)&amp;quot; part) and &amp;quot;victor&amp;quot; after (&amp;quot;(?!\s+victor)&amp;quot; part) it. &amp;quot;\s+&amp;quot; allows any number of spaces between words. If you want to catch first (last) word (punctuation mark, etc) - then you should place simple assertions for start/end of string (&amp;quot;^&amp;quot; or &amp;quot;$&amp;quot;) instead of words in related assertions. For instance to look for misplaced &amp;quot;I&amp;quot; you should write something like&lt;br /&gt;
   (?!&amp;lt;^)\bI\b(?!\s+am)&lt;br /&gt;
which look for &amp;quot;I&amp;quot; that is not preceded by start of the string and not followed by &amp;quot;am&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Note, that if you have several answers to catch missing and misplaced things, only one will actually works for any given student response.&lt;br /&gt;
&lt;br /&gt;
NFA and DFA matchers for now don&#039;t supports complex assertions, used by these regexes. Since of Preg 2.3 you could combine hints and catching missing words. All you should provide is that answers that looks for missing things (and other to give specific feedback) have a &#039;&#039;&#039;fraction&#039;&#039;&#039; (grade) lower, that &#039;&#039;&#039;hint grade border&#039;&#039;&#039; (see [[#Hinting]]). You actually don&#039;t want to generate hints from these answers, as they don&#039;t define correct situation, so it&#039;s no restriction actually.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions in main (hinting) regular expressions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve 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 the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99839</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99839"/>
		<updated>2012-08-06T19:03:29Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Looking for missing things */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &lt;br /&gt;
#*&#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
#* If you have too many characters that need escaping in some fragment, you could use &#039;&#039;&#039;\Q ... \E&#039;&#039;&#039; sequence instead. Anything between \Q and \E is treated literally as characters:&lt;br /&gt;
#** &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#** &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
# &#039;&#039;&#039;Character sets&#039;&#039;&#039; match any character defined in them. Character sets are defined by brackets. The particular ways to define a character set are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; and &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot.&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets:&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands:&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
# &#039;&#039;&#039;Unicode properties&#039;&#039;&#039; are special escape-sequences &amp;quot;\p{xx}&amp;quot; (positive) or &amp;quot;\P{xx}&amp;quot; (negative) for matching specific unicode characters (the complete list of &amp;quot;xx&amp;quot; variations can be found at found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm):&lt;br /&gt;
#* &amp;quot;\p{Ll}&amp;quot; matches any lowercase letter;&lt;br /&gt;
#* &amp;quot;\P{Lu}&amp;quot; matches any non-uppercase letter.&lt;br /&gt;
# &#039;&#039;&#039;POSIX character classes&#039;&#039;&#039; are used for the same purpose as unicode properties (and complete list of them can be found on the Internet too), but may not work with non-ASCII characters. They are allowed only inside character sets:&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:alnum:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any alpha-numeric character;&lt;br /&gt;
#* &amp;lt;nowiki&amp;gt;&amp;quot;[[:^digit:]]&amp;quot;&amp;lt;/nowiki&amp;gt; matches any non-digit chararcter.&lt;br /&gt;
All characters and escape sequences work both inside and outside character sets, but POSIX classes are allowed only inside character sets [...]. Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;&#039;remember&#039;&#039; substrings captured by the regex. The simplest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplеst syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
# &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
# &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
# &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
# &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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 the &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receives 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 type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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 the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* empty parentheses of any sort &amp;quot;(?=)&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;quot;[[:hamster:]]&amp;quot;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect ranges for quantifiers &amp;quot;a{5,4}&amp;quot; or character sets &amp;quot;[z-a]&amp;quot;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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 and misplaced things====&lt;br /&gt;
Joseph Rezeau&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s REGEX question type!&lt;br /&gt;
&lt;br /&gt;
You could also have a rough search for &#039;&#039;&#039;misplaced words&#039;&#039;&#039; (it will actually work only if anything else would be correct) using syntax like this:&lt;br /&gt;
   (?!&amp;lt;I\s+)\bam\b(?!\s+victor)&lt;br /&gt;
This expression catches misplaced &amp;quot;am&amp;quot; in the sentence &amp;quot;I am victor&amp;quot;, looking for &amp;quot;am&amp;quot; doens&#039;t have &amp;quot;I&amp;quot; before (&amp;quot;(?!&amp;lt;I\s+)&amp;quot; part) and &amp;quot;victor&amp;quot; after (&amp;quot;(?!\s+victor)&amp;quot; part) it. &amp;quot;\s+&amp;quot; allows any number of spaces between words. If you want to catch first (last) word (punctuation mark, etc) - then you should place simple assertions for start/end of string (&amp;quot;^&amp;quot; or &amp;quot;$&amp;quot;) instead of words in related assertions. For instance to look for misplaced &amp;quot;I&amp;quot; you should write something like&lt;br /&gt;
   (?!&amp;lt;^)\bI\b(?!\s+am)&lt;br /&gt;
which look for &amp;quot;I&amp;quot; that is not preceded by start of the string and not followed by &amp;quot;am&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Note, that if you have several answers to catch missing and misplaced things, only one will actually works for any given student response.&lt;br /&gt;
&lt;br /&gt;
NFA and DFA matchers for now don&#039;t supports complex assertions, used by these regexes. Since of Preg 2.3 you could combine hints and catching missing words. All you should provide is that answers that looks for missing things (and other to give specific feedback) have a &#039;&#039;&#039;fraction&#039;&#039;&#039; (grade) lower, that &#039;&#039;&#039;hint grade border&#039;&#039;&#039; (see [[#Hinting]]). You actually don&#039;t want to generate hints from these answers, as they don&#039;t define correct situation, so it&#039;s no restriction actually.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve 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 the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99836</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99836"/>
		<updated>2012-08-06T18:43:11Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Regular expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &lt;br /&gt;
#*&#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
#* If you have too many characters that needs escaping in some fragment, you could use &#039;&#039;&#039;\Q ... \E&#039;&#039;&#039; sequence instead. Anything between \Q and \E is treated literally as characters:&lt;br /&gt;
#** &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#** &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
# &#039;&#039;&#039;Character sets&#039;&#039;&#039; match any character defined in them. Character sets are defined by brackets. The particular ways to define a character set are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; and &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot.&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets:&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands:&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
# &#039;&#039;&#039;Unicode properties&#039;&#039;&#039; are special escape-sequences &amp;quot;\p{xx}&amp;quot; (positive) or &amp;quot;\P{xx}&amp;quot; (negative) for matching specific unicode characters (the complete list of &amp;quot;xx&amp;quot; variations can be found at found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm):&lt;br /&gt;
#* &amp;quot;\p{Ll}&amp;quot; matches any lowercase letter;&lt;br /&gt;
#* &amp;quot;\P{Lu}&amp;quot; matches any non-uppercase letter;&lt;br /&gt;
Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;&#039;remember&#039;&#039; substrings captured by the regex. The simplest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplеst syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
# &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
# &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
# &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
# &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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 the &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receives 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 type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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 the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* empty parentheses of any sort &amp;quot;(?=)&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;quot;[[:hamster:]]&amp;quot;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect ranges for quantifiers &amp;quot;a{5,4}&amp;quot; or character sets &amp;quot;[z-a]&amp;quot;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s 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 requires some complex work.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve 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 the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99835</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99835"/>
		<updated>2012-08-06T18:36:36Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Subpatterns and backreferences */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
# &#039;&#039;&#039;Character sets&#039;&#039;&#039; match any character defined in them. Character sets are defined by brackets. The particular ways to define a character set are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; and &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot.&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets:&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands:&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
# &#039;&#039;&#039;Unicode properties&#039;&#039;&#039; are special escape-sequences &amp;quot;\p{xx}&amp;quot; (positive) or &amp;quot;\P{xx}&amp;quot; (negative) for matching specific unicode characters (the complete list of &amp;quot;xx&amp;quot; variations can be found at found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm):&lt;br /&gt;
#* &amp;quot;\p{Ll}&amp;quot; matches any lowercase letter;&lt;br /&gt;
#* &amp;quot;\P{Lu}&amp;quot; matches any non-uppercase letter;&lt;br /&gt;
Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
# The &#039;&#039;&#039;\Q...\E&#039;&#039;&#039; sequence (recognized both inside and outside character sets) is used for quoting substrings. Characters in between are treated as literals:&lt;br /&gt;
#* &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#* &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;&#039;remember&#039;&#039; substrings captured by the regex. The simplest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplеst syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
# &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
# &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
# &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
# &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
# &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
# &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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 the &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receives 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 type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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 the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* empty parentheses of any sort &amp;quot;(?=)&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;quot;[[:hamster:]]&amp;quot;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect ranges for quantifiers &amp;quot;a{5,4}&amp;quot; or character sets &amp;quot;[z-a]&amp;quot;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s 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 requires some complex work.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve 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 the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99834</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99834"/>
		<updated>2012-08-06T18:36:02Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Subpatterns and backreferences */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
# &#039;&#039;&#039;Character sets&#039;&#039;&#039; match any character defined in them. Character sets are defined by brackets. The particular ways to define a character set are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; and &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot.&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets:&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands:&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
# &#039;&#039;&#039;Unicode properties&#039;&#039;&#039; are special escape-sequences &amp;quot;\p{xx}&amp;quot; (positive) or &amp;quot;\P{xx}&amp;quot; (negative) for matching specific unicode characters (the complete list of &amp;quot;xx&amp;quot; variations can be found at found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm):&lt;br /&gt;
#* &amp;quot;\p{Ll}&amp;quot; matches any lowercase letter;&lt;br /&gt;
#* &amp;quot;\P{Lu}&amp;quot; matches any non-uppercase letter;&lt;br /&gt;
Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
# The &#039;&#039;&#039;\Q...\E&#039;&#039;&#039; sequence (recognized both inside and outside character sets) is used for quoting substrings. Characters in between are treated as literals:&lt;br /&gt;
#* &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#* &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;&#039;remember&#039;&#039; substrings captured by the regex. The simplest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplеst syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
#* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
#* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
#* &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
#* &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
#* &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
#* &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
#* &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
#* &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
#* &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
#* &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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 the &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receives 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 type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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 the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* empty parentheses of any sort &amp;quot;(?=)&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;quot;[[:hamster:]]&amp;quot;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect ranges for quantifiers &amp;quot;a{5,4}&amp;quot; or character sets &amp;quot;[z-a]&amp;quot;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s 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 requires some complex work.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve 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 the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99831</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=99831"/>
		<updated>2012-08-06T18:34:22Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Duplicate subpattern numbers and names */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Preg question type is a question type that uses regular expressions (regexes) to check student&#039;s responses. Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. This documentation contains a part about expressions in general, a part about the regular expressions as a particular case of expressions and finally a part about the Preg question type itself. If you are familiar with the regex syntax you may skip first parts and go to the [[#Usage of the Preg question type|usage of the Preg question type]] section. More details about the regex syntax can be found at 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 and behaviours code, regex parsing and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore, subpatterns, backreferences and unicode support - Valeriy Streltsov.&lt;br /&gt;
We would gladly accept testers and contributors (see the [[#Development plans|development plans]] section) - there is still more work to be done than we have time. Thanks to Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that was implemented in Preg question type.&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. 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;: &#039;+&#039; and &#039;*&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;*&#039; are &#039;y&#039; and &#039;2&#039;. The &#039;&#039;&#039;operands&#039;&#039;&#039; of &#039;+&#039; are &#039;x&#039; and the result of &#039;y*2&#039;. Easy?&lt;br /&gt;
&lt;br /&gt;
Thinking about that expression deeper we can find that there is a definite &#039;&#039;&#039;order of evaluation&#039;&#039;&#039;, governed by operator&#039;s &#039;&#039;&#039;precedence&#039;&#039;&#039;. The &#039;*&#039; has a precedence over &#039;+&#039;, so it is evaluated first. You can change the evaluation order by using parentheses: &#039;&#039;&#039;(x+y)*2&#039;&#039;&#039; will evaluate &#039;+&#039; first and multiply the result by 2. Still easy?&lt;br /&gt;
&lt;br /&gt;
One more thing we should learn about operators is their &#039;&#039;&#039;arity&#039;&#039;&#039; - this is just the number of operands required. In the example above &#039;+&#039; and &#039;*&#039; are &#039;&#039;&#039;binary&#039;&#039;&#039; operators - they both take two operands. Most of arithmetic operators are binary, but the minus has the &#039;&#039;&#039;unary&#039;&#039;&#039; (single operand) form, like in this equation: &#039;&#039;&#039;y=-x&#039;&#039;&#039;. Note that the unary and binary minuses work differently. &lt;br /&gt;
&lt;br /&gt;
Now any epxression are just a 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 (arity), taking heed of their evaluation order by using their &#039;&#039;&#039;precedence&#039;&#039;&#039; and parentheses. Arithmetic expressions are for evaluating numbers. Regular expressions are for finding patterns in 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;
Regular expressions is a powerful mechanism for searching in strings using patterns. So their &#039;&#039;&#039;operands&#039;&#039;&#039; are characters or character sets. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The ways to define character sets are described below. The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash. These special characters are:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Mathematical expressions never have escaping problems since their operands (numbers, variables) are constructed from different characters than operators (+,- etc), but when constructing a pattern for matching you should be able to use &#039;&#039;any&#039;&#039; character as an operand.&lt;br /&gt;
&lt;br /&gt;
====Operands====&lt;br /&gt;
Here&#039;s an incomplete list of operands that define character sets.&lt;br /&gt;
# &#039;&#039;&#039;Simple characters&#039;&#039;&#039; (with no special meaning) match themselves.&lt;br /&gt;
# &#039;&#039;&#039;Escaped special characters&#039;&#039;&#039; match corresponding special characters. Escaping means preceding special characters by the backslash &amp;quot;\&amp;quot;. For example, the regex &amp;quot;\|&amp;quot; matches the string &amp;quot;|&amp;quot;, the regex &amp;quot;a\*b\[&amp;quot; matches the string &amp;quot;a*b[&amp;quot;. Backslash is a special character too and should be escaped: &amp;quot;\\&amp;quot; matches &amp;quot;\&amp;quot;. &#039;&#039;&#039;NOTE!&#039;&#039;&#039; when you are &#039;&#039;unsure&#039;&#039; whether to escape some character, it is safe to place &amp;quot;\&amp;quot; before any character except letters and digits. &#039;&#039;Do not&#039;&#039; escape letters and digits unless you know what you are doing - they get special meaning when escaped and lose it when not.&lt;br /&gt;
# &#039;&#039;&#039;Character sets&#039;&#039;&#039; match any character defined in them. Character sets are defined by brackets. The particular ways to define a character set are:&lt;br /&gt;
#* &amp;quot;[ab,!]&amp;quot; matches &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;,&amp;quot; and &amp;quot;!&amp;quot;;&lt;br /&gt;
#* &amp;quot;[a-szC-F0-9]&amp;quot; contains ranges (defined by a &#039;&#039;hyphen between 2 characters&#039;&#039;) &amp;quot;a-z&amp;quot;, &amp;quot;C-F&amp;quot; and &amp;quot;0-9&amp;quot; mixed with the single character &amp;quot;z&amp;quot;, it matches any character from &amp;quot;a&amp;quot; to &amp;quot;s&amp;quot;, &amp;quot;z&amp;quot;, from &amp;quot;C to &amp;quot;F&amp;quot; and from &amp;quot;0&amp;quot; to &amp;quot;9&amp;quot;;&lt;br /&gt;
#* &amp;quot;[^a-z-]&amp;quot; starts with the &amp;quot;^&amp;quot; that means a &#039;&#039;&#039;negative character set&#039;&#039;&#039;: it matches any character except from &amp;quot;a&amp;quot; to &amp;quot;z&amp;quot; and &amp;quot;-&amp;quot; (note that the second hyphen is not placed between 2 characters so defines itself);&lt;br /&gt;
#* &amp;quot;[\-\]\\]&amp;quot; contains &#039;&#039;escaping inside a character set&#039;&#039;:  it matches &amp;quot;-&amp;quot;, &amp;quot;]&amp;quot; and &amp;quot;\&amp;quot;, other characters loose their special meaning inside a character set and can be be not escaped, but if you want to include &amp;quot;^&amp;quot; in a character set it shouldn&#039;t be first there;&lt;br /&gt;
# &#039;&#039;&#039;Dot meta-character&#039;&#039;&#039; (&amp;quot;.&amp;quot;) matches &#039;&#039;any&#039;&#039; possible character (except newline, but students can&#039;t enter it anywhere), escape it &amp;quot;\.&amp;quot; if you need to match a single dot.&lt;br /&gt;
# &#039;&#039;&#039;Escape sequences&#039;&#039;&#039; for common character sets:&lt;br /&gt;
#* &amp;quot;\w&amp;quot; for any word character (letter, underscore or digit) and &amp;quot;\W&amp;quot; for any non-word character;&lt;br /&gt;
#* &amp;quot;\s&amp;quot; for any space character and &amp;quot;\S&amp;quot; for any non-space character;&lt;br /&gt;
#* &amp;quot;\d&amp;quot; for any digit and &amp;quot;\D&amp;quot; for any non-digit.&lt;br /&gt;
# &#039;&#039;&#039;Simple assertions&#039;&#039;&#039; - they are not characters, but conditions to test, they &#039;&#039;don&#039;t consume&#039;&#039; characters while matching, unlike other operands:&lt;br /&gt;
#* &amp;quot;^&amp;quot; matches in the start of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;$&amp;quot; matches in the end of the string, fails otherwise;&lt;br /&gt;
#* &amp;quot;\b&amp;quot; matches on a word boundary, i.e. either between word (\w) and non-word (\W) characters, or in the start (end) of the string if it starts (ends) with a word character;&lt;br /&gt;
#* &amp;quot;\B&amp;quot; matches not on a word boundary, negative to &amp;quot;\b&amp;quot;.&lt;br /&gt;
Still, a pattern that matches only one character isn&#039;t very useful. So here comes the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression which matches strings of several characters.&lt;br /&gt;
&lt;br /&gt;
====Operators====&lt;br /&gt;
Here&#039;s a list of the common regex operators:&lt;br /&gt;
# &#039;&#039;&#039;Concatenation&#039;&#039;&#039; - so simple &#039;&#039;binary&#039;&#039; operator that doesn&#039;t require any special character to be defined. It is still 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;
#* &amp;quot;ab&amp;quot; matches &amp;quot;ab&amp;quot;;&lt;br /&gt;
#* &amp;quot;a[0-9]&amp;quot; matches &amp;quot;a&amp;quot; followed by any digit, for example, &amp;quot;a5&amp;quot;&lt;br /&gt;
# &#039;&#039;&#039;Alternative&#039;&#039;&#039; - a &#039;&#039;binary&#039;&#039; operator that lets you define a set of alternatives:&lt;br /&gt;
#* &amp;quot;a|b&amp;quot; matches &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;ab|cd|&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &#039;&#039;emptiness&#039;&#039; (useful as a part in more complex expressions);&lt;br /&gt;
#* &amp;quot;(aa|bb)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; - using parentheses to outline alternative set;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical usage of the emptiness;&lt;br /&gt;
# &#039;&#039;&#039;Quantifiers&#039;&#039;&#039; - an &#039;&#039;unary&#039;&#039; operator that lets you define repetition of something used as its operand:&lt;br /&gt;
#* &amp;quot;x*&amp;quot; matches &amp;quot;x&amp;quot; zero or more times;&lt;br /&gt;
#* &amp;quot;x+&amp;quot; matches &amp;quot;x&amp;quot; one or more times;&lt;br /&gt;
#* &amp;quot;x?&amp;quot; matches &amp;quot;x&amp;quot; zero or one times;&lt;br /&gt;
#* &amp;quot;x{2,4}&amp;quot; matches &amp;quot;x&amp;quot; from 2 to 4 times;&lt;br /&gt;
#* &amp;quot;x{2,}&amp;quot; matches &amp;quot;x&amp;quot; two or more times;&lt;br /&gt;
#* &amp;quot;x{,2}&amp;quot; matches &amp;quot;x&amp;quot; from 0 to 2 times;&lt;br /&gt;
#* &amp;quot;x{2}&amp;quot; matches &amp;quot;x&amp;quot; exactly 2 times;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times, i.e. if you want to use a quantifier on more than one character, you should use parentheses;&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, i.e. it is a repeated alternative, not a repetition of &amp;quot;a&amp;quot; or &amp;quot;b&amp;quot;.&lt;br /&gt;
# The &#039;&#039;&#039;\Q...\E&#039;&#039;&#039; sequence (recognized both inside and outside character sets) is used for quoting substrings. Characters in between are treated as literals:&lt;br /&gt;
#* &amp;quot;\Q^(abc)$\E.&amp;quot; matches &amp;quot;^(abc)$&amp;quot; followed by any character - there are NO simple assertions and subpatterns;&lt;br /&gt;
#* &amp;quot;\Q^(abc)$.&amp;quot; matches &amp;quot;^(abc)$.&amp;quot; because there is no &amp;quot;\E&amp;quot; and all characters after &amp;quot;\Q&amp;quot; are treated as literals till the end of the regex.&lt;br /&gt;
&lt;br /&gt;
====Precedence and order of evaluation====&lt;br /&gt;
A &#039;&#039;&#039;Quantifier&#039;&#039;&#039; has precedence &#039;&#039;&#039;over concatenation&#039;&#039;&#039; and &#039;&#039;&#039;concatenation&#039;&#039;&#039; has precedence &#039;&#039;&#039;over alternative&#039;&#039;&#039;. Let&#039;s look what it means:&lt;br /&gt;
# &#039;&#039;quantifiers over concatenation&#039;&#039; means that quantifiers are executed first and will repeat only a single character if used without parentheses:&lt;br /&gt;
#* &amp;quot;ab*&amp;quot; matches &amp;quot;a&amp;quot; followed by zero or more &amp;quot;b&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab)*&amp;quot; matches &amp;quot;ab&amp;quot; zero or more times - changing the previous regex by using parentheses allows us define a string repetition;  &lt;br /&gt;
# &#039;&#039;concatenation over alternative&#039;&#039; means that you can define multi-character alternatives without parentheses (for single character alternatives it&#039;s better to use character classes, not the alternative operator):&lt;br /&gt;
#* &amp;quot;ab|cd|de&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot; or &amp;quot;de&amp;quot;;&lt;br /&gt;
#* &amp;quot;(aa|bb|)c&amp;quot; matches &amp;quot;aac&amp;quot; or &amp;quot;bbc&amp;quot; or &amp;quot;c&amp;quot; - typical use of an empty alternative;&lt;br /&gt;
# &#039;&#039;quantifier over alternative&#039;&#039; means that you should use parentheses to repeat an alternative set:&lt;br /&gt;
#* &amp;quot;ab|cd*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;c&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;cdddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(ab|cd)*&amp;quot; matches &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;, repeated zero or more time in any order, like &amp;quot;ababcdabcdcd&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(a|b){2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;ab&amp;quot; or &amp;quot;ba&amp;quot; or &amp;quot;bb&amp;quot;, not just &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot;;&lt;br /&gt;
#* &amp;quot;a{2}|b{2}&amp;quot; matches &amp;quot;aa&amp;quot; or &amp;quot;bb&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
====Subpatterns and backreferences====&lt;br /&gt;
&#039;&#039;&#039;Subpatterns&#039;&#039;&#039; are &#039;&#039;&#039;operators&#039;&#039;&#039; that &#039;remember&#039; substrings captured by the regex. The simpliest way to define a subpattern is to use parentheses: the regex &amp;quot;a(bc)d&amp;quot; contains a subpattern &amp;quot;bc&amp;quot;. Subpatterns are numerated from 0 for the whole regex and counted by opening parentheses. That &amp;quot;(bc)&amp;quot; subpattern is the 1st. If we write, say, &amp;quot;a(b(c)(d))e&amp;quot; - there are subpatterns &amp;quot;bcd&amp;quot; which is 1st, &amp;quot;c&amp;quot; which is 2nd and &amp;quot;d&amp;quot; which is 3rd.&lt;br /&gt;
Subpatterns are usually used with &#039;&#039;&#039;backreferences&#039;&#039;&#039; which, too, have numbers. Backreferences are &#039;&#039;&#039;operands&#039;&#039;&#039; that match the same strings which are matched by the subpatterns with the same numbers. The simplist syntax for backreferences is a slash followed by a number: &amp;quot;\1&amp;quot; means a backreference to the 1st subpattern. The regular expression &amp;quot;([ab])\1&amp;quot; matches strings &amp;quot;aa&amp;quot; and &amp;quot;bb&amp;quot;, but neither &amp;quot;ab&amp;quot; nor &amp;quot;ba&amp;quot; because the backreference should match the same character as the subpattern did.&lt;br /&gt;
Constider a little example: declaration and initialization of an integer variable in C programming language:&lt;br /&gt;
#* &amp;quot;int ([_\w][_\w\d]*); \1 = -?\d+;&amp;quot; matches, for example, &amp;quot;int _var; _var = -10;&amp;quot;. Of course, there can be any number of spaces between &amp;quot;int&amp;quot;, variable name etc, so a more correct regex will look like:&lt;br /&gt;
#* &amp;quot;\s*int\s+([_\w][_\w\d]*)\s*;\s*\1\s*=\s*-?\d+\s*;\s*&amp;quot; - this will match, say, &amp;quot;  int var2  ;     var2=123    ;  &amp;quot;. Looks a bit frightning, but it is easier to write this regex once than to try understand it after.&lt;br /&gt;
&lt;br /&gt;
Finally, instead of just numbers, subpatterns and backreferences can have names via a little more complicated syntax:&lt;br /&gt;
#* &amp;quot;(?&amp;lt;name1&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name1&amp;quot;;&lt;br /&gt;
#* &amp;quot;(?&#039;name2&#039;...)&amp;quot; means a subpattern with name &amp;quot;name2&amp;quot;;&lt;br /&gt;
#* &amp;quot;(?P&amp;lt;name3&amp;gt;...)&amp;quot; means a subpattern with name &amp;quot;name3&amp;quot;;&lt;br /&gt;
#* &amp;quot;\k&amp;lt;name4&amp;gt;&amp;quot; means a backreference to the subpattern named &amp;quot;name4&amp;quot;;&lt;br /&gt;
#* &amp;quot;\k&#039;name5&#039;&amp;quot; means a backreference to the subpattern named &amp;quot;name5&amp;quot;;&lt;br /&gt;
#* &amp;quot;\g{name6}&amp;quot; means a backreference to the subpattern named &amp;quot;name6&amp;quot;;&lt;br /&gt;
#* &amp;quot;\k{name7}&amp;quot; means a backreference to the subpattern named &amp;quot;name7&amp;quot;;&lt;br /&gt;
#* &amp;quot;(?P=name8)&amp;quot; means a backreference to the subpattern named &amp;quot;name8&amp;quot;.&lt;br /&gt;
This is very useful when you work with complicated regexes and often modify it by adding or removing subpatterns - names stay the same.&lt;br /&gt;
&lt;br /&gt;
=====Duplicate subpattern numbers and names=====&lt;br /&gt;
There is a useful syntax when combining subpatterns with alternation. If you create a group &amp;quot;(?|...)&amp;quot; than every alternative inside that group will have the same subpattern numeration. Consider the regex &amp;quot;(?|(a(b))|(c(d)))&amp;quot; - there are 2 alternatives with 2 subpatterns in each. Subpatterns &amp;quot;ab&amp;quot; and &amp;quot;cd&amp;quot; are 1st ones, &amp;quot;b&amp;quot; and &amp;quot;d&amp;quot; are 2nd ones.&lt;br /&gt;
&lt;br /&gt;
====Assertions====&lt;br /&gt;
Assertions about some part of the string don&#039;t actually go into matching text, but affect the matching occurrence:&lt;br /&gt;
* &#039;&#039;&#039;positive lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?=b)&amp;quot; matches any number of &amp;quot;a&amp;quot; ending with &amp;quot;b&amp;quot; without including &amp;quot;b&amp;quot; in the match;&lt;br /&gt;
* &#039;&#039;&#039;negative lookahead assertion&#039;&#039;&#039; &amp;quot;a+(?!b)&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not followed by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;positive lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;=b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; preceeded by &amp;quot;b&amp;quot;;&lt;br /&gt;
* &#039;&#039;&#039;negative lookbehind assertion&#039;&#039;&#039; &amp;quot;(?&amp;lt;!b)a+&amp;quot; matches any number of &amp;quot;a&amp;quot; that is not preceeded by &amp;quot;b&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
====Matching====&lt;br /&gt;
Matching means finding a part of the student&#039;s answer that suits the regular expression. This part called &#039;&#039;&#039;match&#039;&#039;&#039;. You should enter regular expressions as &#039;&#039;&#039;answers&#039;&#039;&#039; to questions without modifiers or enclosing characters (modifiers will be added for you by the question - &amp;quot;u&amp;quot; is added always and &amp;quot;i&amp;quot; is added in case-insensitive mode). You should also enter one correct response (that matches at least one 100% grade regex) to be shown to the student as &#039;&#039;&#039;correct answer&#039;&#039;&#039;. The question will 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]]) then a partial match that is the shortest to complete will be choosen (for displaying a hint, zero grade is given) - or the longest one, if engine can&#039;t tell which one will be the shortest to complete.&lt;br /&gt;
&lt;br /&gt;
====Anchoring====&lt;br /&gt;
Anchoring is used to set restrictions on the matching process by using simple assertions:&lt;br /&gt;
* if a regular expression starts with the &#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 expression ends with the &#039;&#039;&#039;$&#039;&#039;&#039; the match should end at the end of the student&#039;s reponse;&lt;br /&gt;
* otherwise a regex match can be found anywhere inside a student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
Note that simple assertions are concatenated with regex and concatenation has precedence over alternative, this makes it&#039;s usage slightly tricky:&lt;br /&gt;
* &amp;quot;^ab|cd$&amp;quot; will match &amp;quot;ab&amp;quot; from the start of the string or &amp;quot;cd&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(ab|cd)$&amp;quot; using brackets to match exactly with &amp;quot;ab&amp;quot; or &amp;quot;cd&amp;quot;;&lt;br /&gt;
* &amp;quot;^ab$|^cd$&amp;quot; is another way to get exact match (all top-level alternatives are anchored).&lt;br /&gt;
&lt;br /&gt;
If you set the &#039;&#039;&#039;exact matching&#039;&#039;&#039; options to &amp;quot;yes&amp;quot; (which is the default value), the question will add ^ and $ in each regular expression for you (it will not affect subpattern usage). However, you may prefer to use some non-anchored regexes to catch common errors and give feedback and use manually anchored expressions for grading.&lt;br /&gt;
&lt;br /&gt;
====Local case-sensitivity modifiers====&lt;br /&gt;
Starting from Preg 2.1 you can set case-(in)sensitivity for parts of your regular expressions by using the standard syntax of Perl-compatible regular expressions:&lt;br /&gt;
* &amp;quot;(?i)&amp;quot; will turn case-sensitivity off;&lt;br /&gt;
* &amp;quot;(?-i)&amp;quot; will turn case-sensitivity on.&lt;br /&gt;
This affects general case-sensitivity, which is choosen on the question level. So you can make some answers case-sensitive and some not, or even do this for the parts of answers. For example you can set question as &amp;quot;use case&amp;quot; and have a 50% answer starting with &amp;quot;(?i)&amp;quot; to grade lesser when the case doesn&#039;t match, but everything else is correct.&lt;br /&gt;
&lt;br /&gt;
When placed in parentheses, local modifiers work up to the closest &amp;quot;)&amp;quot;. When placed on the top level (not inside parentheses) they work up to the end of the expression, i.e. with case sensitivity on for the question:&lt;br /&gt;
* &amp;quot;abc(de(?i)&#039;&#039;&#039;gh&#039;&#039;&#039;)xyz&amp;quot; will have the bold part case-insensitive;&lt;br /&gt;
* &amp;quot;abc(de)(?i)&#039;&#039;&#039;ghxyz&#039;&#039;&#039;&amp;quot; will have the bold part case-insensitive.&lt;br /&gt;
&lt;br /&gt;
===Usage of the Preg question type===&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Starting from Preg 2.1, the &amp;quot;notations&amp;quot; feature allows you to choose a notation in which regexes for answers will be written. The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. The exciting part of notations is that you can use the Preg question type just as improved shortanswer, having access to the hinting without any need to understand regular expressions! Just choose the &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA or DFA engine you can get access to the hinting. You can skip all that is said on regular expression topic there, but be sure to read the [[#Hinting|hinting]] section to understand various settings you can alter to configure you question hinting behaviour.&lt;br /&gt;
&lt;br /&gt;
====Hinting====&lt;br /&gt;
Some matching engines support hinting (not an easy thing to do using PHP at all) in the 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 finds that response starts matching and on some character breaks it. Say you entered an expression:&lt;br /&gt;
  &#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&lt;br /&gt;
and a student answered:&lt;br /&gt;
  they are blue, vhite and red&lt;br /&gt;
Partial matching will find that the 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&#039;s 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 the &#039;&#039;&#039;hint&#039;&#039;&#039; button by pressing which he receives 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 type doesn&#039;t add hinted characters to the student&#039;s response (unlike the regex question type), showing it separately instead for a number of reasons:&lt;br /&gt;
# it is student&#039;s responsibility whether he wants 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 chooses a character that leads to the shortest path to complete the 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 the match, while &#039; &#039; leads to the path 3 characters longer.&lt;br /&gt;
&lt;br /&gt;
It is possible that not all regular expressions will give 100% grade. Consider you added 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 the grade greater or equal than the hint grade border will be used for partial matching and hinting. If you set hint grade border to 1, only 100% grade regular expression will be used for hinting, if you set it to 0,5 regular expressions with 50% then 100% grades will be used and 0%-49% would not. Regular expressions not used for hinting work 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 parentheses in a regex are considered as a &#039;&#039;&#039;subpattern&#039;&#039;&#039; and when matching the engine remembers (&#039;&#039;&#039;captures&#039;&#039;&#039;) not only the whole match, but its parts corresponding to all subpatterns. Subpatterns can be nested. If a 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 (  ). Lookaround assertions don&#039;t create subpatterns.&lt;br /&gt;
&lt;br /&gt;
Subpatterns are counted from left to right by opening parentheses. Precisely &#039;&#039;&#039;0&#039;&#039;&#039; is the whole regex, &#039;&#039;&#039;1&#039;&#039;&#039; is first subpattern etc. You can 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 the first subpattern value etc. That can improve the quality of you feedbacks. Placeholders won&#039;t work on the &#039;&#039;general feedback&#039;&#039; because different answers can have different number of subpatterns.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;PHP preg engine&#039;&#039;&#039; and &#039;&#039;&#039;NFA&#039;&#039;&#039; support full subpattern capturing. &#039;&#039;&#039;DFA&#039;&#039;&#039; engine can&#039;t do it by its nature, so you can use only {$0} placeholder when using the DFA engine.&lt;br /&gt;
&lt;br /&gt;
Let&#039;s look at a regex defining a 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;
If you wrote the feedback:&lt;br /&gt;
 The number is: {$0} Integral part is {$1} and fractional part is {$2}&lt;br /&gt;
Then a student entered&lt;br /&gt;
 123.34&lt;br /&gt;
He 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 is an error in regular expression or not, so &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; engine can&#039;t tell you much about the error.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NFA&#039;&#039;&#039; and &#039;&#039;&#039;DFA&#039;&#039;&#039; engines use a custom &#039;&#039;&#039;regular expression parser&#039;&#039;&#039;, so they support the advanced error reporting. The are several classes of potential errors:&lt;br /&gt;
* more than two top-level alternatives in a conditional subpattern &amp;quot;(?(?=f)first|second|third)&amp;quot;;&lt;br /&gt;
* unopened closing parenthesis &amp;quot;abc)&amp;quot;;&lt;br /&gt;
* unclosed opening parenthesis of any sort (subpatterns, assertions, etc) &amp;quot;(?:qwerty&amp;quot;;&lt;br /&gt;
* empty parentheses of any sort &amp;quot;(?=)&amp;quot;;&lt;br /&gt;
* quantifier without an operand, i.e. at the start of (sub)expression with nothing to repeat &amp;quot;+&amp;quot; or &amp;quot;a(+)&amp;quot;;&lt;br /&gt;
* unclosed brackets of character classes &amp;quot;[a-fA-F\d&amp;quot;;&lt;br /&gt;
* setting and unsetting the same modifier at the same time &amp;quot;(?i-i)&amp;quot;;&lt;br /&gt;
* unknown unicode properties &amp;quot;\p{Squirrel}&amp;quot;;&lt;br /&gt;
* unknown posix classes &amp;quot;[[:hamster:]]&amp;quot;;&lt;br /&gt;
* unknown (*...) sequence &amp;quot;(*QWERTY)&amp;quot;;&lt;br /&gt;
* incorrect ranges for quantifiers &amp;quot;a{5,4}&amp;quot; or character sets &amp;quot;[z-a]&amp;quot;.&lt;br /&gt;
&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, a quantifier {2,4} placed at the start of a regular expression loses the meaning as a quantifier and is treated as a five-characters sequence instead (that matches with the string &amp;quot;{2,4}&amp;quot;). However such syntax is very prone to errors and makes writing regular expression harder.&lt;br /&gt;
&lt;br /&gt;
For now I vote for reporting errors instead of treating them as literals, even if it means incompatibility with PCRE. If you stand for or against this decision then please write you positions and reasons to the comments. 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&#039;s REGEXP question type has a &#039;&#039;&#039;missing words&#039;&#039;&#039; feature, allowing to define an answer that will work when something is absent in the answer (and give an appropriate feedback to the student). &lt;br /&gt;
&lt;br /&gt;
Similar effect can be achieved with &#039;&#039;&#039;negative assertions&#039;&#039;&#039; combined with anchoring the matching start. The 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 is 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 anchores the match to the start of the 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 if 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&#039;s 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 requires some complex work.&lt;br /&gt;
&lt;br /&gt;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use and the regular expressions engine it 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 the native PHP preg functions (which is in turn based on the PCRE library). It supports 100% perl-compatible regular expression features, it is very stable and thoroughly tested. Bot PHP functions doesn&#039;t support partial matching, 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 supports subpattern capturing. Choose it when you need complex regexp features that other engines don&#039;t support.&lt;br /&gt;
&lt;br /&gt;
=====Deterministic finite state automata (DFA)=====&lt;br /&gt;
This is a custom PHP code that uses 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 can still differ from the standard. On the bright side it is support &#039;&#039;&#039;hinting&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Currently supported operands:&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;
* unicode properties&lt;br /&gt;
&lt;br /&gt;
Currently supported operators:&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 was introduced in the 2.1 release. It is a custom matcher that can do everything that DFA matcher can, but also supports:&lt;br /&gt;
* subpattern capturing (including named subpatterns, duplicate subpatterns numbers)&lt;br /&gt;
* backreference capturing (including named backreferences)&lt;br /&gt;
&lt;br /&gt;
So, you don&#039;t have to choose between hiting and subpattern capturing in you questions - NFA can do them both! Also, the NFA matcher is more stable than the DFA one and it is probably the best choise if you want to use partial matching with hinting, but without lookaround assertions.&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 free much 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 your usage of the Preg question I could give reference for would improve rating of the project there and my rating as a researcher/developer, so please publish and let me know the reference if you feel grateful for this 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 your organisation is and how you use preg - that&#039;ll help 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 a 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 the development for those features - it depends on the available time and developers. Many features require complex code to achieve the results. If you want to help us with a specific feature, please contact the question type maintainer (Oleg Sychev) using http://moodle.org messaging.&lt;br /&gt;
* Improve 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 the 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;
&lt;br /&gt;
[[Category:Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=94727</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=94727"/>
		<updated>2011-12-23T14:35:56Z</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 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/2x/ca/index.php?title=Preg_question_type&amp;diff=94529</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=94529"/>
		<updated>2011-12-13T10:33:38Z</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/2x/ca/index.php?title=Preg_question_type&amp;diff=94528</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=94528"/>
		<updated>2011-12-13T10:20:32Z</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;
# NFA regular expression matching engine - Valeriy Streltsov&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. 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/2x/ca/index.php?title=Discussi%C3%B3:Preg_question_type&amp;diff=94527</id>
		<title>Discussió:Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Discussi%C3%B3:Preg_question_type&amp;diff=94527"/>
		<updated>2011-12-13T10:19:56Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Looking for missing things */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=== Looking for missing things ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Joseph Rezeau REGEXP question type has a special syntax for missing words feature, allowing to define an answer that would work when something is absent in the answer (and give appropriate feedback to the student).&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Please note that, starting with the version for moodle 2.1, my REGEXP question type accepts negative assertions (such as used in PREG question type) as well as the proprietary &#039;&#039;&#039;--.*\bnecessary\b.*&#039;&#039;&#039; syntax.&lt;br /&gt;
&lt;br /&gt;
@Oleg: you may wish to change your documentation to reflect this change in REGEXP.&lt;br /&gt;
&lt;br /&gt;
--[[User:Joseph Rézeau|Joseph Rézeau]] 01:41, 21 November 2011 (WST)&lt;br /&gt;
&lt;br /&gt;
Thanks, Joseph, I hope you&#039;ll like the changes I&#039;ve done. Please sent Moodle Message to me directly if there would be similar related changes in the future, as I don&#039;t have much time and rarely monitor documentation wiki talks page.&lt;br /&gt;
--[[User:Oleg Sychev|Oleg Sychev]] 18:19, 13 December 2011 (WST)&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=94526</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Preg_question_type&amp;diff=94526"/>
		<updated>2011-12-13T10:16:59Z</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/2x/ca/index.php?title=Fitxer:Bulk_edit4.JPG&amp;diff=83233</id>
		<title>Fitxer:Bulk edit4.JPG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Fitxer: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/2x/ca/index.php?title=Linked_activities&amp;diff=83232</id>
		<title>Linked activities</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=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/2x/ca/index.php?title=Fitxer:Bulk_edit4.JPG&amp;diff=83231</id>
		<title>Fitxer:Bulk edit4.JPG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Fitxer: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/2x/ca/index.php?title=Fitxer:Bulk_edit3.JPG&amp;diff=83230</id>
		<title>Fitxer:Bulk edit3.JPG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Fitxer: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/2x/ca/index.php?title=Fitxer:Bulk_edit2.JPG&amp;diff=83229</id>
		<title>Fitxer:Bulk edit2.JPG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Fitxer: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/2x/ca/index.php?title=Fitxer:Bulk_edit1.JPG&amp;diff=83228</id>
		<title>Fitxer:Bulk edit1.JPG</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Fitxer: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/2x/ca/index.php?title=Broken/Process&amp;diff=79015</id>
		<title>Broken/Process</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/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/2x/ca/index.php?title=VSTU_projects&amp;diff=77730</id>
		<title>VSTU projects</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/index.php?title=Broken/Forum_thread_subscription&amp;diff=76812</id>
		<title>Broken/Forum thread subscription</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/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/2x/ca/index.php?title=Broken/Javascript-interface_for_repeat_elements_function&amp;diff=76778</id>
		<title>Broken/Javascript-interface for repeat elements function</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/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/2x/ca/index.php?title=Javascript-interface_for_repeat_elements_function&amp;diff=76777</id>
		<title>Javascript-interface for repeat elements function</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=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/2x/ca/index.php?title=Javascript-interface_for_repeat_elements_function&amp;diff=76776</id>
		<title>Javascript-interface for repeat elements function</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=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/2x/ca/index.php?title=Broken/Javascript-interface_for_repeat_elements_function&amp;diff=76422</id>
		<title>Broken/Javascript-interface for repeat elements function</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/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/2x/ca/index.php?title=Broken/Forum_thread_subscription&amp;diff=76344</id>
		<title>Broken/Forum thread subscription</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/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/2x/ca/index.php?title=Broken/Javascript-interface_for_repeat_elements_function&amp;diff=76343</id>
		<title>Broken/Javascript-interface for repeat elements function</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/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/2x/ca/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/2x/ca/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/2x/ca/index.php?title=VSTU_projects&amp;diff=76019</id>
		<title>VSTU projects</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/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/2x/ca/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>
</feed>