<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="pl">
	<id>https://docs.moodle.org/2x/pl/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Oasychev</id>
	<title>MoodleDocs - Wkład użytkownika [pl]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/2x/pl/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Oasychev"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/Specjalna:Wk%C5%82ad/Oasychev"/>
	<updated>2026-04-22T18:10:37Z</updated>
	<subtitle>Wkład użytkownika</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/2x/pl/index.php?title=Supervised_block&amp;diff=111552</id>
		<title>Supervised block</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Supervised_block&amp;diff=111552"/>
		<updated>2014-04-02T00:46:19Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Editing quiz settings */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The main idea of the &#039;&#039;&#039;supervised block&#039;&#039;&#039; is to have an additional control over your students, so they will be able to do something only under teacher supervision. Installed with &#039;&#039;&#039;supervisedcheck&#039;&#039;&#039; (quiz access rules plugin, included out of the box) allows you to add restrictions to your quizzes.&lt;br /&gt;
&lt;br /&gt;
How it works together? The course&#039;s teacher creates the session specifying the academic group, lesson type (e.g. laboratory work, exam, etc.), classroom and duration. After that students will be able to start quizzes from this course according next conditions:&lt;br /&gt;
* the session is active;&lt;br /&gt;
* student is in an academic group for which the session was created;&lt;br /&gt;
* student is in session&#039;s classroom (you can specify the ip subnet for each classroom, see below);&lt;br /&gt;
* the session was created for the lesson type which is specified for current quiz (see editing quiz settings section).&lt;br /&gt;
&lt;br /&gt;
Of course you can specify only some important quizzes to be under teacher&#039;s control and not all in the course.&lt;br /&gt;
&lt;br /&gt;
Also, you can combine the supervised block with [https://docs.moodle.org/2x/pl/Auto_role_assignment_block Auto role assignment block]. It is useful to temporary change user role during the session.&lt;br /&gt;
&lt;br /&gt;
== Structure and installation ==&lt;br /&gt;
Out of the box you have 2 Moodle plugins:&lt;br /&gt;
# supervised is a block for managing sessions, classrooms and lesson types. For installing, put the supervised folder into your-moodle-path/blocks/ directory.&lt;br /&gt;
# supervisedcheck is a quiz access rules plugin for setting up your quizzes to work with supervised block. For installing, put the supervisedcheck folder into your-moodle-path/mod/quiz/accessrule/ directory. This plugin can not be used separately and must be installed only together or after supervised block.&lt;br /&gt;
Go to http://your-moodle-website/admin/ to finish the installation process.&lt;br /&gt;
&lt;br /&gt;
== Teacher-side ==&lt;br /&gt;
First of all, go to your course, turn editing on and add the supervised block to be able work with it. Now before creating your first session, you should create lesson types and classrooms (and may be add some groups to your course?).&lt;br /&gt;
&lt;br /&gt;
=== Creating lesson types ===&lt;br /&gt;
You can omit this step if you want all you quizzes either to be accessible on all sessions (lessons) or without restrictions at all. &lt;br /&gt;
&lt;br /&gt;
However, in more complex courses you want some quizzes accessible on some lessons and not on the other. Then you need to define &amp;quot;lesson type&amp;quot;. Each lesson type is a kind of lesson, to which quiz can be limited - it is called &amp;quot;lesson type&amp;quot; as a teacher often can have several lessons on one theme (type) with different students groups. If you define lesson types for you course, quiz editing will allow you option to check on which lesson types quiz will be available.&lt;br /&gt;
&lt;br /&gt;
In the supervised block&#039;s body click the &amp;quot;lesson types&amp;quot; link. Here you can add some:&lt;br /&gt;
&lt;br /&gt;
[[Image:lessontypes.png]]&lt;br /&gt;
&lt;br /&gt;
Lesson type describes just by it&#039;s name and nothing else. Consider that lesson types creates for current course, so you must create new ones for each other course.&lt;br /&gt;
&lt;br /&gt;
=== Creating classrooms ===&lt;br /&gt;
To start session you must provide at least one classroom - i.e. a range of computers under you supervision. A range of computers can be defined by IP subnet. Ask you admin for &amp;quot;IP range&amp;quot; for you class if you don&#039;t know wha t it means, and he will came as a string you could enter there.&lt;br /&gt;
&lt;br /&gt;
Each classroom describes by name and IP subnet. Additionally you can &amp;quot;hide&amp;quot; the classroom, so it will remain in system but you won&#039;t be able to create a session in this classroom.&lt;br /&gt;
You can specify an IP subnet in standard Moodle format. So they divided by comma and each address can be described in one on those ways:&lt;br /&gt;
* xxx.xxx.xxx.xxx (full IP address)&lt;br /&gt;
* xxx.xxx.xxx.xxx/nn (number of bits in net mask)&lt;br /&gt;
* xxx.xxx.xxx.xxx-yyy (a range of IP addresses in the last group)&lt;br /&gt;
* xxx.xxx or xxx.xxx. (incomplete address)&lt;br /&gt;
Example: 235.144.18, 235.144.19.2-34, 235.144.19.38, 235.144.19.40-44&lt;br /&gt;
&lt;br /&gt;
[[Image:classrooms.png]]&lt;br /&gt;
&lt;br /&gt;
As opposed to lesson types, you can use the same classrooms in all courses.&lt;br /&gt;
&lt;br /&gt;
=== Editing quiz settings ===&lt;br /&gt;
For link some quizzes from course with supervised sessions, go to their settings, extra restrictions on attempts section. Here you will find three options for setting your quiz:&lt;br /&gt;
* Don&#039;t use supervised control for this quiz (by default) - quiz is accessible without need to be supervised.&lt;br /&gt;
* Quiz is accessible on any supervised session, but only on them.&lt;br /&gt;
* Quiz iz accessible only on sessions of some lesson types (even if you start a session for unchecked lesson type, student won&#039;t be able to start this quiz).&lt;br /&gt;
&lt;br /&gt;
[[Image:qar.png]]&lt;br /&gt;
&lt;br /&gt;
=== Sessions starting or planning ===&lt;br /&gt;
The teacher can start a new session just from supervised block&#039;s body choosing classroom, lesson type (if some of them was created), academic group and session duration:&lt;br /&gt;
&lt;br /&gt;
[[Image:start_s.png]]&lt;br /&gt;
&lt;br /&gt;
Teacher can also choose &amp;quot;All groups&amp;quot;. In this case all students from this course will be able to start the quiz.&lt;br /&gt;
&lt;br /&gt;
During the session teacher can change the classroom, academic group, duration, view session logs (see below) or finish session:&lt;br /&gt;
&lt;br /&gt;
[[Image:active_s.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The second option is to &#039;&#039;&#039;plan the session&#039;&#039;&#039;. In this case the manager plans the schedule for teachers and add sessions for them. The teacher can also plans sessions for himself (see also capabilities section).&lt;br /&gt;
&lt;br /&gt;
[[Image:plane_s.png]]&lt;br /&gt;
&lt;br /&gt;
If the option &amp;quot;Send e-mail&amp;quot; is checked, the teacher will be notified about session creation and any changes.&lt;br /&gt;
&lt;br /&gt;
What happens if the teacher has a planned session near current time? The supervised block shows the notification about it and propose to start it. The teacher is able to change something before starting (classroom, group, lesson type or duration).&lt;br /&gt;
&lt;br /&gt;
The teacher is able to view session logs and filter them by users took part in session.&lt;br /&gt;
&lt;br /&gt;
== Student-side ==&lt;br /&gt;
When student is in course he can see in a supervised block if there is an active session(s):&lt;br /&gt;
&lt;br /&gt;
[[Image:student_side.png]]&lt;br /&gt;
&lt;br /&gt;
So, he can try to start the quiz.&lt;br /&gt;
&lt;br /&gt;
== Capabilities ==&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Capability name&#039;&#039;&#039;&lt;br /&gt;
|&#039;&#039;&#039;Archetypes&#039;&#039;&#039;&lt;br /&gt;
|&#039;&#039;&#039;Description&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: besupervised&lt;br /&gt;
|student&lt;br /&gt;
|User can be supervised&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: supervise&lt;br /&gt;
|editingteacher, teacher&lt;br /&gt;
|Ability to:&lt;br /&gt;
* start planned session&lt;br /&gt;
* start new session&lt;br /&gt;
* change / finish active session&lt;br /&gt;
* view active session logs&lt;br /&gt;
User can&#039;t plane his own sessions.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: editclassrooms&lt;br /&gt;
|editingteacher, manager&lt;br /&gt;
|Add / edit / delete / view classrooms&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: editlessontypes&lt;br /&gt;
|editingteacher, manager&lt;br /&gt;
|Add / edit / delete / view lesson types&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: viewownsessions&lt;br /&gt;
|editingteacher, teacher&lt;br /&gt;
|View own sessions (active, planned, finished) and their logs.&lt;br /&gt;
User can&#039;t edit them, start or plane new.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: viewallsessions&lt;br /&gt;
|manager, editingteacher&lt;br /&gt;
|View sessions of all teachers(active, planned, finished) and their logs.&lt;br /&gt;
User can&#039;t edit them, start or plane new.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: manageownsessions&lt;br /&gt;
|editingteacher, teacher&lt;br /&gt;
|Ability for own sessions:&lt;br /&gt;
* plan&lt;br /&gt;
* view / edit / remove planned&lt;br /&gt;
User can&#039;t start his own sessions.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: manageallsessions&lt;br /&gt;
|manager, coursecreator&lt;br /&gt;
|Ability for sessions of all teachers:&lt;br /&gt;
* plan&lt;br /&gt;
* view / edit / remove planned&lt;br /&gt;
User can&#039;t start sessions.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: managefinishedsessions&lt;br /&gt;
|manager, coursecreator&lt;br /&gt;
| Ability to remove finished sessions.&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/pl/index.php?title=Supervised_block&amp;diff=111551</id>
		<title>Supervised block</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Supervised_block&amp;diff=111551"/>
		<updated>2014-04-02T00:44:28Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Creating classrooms */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The main idea of the &#039;&#039;&#039;supervised block&#039;&#039;&#039; is to have an additional control over your students, so they will be able to do something only under teacher supervision. Installed with &#039;&#039;&#039;supervisedcheck&#039;&#039;&#039; (quiz access rules plugin, included out of the box) allows you to add restrictions to your quizzes.&lt;br /&gt;
&lt;br /&gt;
How it works together? The course&#039;s teacher creates the session specifying the academic group, lesson type (e.g. laboratory work, exam, etc.), classroom and duration. After that students will be able to start quizzes from this course according next conditions:&lt;br /&gt;
* the session is active;&lt;br /&gt;
* student is in an academic group for which the session was created;&lt;br /&gt;
* student is in session&#039;s classroom (you can specify the ip subnet for each classroom, see below);&lt;br /&gt;
* the session was created for the lesson type which is specified for current quiz (see editing quiz settings section).&lt;br /&gt;
&lt;br /&gt;
Of course you can specify only some important quizzes to be under teacher&#039;s control and not all in the course.&lt;br /&gt;
&lt;br /&gt;
Also, you can combine the supervised block with [https://docs.moodle.org/2x/pl/Auto_role_assignment_block Auto role assignment block]. It is useful to temporary change user role during the session.&lt;br /&gt;
&lt;br /&gt;
== Structure and installation ==&lt;br /&gt;
Out of the box you have 2 Moodle plugins:&lt;br /&gt;
# supervised is a block for managing sessions, classrooms and lesson types. For installing, put the supervised folder into your-moodle-path/blocks/ directory.&lt;br /&gt;
# supervisedcheck is a quiz access rules plugin for setting up your quizzes to work with supervised block. For installing, put the supervisedcheck folder into your-moodle-path/mod/quiz/accessrule/ directory. This plugin can not be used separately and must be installed only together or after supervised block.&lt;br /&gt;
Go to http://your-moodle-website/admin/ to finish the installation process.&lt;br /&gt;
&lt;br /&gt;
== Teacher-side ==&lt;br /&gt;
First of all, go to your course, turn editing on and add the supervised block to be able work with it. Now before creating your first session, you should create lesson types and classrooms (and may be add some groups to your course?).&lt;br /&gt;
&lt;br /&gt;
=== Creating lesson types ===&lt;br /&gt;
You can omit this step if you want all you quizzes either to be accessible on all sessions (lessons) or without restrictions at all. &lt;br /&gt;
&lt;br /&gt;
However, in more complex courses you want some quizzes accessible on some lessons and not on the other. Then you need to define &amp;quot;lesson type&amp;quot;. Each lesson type is a kind of lesson, to which quiz can be limited - it is called &amp;quot;lesson type&amp;quot; as a teacher often can have several lessons on one theme (type) with different students groups. If you define lesson types for you course, quiz editing will allow you option to check on which lesson types quiz will be available.&lt;br /&gt;
&lt;br /&gt;
In the supervised block&#039;s body click the &amp;quot;lesson types&amp;quot; link. Here you can add some:&lt;br /&gt;
&lt;br /&gt;
[[Image:lessontypes.png]]&lt;br /&gt;
&lt;br /&gt;
Lesson type describes just by it&#039;s name and nothing else. Consider that lesson types creates for current course, so you must create new ones for each other course.&lt;br /&gt;
&lt;br /&gt;
=== Creating classrooms ===&lt;br /&gt;
To start session you must provide at least one classroom - i.e. a range of computers under you supervision. A range of computers can be defined by IP subnet. Ask you admin for &amp;quot;IP range&amp;quot; for you class if you don&#039;t know wha t it means, and he will came as a string you could enter there.&lt;br /&gt;
&lt;br /&gt;
Each classroom describes by name and IP subnet. Additionally you can &amp;quot;hide&amp;quot; the classroom, so it will remain in system but you won&#039;t be able to create a session in this classroom.&lt;br /&gt;
You can specify an IP subnet in standard Moodle format. So they divided by comma and each address can be described in one on those ways:&lt;br /&gt;
* xxx.xxx.xxx.xxx (full IP address)&lt;br /&gt;
* xxx.xxx.xxx.xxx/nn (number of bits in net mask)&lt;br /&gt;
* xxx.xxx.xxx.xxx-yyy (a range of IP addresses in the last group)&lt;br /&gt;
* xxx.xxx or xxx.xxx. (incomplete address)&lt;br /&gt;
Example: 235.144.18, 235.144.19.2-34, 235.144.19.38, 235.144.19.40-44&lt;br /&gt;
&lt;br /&gt;
[[Image:classrooms.png]]&lt;br /&gt;
&lt;br /&gt;
As opposed to lesson types, you can use the same classrooms in all courses.&lt;br /&gt;
&lt;br /&gt;
=== Editing quiz settings ===&lt;br /&gt;
For link some quizzes from course with supervised sessions, go to their settings, extra restrictions on attempts section. Here you will find three options for setting your quiz:&lt;br /&gt;
* Don&#039;t use supervised control for this quiz (by default).&lt;br /&gt;
* Use supervised control for all lesson types.&lt;br /&gt;
* Use supervised control for custom lesson types (even if you start a session for unchecked lesson type, student won&#039;t be able to start this quiz).&lt;br /&gt;
&lt;br /&gt;
[[Image:qar.png]]&lt;br /&gt;
&lt;br /&gt;
=== Sessions starting or planning ===&lt;br /&gt;
The teacher can start a new session just from supervised block&#039;s body choosing classroom, lesson type (if some of them was created), academic group and session duration:&lt;br /&gt;
&lt;br /&gt;
[[Image:start_s.png]]&lt;br /&gt;
&lt;br /&gt;
Teacher can also choose &amp;quot;All groups&amp;quot;. In this case all students from this course will be able to start the quiz.&lt;br /&gt;
&lt;br /&gt;
During the session teacher can change the classroom, academic group, duration, view session logs (see below) or finish session:&lt;br /&gt;
&lt;br /&gt;
[[Image:active_s.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The second option is to &#039;&#039;&#039;plan the session&#039;&#039;&#039;. In this case the manager plans the schedule for teachers and add sessions for them. The teacher can also plans sessions for himself (see also capabilities section).&lt;br /&gt;
&lt;br /&gt;
[[Image:plane_s.png]]&lt;br /&gt;
&lt;br /&gt;
If the option &amp;quot;Send e-mail&amp;quot; is checked, the teacher will be notified about session creation and any changes.&lt;br /&gt;
&lt;br /&gt;
What happens if the teacher has a planned session near current time? The supervised block shows the notification about it and propose to start it. The teacher is able to change something before starting (classroom, group, lesson type or duration).&lt;br /&gt;
&lt;br /&gt;
The teacher is able to view session logs and filter them by users took part in session.&lt;br /&gt;
&lt;br /&gt;
== Student-side ==&lt;br /&gt;
When student is in course he can see in a supervised block if there is an active session(s):&lt;br /&gt;
&lt;br /&gt;
[[Image:student_side.png]]&lt;br /&gt;
&lt;br /&gt;
So, he can try to start the quiz.&lt;br /&gt;
&lt;br /&gt;
== Capabilities ==&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Capability name&#039;&#039;&#039;&lt;br /&gt;
|&#039;&#039;&#039;Archetypes&#039;&#039;&#039;&lt;br /&gt;
|&#039;&#039;&#039;Description&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: besupervised&lt;br /&gt;
|student&lt;br /&gt;
|User can be supervised&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: supervise&lt;br /&gt;
|editingteacher, teacher&lt;br /&gt;
|Ability to:&lt;br /&gt;
* start planned session&lt;br /&gt;
* start new session&lt;br /&gt;
* change / finish active session&lt;br /&gt;
* view active session logs&lt;br /&gt;
User can&#039;t plane his own sessions.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: editclassrooms&lt;br /&gt;
|editingteacher, manager&lt;br /&gt;
|Add / edit / delete / view classrooms&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: editlessontypes&lt;br /&gt;
|editingteacher, manager&lt;br /&gt;
|Add / edit / delete / view lesson types&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: viewownsessions&lt;br /&gt;
|editingteacher, teacher&lt;br /&gt;
|View own sessions (active, planned, finished) and their logs.&lt;br /&gt;
User can&#039;t edit them, start or plane new.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: viewallsessions&lt;br /&gt;
|manager, editingteacher&lt;br /&gt;
|View sessions of all teachers(active, planned, finished) and their logs.&lt;br /&gt;
User can&#039;t edit them, start or plane new.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: manageownsessions&lt;br /&gt;
|editingteacher, teacher&lt;br /&gt;
|Ability for own sessions:&lt;br /&gt;
* plan&lt;br /&gt;
* view / edit / remove planned&lt;br /&gt;
User can&#039;t start his own sessions.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: manageallsessions&lt;br /&gt;
|manager, coursecreator&lt;br /&gt;
|Ability for sessions of all teachers:&lt;br /&gt;
* plan&lt;br /&gt;
* view / edit / remove planned&lt;br /&gt;
User can&#039;t start sessions.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: managefinishedsessions&lt;br /&gt;
|manager, coursecreator&lt;br /&gt;
| Ability to remove finished sessions.&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/pl/index.php?title=Supervised_block&amp;diff=111550</id>
		<title>Supervised block</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Supervised_block&amp;diff=111550"/>
		<updated>2014-04-02T00:41:32Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Creating lesson types */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The main idea of the &#039;&#039;&#039;supervised block&#039;&#039;&#039; is to have an additional control over your students, so they will be able to do something only under teacher supervision. Installed with &#039;&#039;&#039;supervisedcheck&#039;&#039;&#039; (quiz access rules plugin, included out of the box) allows you to add restrictions to your quizzes.&lt;br /&gt;
&lt;br /&gt;
How it works together? The course&#039;s teacher creates the session specifying the academic group, lesson type (e.g. laboratory work, exam, etc.), classroom and duration. After that students will be able to start quizzes from this course according next conditions:&lt;br /&gt;
* the session is active;&lt;br /&gt;
* student is in an academic group for which the session was created;&lt;br /&gt;
* student is in session&#039;s classroom (you can specify the ip subnet for each classroom, see below);&lt;br /&gt;
* the session was created for the lesson type which is specified for current quiz (see editing quiz settings section).&lt;br /&gt;
&lt;br /&gt;
Of course you can specify only some important quizzes to be under teacher&#039;s control and not all in the course.&lt;br /&gt;
&lt;br /&gt;
Also, you can combine the supervised block with [https://docs.moodle.org/2x/pl/Auto_role_assignment_block Auto role assignment block]. It is useful to temporary change user role during the session.&lt;br /&gt;
&lt;br /&gt;
== Structure and installation ==&lt;br /&gt;
Out of the box you have 2 Moodle plugins:&lt;br /&gt;
# supervised is a block for managing sessions, classrooms and lesson types. For installing, put the supervised folder into your-moodle-path/blocks/ directory.&lt;br /&gt;
# supervisedcheck is a quiz access rules plugin for setting up your quizzes to work with supervised block. For installing, put the supervisedcheck folder into your-moodle-path/mod/quiz/accessrule/ directory. This plugin can not be used separately and must be installed only together or after supervised block.&lt;br /&gt;
Go to http://your-moodle-website/admin/ to finish the installation process.&lt;br /&gt;
&lt;br /&gt;
== Teacher-side ==&lt;br /&gt;
First of all, go to your course, turn editing on and add the supervised block to be able work with it. Now before creating your first session, you should create lesson types and classrooms (and may be add some groups to your course?).&lt;br /&gt;
&lt;br /&gt;
=== Creating lesson types ===&lt;br /&gt;
You can omit this step if you want all you quizzes either to be accessible on all sessions (lessons) or without restrictions at all. &lt;br /&gt;
&lt;br /&gt;
However, in more complex courses you want some quizzes accessible on some lessons and not on the other. Then you need to define &amp;quot;lesson type&amp;quot;. Each lesson type is a kind of lesson, to which quiz can be limited - it is called &amp;quot;lesson type&amp;quot; as a teacher often can have several lessons on one theme (type) with different students groups. If you define lesson types for you course, quiz editing will allow you option to check on which lesson types quiz will be available.&lt;br /&gt;
&lt;br /&gt;
In the supervised block&#039;s body click the &amp;quot;lesson types&amp;quot; link. Here you can add some:&lt;br /&gt;
&lt;br /&gt;
[[Image:lessontypes.png]]&lt;br /&gt;
&lt;br /&gt;
Lesson type describes just by it&#039;s name and nothing else. Consider that lesson types creates for current course, so you must create new ones for each other course.&lt;br /&gt;
&lt;br /&gt;
=== Creating classrooms ===&lt;br /&gt;
Each classroom describes by name and IP subnet. Additionally you can &amp;quot;hide&amp;quot; the classroom, so it will remain in system but you won&#039;t be able to create a session in this classroom.&lt;br /&gt;
You can specify an IP subnet in standard Moodle format. So they divided by comma and each address can be described in one on those ways:&lt;br /&gt;
* xxx.xxx.xxx.xxx (full IP address)&lt;br /&gt;
* xxx.xxx.xxx.xxx/nn (number of bits in net mask)&lt;br /&gt;
* xxx.xxx.xxx.xxx-yyy (a range of IP addresses in the last group)&lt;br /&gt;
* xxx.xxx or xxx.xxx. (incomplete address)&lt;br /&gt;
Example: 235.144.18, 235.144.19.2-34, 235.144.19.38, 235.144.19.40-44&lt;br /&gt;
&lt;br /&gt;
[[Image:classrooms.png]]&lt;br /&gt;
&lt;br /&gt;
As opposed to lesson types, you can use the same classrooms in all courses. For start session you must provide at least one classroom. Consider, if you create classroom with an incorrect IP subnet, students won&#039;t be able to start the quiz.&lt;br /&gt;
&lt;br /&gt;
=== Editing quiz settings ===&lt;br /&gt;
For link some quizzes from course with supervised sessions, go to their settings, extra restrictions on attempts section. Here you will find three options for setting your quiz:&lt;br /&gt;
* Don&#039;t use supervised control for this quiz (by default).&lt;br /&gt;
* Use supervised control for all lesson types.&lt;br /&gt;
* Use supervised control for custom lesson types (even if you start a session for unchecked lesson type, student won&#039;t be able to start this quiz).&lt;br /&gt;
&lt;br /&gt;
[[Image:qar.png]]&lt;br /&gt;
&lt;br /&gt;
=== Sessions starting or planning ===&lt;br /&gt;
The teacher can start a new session just from supervised block&#039;s body choosing classroom, lesson type (if some of them was created), academic group and session duration:&lt;br /&gt;
&lt;br /&gt;
[[Image:start_s.png]]&lt;br /&gt;
&lt;br /&gt;
Teacher can also choose &amp;quot;All groups&amp;quot;. In this case all students from this course will be able to start the quiz.&lt;br /&gt;
&lt;br /&gt;
During the session teacher can change the classroom, academic group, duration, view session logs (see below) or finish session:&lt;br /&gt;
&lt;br /&gt;
[[Image:active_s.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The second option is to &#039;&#039;&#039;plan the session&#039;&#039;&#039;. In this case the manager plans the schedule for teachers and add sessions for them. The teacher can also plans sessions for himself (see also capabilities section).&lt;br /&gt;
&lt;br /&gt;
[[Image:plane_s.png]]&lt;br /&gt;
&lt;br /&gt;
If the option &amp;quot;Send e-mail&amp;quot; is checked, the teacher will be notified about session creation and any changes.&lt;br /&gt;
&lt;br /&gt;
What happens if the teacher has a planned session near current time? The supervised block shows the notification about it and propose to start it. The teacher is able to change something before starting (classroom, group, lesson type or duration).&lt;br /&gt;
&lt;br /&gt;
The teacher is able to view session logs and filter them by users took part in session.&lt;br /&gt;
&lt;br /&gt;
== Student-side ==&lt;br /&gt;
When student is in course he can see in a supervised block if there is an active session(s):&lt;br /&gt;
&lt;br /&gt;
[[Image:student_side.png]]&lt;br /&gt;
&lt;br /&gt;
So, he can try to start the quiz.&lt;br /&gt;
&lt;br /&gt;
== Capabilities ==&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot;&lt;br /&gt;
|&#039;&#039;&#039;Capability name&#039;&#039;&#039;&lt;br /&gt;
|&#039;&#039;&#039;Archetypes&#039;&#039;&#039;&lt;br /&gt;
|&#039;&#039;&#039;Description&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: besupervised&lt;br /&gt;
|student&lt;br /&gt;
|User can be supervised&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: supervise&lt;br /&gt;
|editingteacher, teacher&lt;br /&gt;
|Ability to:&lt;br /&gt;
* start planned session&lt;br /&gt;
* start new session&lt;br /&gt;
* change / finish active session&lt;br /&gt;
* view active session logs&lt;br /&gt;
User can&#039;t plane his own sessions.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: editclassrooms&lt;br /&gt;
|editingteacher, manager&lt;br /&gt;
|Add / edit / delete / view classrooms&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: editlessontypes&lt;br /&gt;
|editingteacher, manager&lt;br /&gt;
|Add / edit / delete / view lesson types&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: viewownsessions&lt;br /&gt;
|editingteacher, teacher&lt;br /&gt;
|View own sessions (active, planned, finished) and their logs.&lt;br /&gt;
User can&#039;t edit them, start or plane new.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: viewallsessions&lt;br /&gt;
|manager, editingteacher&lt;br /&gt;
|View sessions of all teachers(active, planned, finished) and their logs.&lt;br /&gt;
User can&#039;t edit them, start or plane new.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: manageownsessions&lt;br /&gt;
|editingteacher, teacher&lt;br /&gt;
|Ability for own sessions:&lt;br /&gt;
* plan&lt;br /&gt;
* view / edit / remove planned&lt;br /&gt;
User can&#039;t start his own sessions.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: manageallsessions&lt;br /&gt;
|manager, coursecreator&lt;br /&gt;
|Ability for sessions of all teachers:&lt;br /&gt;
* plan&lt;br /&gt;
* view / edit / remove planned&lt;br /&gt;
User can&#039;t start sessions.&lt;br /&gt;
|-&lt;br /&gt;
|block/supervised: managefinishedsessions&lt;br /&gt;
|manager, coursecreator&lt;br /&gt;
| Ability to remove finished sessions.&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/pl/index.php?title=Formal_Languages_Block&amp;diff=108523</id>
		<title>Formal Languages Block</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Formal_Languages_Block&amp;diff=108523"/>
		<updated>2013-12-28T19:05:51Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* User interface */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Authors ==&lt;br /&gt;
&lt;br /&gt;
# Idea, string analysis method, general architecture and architecture implementation - Oleg Sychev &lt;br /&gt;
# Implementation of built-in language - Dmitry Mamontov&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
The goal of formal languages block is to provide an API for managing formal languages - a well-known mathematical formalism, that defines a set of strings, constrained by rules.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Сurrent status&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Feature&lt;br /&gt;
! Description&lt;br /&gt;
! Status&lt;br /&gt;
|-&lt;br /&gt;
| Scanning&lt;br /&gt;
| Breaks string into tokens, that can be useful in further analysis&lt;br /&gt;
| Implemented&lt;br /&gt;
|-&lt;br /&gt;
| Parsing&lt;br /&gt;
| Constructs [http://en.wikipedia.org/wiki/Abstract_syntax_tree abstract syntax tree]  from a string, allowing deep analysis of string or even performing evaluation&lt;br /&gt;
| Not implemented&lt;br /&gt;
|-&lt;br /&gt;
| Managing formal languages&lt;br /&gt;
| Currently C, C++, printf format string scanners are implemented. Also implemented simple lexer for english language. User-defined lexers and parser are going to be implemented in next releases.&lt;br /&gt;
| Implemented partially&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039; Current language implementation status&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Language&lt;br /&gt;
! Scanning &lt;br /&gt;
! Parsing&lt;br /&gt;
|-&lt;br /&gt;
| Simple english&lt;br /&gt;
| Implemented&lt;br /&gt;
| Not implemented&lt;br /&gt;
|-&lt;br /&gt;
| C&lt;br /&gt;
| Implemented&lt;br /&gt;
| Not implemented&lt;br /&gt;
|-&lt;br /&gt;
| C++&lt;br /&gt;
| Implemented&lt;br /&gt;
| Implemented partially&lt;br /&gt;
|-&lt;br /&gt;
| printf formatting string&lt;br /&gt;
| Implemented&lt;br /&gt;
| Not implemented&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
&lt;br /&gt;
To work Formal languages block needs some additional components. They all need to be installed in order for question to work. &lt;br /&gt;
&lt;br /&gt;
You need to install question type POASquestion ([https://moodle.org/plugins/view.php?plugin=qtype_poasquestion qtype_poasquestion]), which is abstract (i.e. not showing as real question), but contains useful code for scanning and working with Unicode strings.&lt;br /&gt;
&lt;br /&gt;
== User interface ==&lt;br /&gt;
&lt;br /&gt;
[[{{ns:file}}:block_formal_langs.PNG]] &lt;br /&gt;
&lt;br /&gt;
After adding formal languages block to course, a teacher could manage language visibilty by clicking on eye icons.  The dimmed languages with disabled visibility will not be shown in CorrectWriting or other plugins, that use API of formal language block.&lt;br /&gt;
&lt;br /&gt;
Note, that language visibility can be inherited from site settings. So, when site setting is applied to current course, user will see label &amp;quot;(Site)&amp;quot; before name of language, while &amp;quot;(Course)&amp;quot; label shows that setting applied on course level. If the language visibility for the site and course level are equal, language in this course assumed to be set to site-level visibility, and will be changed when the course-level visibility changes. If site visibility differs from course, it is assumed independent and will not change with the site-level changes.&lt;br /&gt;
&lt;br /&gt;
Administrator also, can edit global visibility of formal languages, using global settings, located in administrator menu (see pictures for details).&lt;br /&gt;
&lt;br /&gt;
[[{{ns:file}}:block_formal_langs_global_settings_link.PNG]] &lt;br /&gt;
&lt;br /&gt;
A central part of admin page will look just like below.&lt;br /&gt;
&lt;br /&gt;
[[{{ns:file}}:block_formal_langs_global_settings_page.PNG]]&lt;br /&gt;
&lt;br /&gt;
When changing site-level language visibility, admin is shown the list of courses, affected by this change.&lt;br /&gt;
&lt;br /&gt;
== API for developers ==&lt;br /&gt;
&lt;br /&gt;
A main block class - &#039;&#039;&#039;block_formal_langs&#039;&#039;&#039; provides a two simple functions, that might be useful for someone, that want to use our API to perform string scanning (see CorrectWriting question type examples to see how it works).&lt;br /&gt;
&lt;br /&gt;
#&#039;&#039;&#039;block_formal_langs::available_langs&#039;&#039;&#039; - returns array of languages, that could be used in current context. Receives current context ID.&lt;br /&gt;
#&#039;&#039;&#039;block_formal_langs::lang_object&#039;&#039;&#039;    - returns language object by language ID.&lt;br /&gt;
&lt;br /&gt;
After getting language object, you can use &#039;&#039;&#039;create_from_string&#039;&#039;&#039; or &#039;&#039;&#039;create_from_db&#039;&#039;&#039; to scan string without referring to descriptions in database or refer to them. This will return a special object, that can be used for working with lexemes.&lt;br /&gt;
&lt;br /&gt;
If $a is object, returned by &#039;&#039;&#039;create_from_string&#039;&#039;&#039; or &#039;&#039;&#039;create_from_db&#039;&#039;&#039;, you can use $a-&amp;gt;string to get scanned string, $a-&amp;gt;stream-&amp;gt;tokens to return array of scanned lexemes, $a-&amp;gt;stream-&amp;gt;errors to get array of errors.&lt;br /&gt;
&lt;br /&gt;
Using scanned lexemes, you could use &#039;&#039;&#039;type()&#039;&#039;&#039; method to obtain special type of lexeme, or &#039;&#039;&#039;value()&#039;&#039;&#039; to obtain a semantic value for lexeme.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. You can use following code to print names and ids of all available languages in system context.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$langs = block_formal_langs::available_langs( context_system::instance()-&amp;gt;id );&lt;br /&gt;
&lt;br /&gt;
foreach($langs as $id =&amp;gt; $name)&lt;br /&gt;
&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
echo  &#039;id :&#039; . $id  . &#039; name: &#039; . $name . PHP_EOL;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2.  You can use following code to print all tokens&#039; text, scanned a string with C language lexer.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$lang = block_formal_langs::lang_object(2); // Id for C programming language in most databases will be 2&lt;br /&gt;
&lt;br /&gt;
$string = $lang-&amp;gt;create_from_string(&#039;int a;&#039;);&lt;br /&gt;
&lt;br /&gt;
$tokens = $string-&amp;gt;stream-&amp;gt;tokens;&lt;br /&gt;
&lt;br /&gt;
foreach($tokens as $token)&lt;br /&gt;
&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
echo  $token-&amp;gt;value() . PHP_EOL;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: Block]][[Category: Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/pl/index.php?title=Formal_Languages_Block&amp;diff=108522</id>
		<title>Formal Languages Block</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Formal_Languages_Block&amp;diff=108522"/>
		<updated>2013-12-28T19:01:47Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Installation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Authors ==&lt;br /&gt;
&lt;br /&gt;
# Idea, string analysis method, general architecture and architecture implementation - Oleg Sychev &lt;br /&gt;
# Implementation of built-in language - Dmitry Mamontov&lt;br /&gt;
&lt;br /&gt;
== Description ==&lt;br /&gt;
&lt;br /&gt;
The goal of formal languages block is to provide an API for managing formal languages - a well-known mathematical formalism, that defines a set of strings, constrained by rules.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Сurrent status&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Feature&lt;br /&gt;
! Description&lt;br /&gt;
! Status&lt;br /&gt;
|-&lt;br /&gt;
| Scanning&lt;br /&gt;
| Breaks string into tokens, that can be useful in further analysis&lt;br /&gt;
| Implemented&lt;br /&gt;
|-&lt;br /&gt;
| Parsing&lt;br /&gt;
| Constructs [http://en.wikipedia.org/wiki/Abstract_syntax_tree abstract syntax tree]  from a string, allowing deep analysis of string or even performing evaluation&lt;br /&gt;
| Not implemented&lt;br /&gt;
|-&lt;br /&gt;
| Managing formal languages&lt;br /&gt;
| Currently C, C++, printf format string scanners are implemented. Also implemented simple lexer for english language. User-defined lexers and parser are going to be implemented in next releases.&lt;br /&gt;
| Implemented partially&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039; Current language implementation status&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Language&lt;br /&gt;
! Scanning &lt;br /&gt;
! Parsing&lt;br /&gt;
|-&lt;br /&gt;
| Simple english&lt;br /&gt;
| Implemented&lt;br /&gt;
| Not implemented&lt;br /&gt;
|-&lt;br /&gt;
| C&lt;br /&gt;
| Implemented&lt;br /&gt;
| Not implemented&lt;br /&gt;
|-&lt;br /&gt;
| C++&lt;br /&gt;
| Implemented&lt;br /&gt;
| Implemented partially&lt;br /&gt;
|-&lt;br /&gt;
| printf formatting string&lt;br /&gt;
| Implemented&lt;br /&gt;
| Not implemented&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
&lt;br /&gt;
To work Formal languages block needs some additional components. They all need to be installed in order for question to work. &lt;br /&gt;
&lt;br /&gt;
You need to install question type POASquestion ([https://moodle.org/plugins/view.php?plugin=qtype_poasquestion qtype_poasquestion]), which is abstract (i.e. not showing as real question), but contains useful code for scanning and working with Unicode strings.&lt;br /&gt;
&lt;br /&gt;
== User interface ==&lt;br /&gt;
&lt;br /&gt;
[[{{ns:file}}:block_formal_langs.PNG]] &lt;br /&gt;
&lt;br /&gt;
After adding formal languages block to course, a teacher could manage language visibilty by clicking on eye icons.  The dimmed languages with disabled visibility will not be shown in CorrectWriting or other plugins, that use API of formal language block.&lt;br /&gt;
&lt;br /&gt;
Note, that language visibility can be inherited from site settings. So, when site setting is applied to current course, user will see label &amp;quot;(Site)&amp;quot; before name of language, and &amp;quot;(Course)&amp;quot; if setting is applied to course. Note, that block tries to keep settings in database as short as possible, so if current course setting for  language matches site setting for langugage, it will be removed and visibility will be taken from site visibility setting. If it does not match, setting will be applied on course level.&lt;br /&gt;
&lt;br /&gt;
Administrator also, can edit global visibility of formal languages, using global settings, located in administrator menu (see pictures for details).&lt;br /&gt;
&lt;br /&gt;
[[{{ns:file}}:block_formal_langs_global_settings_link.PNG]] &lt;br /&gt;
&lt;br /&gt;
A central part of admin page will look just like below.&lt;br /&gt;
&lt;br /&gt;
[[{{ns:file}}:block_formal_langs_global_settings_page.PNG]]&lt;br /&gt;
&lt;br /&gt;
== API for developers ==&lt;br /&gt;
&lt;br /&gt;
A main block class - &#039;&#039;&#039;block_formal_langs&#039;&#039;&#039; provides a two simple functions, that might be useful for someone, that want to use our API to perform string scanning (see CorrectWriting question type examples to see how it works).&lt;br /&gt;
&lt;br /&gt;
#&#039;&#039;&#039;block_formal_langs::available_langs&#039;&#039;&#039; - returns array of languages, that could be used in current context. Receives current context ID.&lt;br /&gt;
#&#039;&#039;&#039;block_formal_langs::lang_object&#039;&#039;&#039;    - returns language object by language ID.&lt;br /&gt;
&lt;br /&gt;
After getting language object, you can use &#039;&#039;&#039;create_from_string&#039;&#039;&#039; or &#039;&#039;&#039;create_from_db&#039;&#039;&#039; to scan string without referring to descriptions in database or refer to them. This will return a special object, that can be used for working with lexemes.&lt;br /&gt;
&lt;br /&gt;
If $a is object, returned by &#039;&#039;&#039;create_from_string&#039;&#039;&#039; or &#039;&#039;&#039;create_from_db&#039;&#039;&#039;, you can use $a-&amp;gt;string to get scanned string, $a-&amp;gt;stream-&amp;gt;tokens to return array of scanned lexemes, $a-&amp;gt;stream-&amp;gt;errors to get array of errors.&lt;br /&gt;
&lt;br /&gt;
Using scanned lexemes, you could use &#039;&#039;&#039;type()&#039;&#039;&#039; method to obtain special type of lexeme, or &#039;&#039;&#039;value()&#039;&#039;&#039; to obtain a semantic value for lexeme.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;1. You can use following code to print names and ids of all available languages in system context.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$langs = block_formal_langs::available_langs( context_system::instance()-&amp;gt;id );&lt;br /&gt;
&lt;br /&gt;
foreach($langs as $id =&amp;gt; $name)&lt;br /&gt;
&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
echo  &#039;id :&#039; . $id  . &#039; name: &#039; . $name . PHP_EOL;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;2.  You can use following code to print all tokens&#039; text, scanned a string with C language lexer.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$lang = block_formal_langs::lang_object(2); // Id for C programming language in most databases will be 2&lt;br /&gt;
&lt;br /&gt;
$string = $lang-&amp;gt;create_from_string(&#039;int a;&#039;);&lt;br /&gt;
&lt;br /&gt;
$tokens = $string-&amp;gt;stream-&amp;gt;tokens;&lt;br /&gt;
&lt;br /&gt;
foreach($tokens as $token)&lt;br /&gt;
&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
echo  $token-&amp;gt;value() . PHP_EOL;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category: Block]][[Category: Contributed code]]&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106858</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106858"/>
		<updated>2013-10-12T20:01:43Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Authoring tools */ - updated to the RC state&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting, error reporting, regular expression testing (authoring tool) - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, matchers testing, backup&amp;amp;restore, unicode support, selection in regex text (in authoring tools) - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine (deprecated for now) - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Syntax tree (authoring tool) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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. &lt;br /&gt;
Thanks to:&lt;br /&gt;
*  Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that have been implemented in Preg question type;&lt;br /&gt;
* Tim Hunt - for his polite and useful answers and commentaries that helped writing this question, also for joint work on extra_question_fields and extra_answer_fields code, that is useful to many question type developers;&lt;br /&gt;
* Bondarenko Vitaly - for conversion of a vast set of regular expression matching tests.&lt;br /&gt;
You, too, could aways [[#The ways to give back|help us]] a lot - regardless of the way you use Preg and your capabilities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;Pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers&lt;br /&gt;
* &#039;&#039;&#039;Hinting&#039;&#039;&#039; - when students are stuck doing the question, you may allow them to ask for a next correct word (lexem) or a character (with possible penalty)&lt;br /&gt;
 &lt;br /&gt;
===Settings affecting question work===&lt;br /&gt;
Sets the case sensitivity for all regular expressions you specify as answers. Note that you can also [[#Local case-sensitivity modifiers|set the case sensitivity for regex parts]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Exact matching&#039;&#039;&#039; affects the question in the following way:&lt;br /&gt;
; &#039;&#039;Yes&#039;&#039; : the &#039;&#039;entire&#039;&#039; student&#039;s response, from the first to the last letter, should match your regular expression&lt;br /&gt;
; &#039;&#039;No&#039;&#039; : student&#039;s response can just contain a &#039;&#039;part&#039;&#039; that matches your regex: for example, if the correct answer is &amp;quot;whole&amp;quot; then &amp;quot;the whole string&amp;quot; will be a correct student response&lt;br /&gt;
 &lt;br /&gt;
You still can set some of your regexes to match the whole student&#039;s response using [[#Anchoring|special regex syntax]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Notations&#039;&#039;&#039; specify the &amp;quot;language&amp;quot; of your answers.&lt;br /&gt;
; &#039;&#039;Regular expression&#039;&#039; : a usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;Regular expression (extended)&#039;&#039; : useful for really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not inside character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything after &#039;#&#039; character untill the end of string is treated as commentary (# should not be escaped and should not be inside a character class).&lt;br /&gt;
; &#039;&#039;Moodle shortanswer&#039;&#039; : use it to avoid regex syntax at all: just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA engine you can get access to the hinting features. You can skip all that is said on regexes 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;
&#039;&#039;&#039;Matching engine&#039;&#039;&#039; specifies the program module that performs the regex matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use. Engines have different stability and offer different features to use.&lt;br /&gt;
 &lt;br /&gt;
; &#039;&#039;PHP preg extension&#039;&#039; : should be used when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. It is based on the native PHP preg_ functions. It supports 100% perl-compatible regex features, it is very stable and thoroughly tested. But it doesn&#039;t support partial matching, so (unless we storm PHP developers to add support of partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039;. However it supports subpattern capturing. Choose it when you need complex regex features that other engines don&#039;t support.&lt;br /&gt;
; &#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039; : can be used to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for your students. NFA engine is a custom PHP code, it allows many (but not all) regex features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but still may contain bugs in rare cases. Unsupported features for now are lookaround assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;Deterministic finite state automata (DFA)&#039;&#039; : WARNING - this engine lacking support the past year. Use NFA engine instead if you can (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine does, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Hinting is supported by NFA and DFA engines in adaptive and interactive behaviours.&lt;br /&gt;
&lt;br /&gt;
====Partial matching====&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. By partially correct response we understand a string that starts with correct characters (matching your regex) but on some character the match breaks. Assume you entered the regex&lt;br /&gt;
  &amp;quot;&#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&amp;quot;&lt;br /&gt;
and a student answered&lt;br /&gt;
  &amp;quot;they are blue, vhite and red&amp;quot;&lt;br /&gt;
In this situation the partial match is&lt;br /&gt;
  &amp;quot;are blue, &amp;quot;&lt;br /&gt;
Note that the regex is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) so the match may not start with the first character of the student&#039;s response (like in the example above: &amp;quot;they &amp;quot; is skipped). While using just partial matching the student will see the 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;
====General hinting rules====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the REGEXP 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 a hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not usually a desirable behaviour.&lt;br /&gt;
When possible, 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: &amp;quot;,&amp;quot; or &amp;quot; &amp;quot; (leading to the &amp;quot; and&amp;quot; path). The question will choose &amp;quot;,&amp;quot; because it leads to the shortest path to complete the match, while &amp;quot; &amp;quot; 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 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 the 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 grades from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 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 the 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 (word) 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 can be 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 student either completion of the current lexem (if partial match ends inside it) or next one (if student complete the 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 these languages (there will be more):&lt;br /&gt;
; &#039;&#039;simple english&#039;&#039; : english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
; &#039;&#039;C/C++ language&#039;&#039; : a programming language C (or C++);&lt;br /&gt;
; &#039;&#039;printf language&#039;&#039; : a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t the word you would like your students to see on the hinting button. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how your expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regex&lt;br /&gt;
&lt;br /&gt;
===Installation note and known technical issues===&lt;br /&gt;
To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you (or your site admin) have to install Graphviz[http://www.graphviz.org/Graphviz] on the server and fill the &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. Graphviz is used to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
Syntax tree and explaining graph may not work correctly in old Opera versions - for some reason the images are not updated on user actions. Fortunately, there&#039;s a newer version 16 for Windows which works with authoring tools pretty well. On Linux you will have to use something else.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
Here you can edit your regular expression. Clicking on &amp;quot;Show&amp;quot; sends the regex to all tools - syntax tree, explaining graph, description and testing results will be updated. &amp;quot;Save&amp;quot; closes the authoring tools form and saves the regex and test strings in the main question editing form. &amp;quot;Cancel&amp;quot; closes the authoring tools form and discards all changes made there.&lt;br /&gt;
&lt;br /&gt;
You can select part of regular expression there, and corresponding parts of syntax tree, explaining graph, description and matched part of the strings will be highlighted. It is possible to select part of regex text, that doesnt correspond with a logically completed part of regular expression. In that case you selection will be widened to the nearest logically completed part.&lt;br /&gt;
&lt;br /&gt;
===Matching options===&lt;br /&gt;
There you can change options, affecting you matching - matching engine, regex notation, exact matching, and case sensitivity. &lt;br /&gt;
* &#039;&#039;&#039;Matching engine&#039;&#039;&#039; will change the code, performing matching - you could use Testing tool to see if it suit you needs.&lt;br /&gt;
* &#039;&#039;&#039;Regular expression notation&#039;&#039;&#039; will change the way regexes are written - all instruments will show you the difference how this notation is interpreted.&lt;br /&gt;
* &#039;&#039;&#039;Case sensitivity&#039;&#039;&#039; will affect basic case sensitivity of expression, the results you could see in the graph - case insensitive nodes are gray, case sensitive - white.&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; will add new parts to the you regexp to ensure the entire student&#039;s response will match with it. These added parts will be shown on gray background in the tools - see picture below. &lt;br /&gt;
[[Image:qtype preg authortools9.png|exact matching]]&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown in green rectangle. You can select nodes of the tree to by pressing on them.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|part of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
The tree will show you names and numbers of all subexpressions (subpatterns), so you can check their numerations - and back references to it.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools8.png|numbered and named subexpressions in tree]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
Green rectangle shows you selected part of expression.&lt;br /&gt;
[[Image:qtype preg authortools11.png|selection in the tree and graph ]]&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work. Selected part of the expression will be shown by yellow background color.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expected. You will also see green check marks for the strings that match entire regular expressions (and will be graded for that regex) and red crosses for the strings  that don&#039;t give full match. PHP preg matcher can&#039;t show partial matches, so it only shows full matches or nothing (to not mislead you that entire string is wrong).&lt;br /&gt;
&lt;br /&gt;
If you selected a part of regex, you will be able to see what part of strings matches that part (usually in yellow color, but that may depend on you theme). NFA matcher will show that for any part of regex, PHP preg matcher - only for capturing subexpressions (subpatterns).&lt;br /&gt;
&lt;br /&gt;
The strings for testing will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button) and (later) question.&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;[[:alpha:]]&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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;many times*&amp;quot; matches &amp;quot;manytime&amp;quot; followed by zero or more &amp;quot;s&amp;quot;;&lt;br /&gt;
#* &amp;quot;(many times)*&amp;quot; matches &amp;quot;many times&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;first|second|third&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot; or &amp;quot;third&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first |second |)part&amp;quot; matches &amp;quot;first part&amp;quot; or &amp;quot;second part&amp;quot; or just &amp;quot;part&amp;quot; - typical use of an empty alternative (note that space is in alternative to not require it before just &amp;quot;part&amp;quot;);&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;first|second*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;secon&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;secondddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first|second)*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot;, repeated zero or more time in any order, like &amp;quot;firstsecondfirstfirst&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(1|2){2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;12&amp;quot; or &amp;quot;21&amp;quot; or &amp;quot;22&amp;quot;, not just &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot;;&lt;br /&gt;
#* &amp;quot;1{2}|2{2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
An internal structure of regular expression can be viewed well on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;^start|end$&amp;quot; will match &amp;quot;start&amp;quot; from the start of the string or &amp;quot;end&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(start|end)$&amp;quot; using brackets to match exactly with &amp;quot;start&amp;quot; or &amp;quot;end&amp;quot;;&lt;br /&gt;
* &amp;quot;^start$|^end$&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This project is free software, so it&#039;s hard to get any feedback. You shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
This software is considered a scientific project and such things could be really useful and appreciated:&lt;br /&gt;
* an evidence that the results of our work (i.e. Preg questoin type) are really useful to people and were used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you use Preg and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or a journal article;&lt;br /&gt;
* cooperating in writing article or help publishing it in English-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 though. 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 - there are many settings in the question, and regexes can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Plik:qtype_preg_authortools9.png&amp;diff=106857</id>
		<title>Plik:qtype preg authortools9.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools9.png&amp;diff=106857"/>
		<updated>2013-10-12T19:54:25Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Exact matching.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Exact matching.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools11.png&amp;diff=106856</id>
		<title>Plik:qtype preg authortools11.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools11.png&amp;diff=106856"/>
		<updated>2013-10-12T19:39:51Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Selection in the tree and graph.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Selection in the tree and graph.&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools8.png&amp;diff=106855</id>
		<title>Plik:qtype preg authortools8.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools8.png&amp;diff=106855"/>
		<updated>2013-10-12T19:31:32Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: numbered and named subexpressions in tree&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;numbered and named subexpressions in tree&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools71.png&amp;diff=106854</id>
		<title>Plik:qtype preg authortools71.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools71.png&amp;diff=106854"/>
		<updated>2013-10-12T19:26:22Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Oasychev uploaded a new version of &amp;amp;quot;File:qtype preg authortools71.png&amp;amp;quot;: A new tree etc.&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/pl/index.php?title=Plik:qtype_preg_authortools6.jpg&amp;diff=106853</id>
		<title>Plik:qtype preg authortools6.jpg</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools6.jpg&amp;diff=106853"/>
		<updated>2013-10-12T19:24:50Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Oasychev uploaded a new version of &amp;amp;quot;File:qtype preg authortools6.jpg&amp;amp;quot;: A new quantifier title&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/pl/index.php?title=Plik:qtype_preg_authortools6.jpg&amp;diff=106852</id>
		<title>Plik:qtype preg authortools6.jpg</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools6.jpg&amp;diff=106852"/>
		<updated>2013-10-12T19:23:59Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Oasychev uploaded a new version of &amp;amp;quot;File:qtype preg authortools6.jpg&amp;amp;quot;: A new title for quantifier&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/pl/index.php?title=Plik:qtype_preg_authortools4.png&amp;diff=106851</id>
		<title>Plik:qtype preg authortools4.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools4.png&amp;diff=106851"/>
		<updated>2013-10-12T19:21:09Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Oasychev uploaded a new version of &amp;amp;quot;File:qtype preg authortools4.png&amp;amp;quot;: A smaller version, without unnecessary fields.&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/pl/index.php?title=Plik:qtype_preg_authortools3.jpg&amp;diff=106850</id>
		<title>Plik:qtype preg authortools3.jpg</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools3.jpg&amp;diff=106850"/>
		<updated>2013-10-12T19:15:18Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Oasychev uploaded a new version of &amp;amp;quot;File:qtype preg authortools3.jpg&amp;amp;quot;: A new version of selection.&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/pl/index.php?title=Plik:qtype_preg_authortools2.png&amp;diff=106849</id>
		<title>Plik:qtype preg authortools2.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools2.png&amp;diff=106849"/>
		<updated>2013-10-12T19:12:14Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Oasychev uploaded a new version of &amp;amp;quot;File:qtype preg authortools2.png&amp;amp;quot;: Updated quantifier title&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/pl/index.php?title=Preg_question_type&amp;diff=106841</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106841"/>
		<updated>2013-10-11T17:21:39Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Authoring tools */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting, error reporting, regular expression testing (authoring tool) - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, matchers testing, backup&amp;amp;restore, unicode support, selection in regex text (in authoring tools) - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine (deprecated for now) - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Syntax tree (authoring tool) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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. &lt;br /&gt;
Thanks to:&lt;br /&gt;
*  Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that have been implemented in Preg question type;&lt;br /&gt;
* Tim Hunt - for his polite and useful answers and commentaries that helped writing this question, also for joint work on extra_question_fields and extra_answer_fields code, that is useful to many question type developers;&lt;br /&gt;
* Bondarenko Vitaly - for conversion of a vast set of regular expression matching tests.&lt;br /&gt;
You, too, could aways [[#The ways to give back|help us]] a lot - regardless of the way you use Preg and your capabilities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;Pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers&lt;br /&gt;
* &#039;&#039;&#039;Hinting&#039;&#039;&#039; - when students are stuck doing the question, you may allow them to ask for a next correct word (lexem) or a character (with possible penalty)&lt;br /&gt;
 &lt;br /&gt;
===Settings affecting question work===&lt;br /&gt;
Sets the case sensitivity for all regular expressions you specify as answers. Note that you can also [[#Local case-sensitivity modifiers|set the case sensitivity for regex parts]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Exact matching&#039;&#039;&#039; affects the question in the following way:&lt;br /&gt;
; &#039;&#039;Yes&#039;&#039; : the &#039;&#039;entire&#039;&#039; student&#039;s response, from the first to the last letter, should match your regular expression&lt;br /&gt;
; &#039;&#039;No&#039;&#039; : student&#039;s response can just contain a &#039;&#039;part&#039;&#039; that matches your regex: for example, if the correct answer is &amp;quot;whole&amp;quot; then &amp;quot;the whole string&amp;quot; will be a correct student response&lt;br /&gt;
 &lt;br /&gt;
You still can set some of your regexes to match the whole student&#039;s response using [[#Anchoring|special regex syntax]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Notations&#039;&#039;&#039; specify the &amp;quot;language&amp;quot; of your answers.&lt;br /&gt;
; &#039;&#039;Regular expression&#039;&#039; : a usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;Regular expression (extended)&#039;&#039; : useful for really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not inside character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything after &#039;#&#039; character untill the end of string is treated as commentary (# should not be escaped and should not be inside a character class).&lt;br /&gt;
; &#039;&#039;Moodle shortanswer&#039;&#039; : use it to avoid regex syntax at all: just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA engine you can get access to the hinting features. You can skip all that is said on regexes 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;
&#039;&#039;&#039;Matching engine&#039;&#039;&#039; specifies the program module that performs the regex matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use. Engines have different stability and offer different features to use.&lt;br /&gt;
 &lt;br /&gt;
; &#039;&#039;PHP preg extension&#039;&#039; : should be used when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. It is based on the native PHP preg_ functions. It supports 100% perl-compatible regex features, it is very stable and thoroughly tested. But it doesn&#039;t support partial matching, so (unless we storm PHP developers to add support of partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039;. However it supports subpattern capturing. Choose it when you need complex regex features that other engines don&#039;t support.&lt;br /&gt;
; &#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039; : can be used to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for your students. NFA engine is a custom PHP code, it allows many (but not all) regex features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but still may contain bugs in rare cases. Unsupported features for now are lookaround assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;Deterministic finite state automata (DFA)&#039;&#039; : WARNING - this engine lacking support the past year. Use NFA engine instead if you can (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine does, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Hinting is supported by NFA and DFA engines in adaptive and interactive behaviours.&lt;br /&gt;
&lt;br /&gt;
====Partial matching====&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. By partially correct response we understand a string that starts with correct characters (matching your regex) but on some character the match breaks. Assume you entered the regex&lt;br /&gt;
  &amp;quot;&#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&amp;quot;&lt;br /&gt;
and a student answered&lt;br /&gt;
  &amp;quot;they are blue, vhite and red&amp;quot;&lt;br /&gt;
In this situation the partial match is&lt;br /&gt;
  &amp;quot;are blue, &amp;quot;&lt;br /&gt;
Note that the regex is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) so the match may not start with the first character of the student&#039;s response (like in the example above: &amp;quot;they &amp;quot; is skipped). While using just partial matching the student will see the 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;
====General hinting rules====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the REGEXP 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 a hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not usually a desirable behaviour.&lt;br /&gt;
When possible, 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: &amp;quot;,&amp;quot; or &amp;quot; &amp;quot; (leading to the &amp;quot; and&amp;quot; path). The question will choose &amp;quot;,&amp;quot; because it leads to the shortest path to complete the match, while &amp;quot; &amp;quot; 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 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 the 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 grades from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 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 the 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 (word) 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 can be 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 student either completion of the current lexem (if partial match ends inside it) or next one (if student complete the 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 these languages (there will be more):&lt;br /&gt;
; &#039;&#039;simple english&#039;&#039; : english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
; &#039;&#039;C/C++ language&#039;&#039; : a programming language C (or C++);&lt;br /&gt;
; &#039;&#039;printf language&#039;&#039; : a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t the word you would like your students to see on the hinting button. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how your expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regex&lt;br /&gt;
&lt;br /&gt;
===Installation note and known technical issues===&lt;br /&gt;
To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you (or your site admin) have to install Graphviz[http://www.graphviz.org/Graphviz] on the server and fill the &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. Graphviz is used to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
Syntax tree and explaining graph may not work correctly in old Opera versions - for some reason the images are not updated on user actions. Fortunately, there&#039;s a newer version 16 for Windows which works with authoring tools pretty well. On Linux you will have to use something else.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
Here you can edit your regular expression. Clicking on &amp;quot;Show&amp;quot; sends the regex to all tools - syntax tree, explaining graph, description and testing results will be updated. &amp;quot;Save&amp;quot; closes the authoring tools form and saves the regex and test strings in the main question editing form. &amp;quot;Cancel&amp;quot; closes the authoring tools form and discards all changes made there.&lt;br /&gt;
&lt;br /&gt;
You can select part of regular expression there, and corresponding parts of syntax tree, explaining graph, description and matched part of the strings will be highlighted. It is possible to select part of regex text, that doesnt correspond with a logically completed part of regular expression. In that case you selection will be widened to the nearest logically completed part.&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown in green rectangle. You can select nodes of the tree to by pressing on them.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
The tree will show you names and numbers of all subexpressions (subpatterns), so you can check their numerations - and back references to it.&lt;br /&gt;
  TODO - picture.&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work. Selected part of the expression will be shown by yellow background color.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expected. You will also see green check marks for the strings that match entire regular expressions (and will be graded for that regex) and red crosses for the strings  that don&#039;t give full match. PHP preg matcher can&#039;t show partial matches, so it only shows full matches or nothing (to not mislead you that entire string is wrong).&lt;br /&gt;
&lt;br /&gt;
If you selected a part of regex, you will be able to see what part of strings matches that part (usually in yellow color, but that may depend on you theme). NFA matcher will show that for any part of regex, PHP preg matcher - only for capturing subexpressions (subpatterns).&lt;br /&gt;
&lt;br /&gt;
The strings for testing will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button) and (later) question.&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;[[:alpha:]]&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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;many times*&amp;quot; matches &amp;quot;manytime&amp;quot; followed by zero or more &amp;quot;s&amp;quot;;&lt;br /&gt;
#* &amp;quot;(many times)*&amp;quot; matches &amp;quot;many times&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;first|second|third&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot; or &amp;quot;third&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first |second |)part&amp;quot; matches &amp;quot;first part&amp;quot; or &amp;quot;second part&amp;quot; or just &amp;quot;part&amp;quot; - typical use of an empty alternative (note that space is in alternative to not require it before just &amp;quot;part&amp;quot;);&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;first|second*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;secon&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;secondddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first|second)*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot;, repeated zero or more time in any order, like &amp;quot;firstsecondfirstfirst&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(1|2){2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;12&amp;quot; or &amp;quot;21&amp;quot; or &amp;quot;22&amp;quot;, not just &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot;;&lt;br /&gt;
#* &amp;quot;1{2}|2{2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
An internal structure of regular expression can be viewed well on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;^start|end$&amp;quot; will match &amp;quot;start&amp;quot; from the start of the string or &amp;quot;end&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(start|end)$&amp;quot; using brackets to match exactly with &amp;quot;start&amp;quot; or &amp;quot;end&amp;quot;;&lt;br /&gt;
* &amp;quot;^start$|^end$&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This project is free software, so it&#039;s hard to get any feedback. You shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
This software is considered a scientific project and such things could be really useful and appreciated:&lt;br /&gt;
* an evidence that the results of our work (i.e. Preg questoin type) are really useful to people and were used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you use Preg and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or a journal article;&lt;br /&gt;
* cooperating in writing article or help publishing it in English-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 though. 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 - there are many settings in the question, and regexes can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Plik:qtype_preg_authortools4.png&amp;diff=106840</id>
		<title>Plik:qtype preg authortools4.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools4.png&amp;diff=106840"/>
		<updated>2013-10-11T17:09:45Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Oasychev uploaded a new version of &amp;amp;quot;File:qtype preg authortools4.png&amp;amp;quot;: Update to new version of tools.&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/pl/index.php?title=Plik:qtype_preg_authortools2.png&amp;diff=106839</id>
		<title>Plik:qtype preg authortools2.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools2.png&amp;diff=106839"/>
		<updated>2013-10-11T17:00:56Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Oasychev uploaded a new version of &amp;amp;quot;File:qtype preg authortools2.png&amp;amp;quot;: Updated to the new state of authoring tools.&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/pl/index.php?title=Preg_question_type&amp;diff=106764</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106764"/>
		<updated>2013-10-05T22:49:06Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Regular expression area */ - updated selection information&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting, error reporting, regular expression testing (authoring tool) - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, matchers testing, backup&amp;amp;restore, unicode support, selection in regex text (in authoring tools) - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine (deprecated for now) - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Syntax tree (authoring tool) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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. &lt;br /&gt;
Thanks to:&lt;br /&gt;
*  Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that have been implemented in Preg question type;&lt;br /&gt;
* Tim Hunt - for his polite and useful answers and commentaries that helped writing this question, also for joint work on extra_question_fields and extra_answer_fields code, that is useful to many question type developers;&lt;br /&gt;
* Bondarenko Vitaly - for conversion of a vast set of regular expression matching tests.&lt;br /&gt;
You, too, could aways [[#The ways to give back|help us]] a lot - regardless of the way you use Preg and your capabilities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;Pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers&lt;br /&gt;
* &#039;&#039;&#039;Hinting&#039;&#039;&#039; - when students are stuck doing the question, you may allow them to ask for a next correct word (lexem) or a character (with possible penalty)&lt;br /&gt;
 &lt;br /&gt;
===Settings affecting question work===&lt;br /&gt;
Sets the case sensitivity for all regular expressions you specify as answers. Note that you can also [[#Local case-sensitivity modifiers|set the case sensitivity for regex parts]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Exact matching&#039;&#039;&#039; affects the question in the following way:&lt;br /&gt;
; &#039;&#039;Yes&#039;&#039; : the &#039;&#039;entire&#039;&#039; student&#039;s response, from the first to the last letter, should match your regular expression&lt;br /&gt;
; &#039;&#039;No&#039;&#039; : student&#039;s response can just contain a &#039;&#039;part&#039;&#039; that matches your regex: for example, if the correct answer is &amp;quot;whole&amp;quot; then &amp;quot;the whole string&amp;quot; will be a correct student response&lt;br /&gt;
 &lt;br /&gt;
You still can set some of your regexes to match the whole student&#039;s response using [[#Anchoring|special regex syntax]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Notations&#039;&#039;&#039; specify the &amp;quot;language&amp;quot; of your answers.&lt;br /&gt;
; &#039;&#039;Regular expression&#039;&#039; : a usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;Regular expression (extended)&#039;&#039; : useful for really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not inside character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything after &#039;#&#039; character untill the end of string is treated as commentary (# should not be escaped and should not be inside a character class).&lt;br /&gt;
; &#039;&#039;Moodle shortanswer&#039;&#039; : use it to avoid regex syntax at all: just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA engine you can get access to the hinting features. You can skip all that is said on regexes 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;
&#039;&#039;&#039;Matching engine&#039;&#039;&#039; specifies the program module that performs the regex matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use. Engines have different stability and offer different features to use.&lt;br /&gt;
 &lt;br /&gt;
; &#039;&#039;PHP preg extension&#039;&#039; : should be used when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. It is based on the native PHP preg_ functions. It supports 100% perl-compatible regex features, it is very stable and thoroughly tested. But it doesn&#039;t support partial matching, so (unless we storm PHP developers to add support of partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039;. However it supports subpattern capturing. Choose it when you need complex regex features that other engines don&#039;t support.&lt;br /&gt;
; &#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039; : can be used to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for your students. NFA engine is a custom PHP code, it allows many (but not all) regex features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but still may contain bugs in rare cases. Unsupported features for now are lookaround assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;Deterministic finite state automata (DFA)&#039;&#039; : WARNING - this engine lacking support the past year. Use NFA engine instead if you can (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine does, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Hinting is supported by NFA and DFA engines in adaptive and interactive behaviours.&lt;br /&gt;
&lt;br /&gt;
====Partial matching====&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. By partially correct response we understand a string that starts with correct characters (matching your regex) but on some character the match breaks. Assume you entered the regex&lt;br /&gt;
  &amp;quot;&#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&amp;quot;&lt;br /&gt;
and a student answered&lt;br /&gt;
  &amp;quot;they are blue, vhite and red&amp;quot;&lt;br /&gt;
In this situation the partial match is&lt;br /&gt;
  &amp;quot;are blue, &amp;quot;&lt;br /&gt;
Note that the regex is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) so the match may not start with the first character of the student&#039;s response (like in the example above: &amp;quot;they &amp;quot; is skipped). While using just partial matching the student will see the 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;
====General hinting rules====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the REGEXP 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 a hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not usually a desirable behaviour.&lt;br /&gt;
When possible, 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: &amp;quot;,&amp;quot; or &amp;quot; &amp;quot; (leading to the &amp;quot; and&amp;quot; path). The question will choose &amp;quot;,&amp;quot; because it leads to the shortest path to complete the match, while &amp;quot; &amp;quot; 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 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 the 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 grades from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 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 the 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 (word) 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 can be 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 student either completion of the current lexem (if partial match ends inside it) or next one (if student complete the 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 these languages (there will be more):&lt;br /&gt;
; &#039;&#039;simple english&#039;&#039; : english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
; &#039;&#039;C/C++ language&#039;&#039; : a programming language C (or C++);&lt;br /&gt;
; &#039;&#039;printf language&#039;&#039; : a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t the word you would like your students to see on the hinting button. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how your expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regex&lt;br /&gt;
&lt;br /&gt;
===Installation note and known technical issues===&lt;br /&gt;
To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you (or your site admin) have to install Graphviz[http://www.graphviz.org/Graphviz] on the server and fill the &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. Graphviz is used to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
Syntax tree and explaining graph may not work correctly in old Opera versions - for some reason the images are not updated on user actions. Fortunately, there&#039;s a newer version 16 for Windows which works with authoring tools pretty well. On Linux you will have to use something else.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
Here you can edit your regular expression. Clicking on &amp;quot;Show&amp;quot; sends the regex to all tools - syntax tree, explaining graph, description and testing results will be updated. &amp;quot;Save&amp;quot; closes the authoring tools form and saves the regex and test strings in the main question editing form. &amp;quot;Cancel&amp;quot; closes the authoring tools form and discards all changes made there.&lt;br /&gt;
&lt;br /&gt;
You can select part of regular expression there, and corresponding parts of syntax tree, explaining graph, description and matched part of the strings will be highlighted. It is possible to select part of regex text, that doesnt correspond with a logically completed part of regular expression. In that case you selection will be widened to the nearest logically completed part.&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see a coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expected. You will also see green check mark for the strings that match entire regular expressions and red crosses for the strings (and will be graded for that regex), that doesn&#039;t give full match. PHP preg matcher can&#039;t show partial matches, so it only shows full matches or nothing (to not mislead you that entire string is wrong).&lt;br /&gt;
&lt;br /&gt;
If you selected part of regex, you will be able to see what part of strings matches with that part of regex (usually in yellow color, but that may depend on you theme). NFA matcher will show that for any part of regex, PHP preg matcher - only for the capturing subexpressions (subpattern).&lt;br /&gt;
&lt;br /&gt;
The strings for testing will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button) and (later) question.&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;[[:alpha:]]&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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;many times*&amp;quot; matches &amp;quot;manytime&amp;quot; followed by zero or more &amp;quot;s&amp;quot;;&lt;br /&gt;
#* &amp;quot;(many times)*&amp;quot; matches &amp;quot;many times&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;first|second|third&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot; or &amp;quot;third&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first |second |)part&amp;quot; matches &amp;quot;first part&amp;quot; or &amp;quot;second part&amp;quot; or just &amp;quot;part&amp;quot; - typical use of an empty alternative (note that space is in alternative to not require it before just &amp;quot;part&amp;quot;);&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;first|second*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;secon&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;secondddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first|second)*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot;, repeated zero or more time in any order, like &amp;quot;firstsecondfirstfirst&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(1|2){2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;12&amp;quot; or &amp;quot;21&amp;quot; or &amp;quot;22&amp;quot;, not just &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot;;&lt;br /&gt;
#* &amp;quot;1{2}|2{2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
An internal structure of regular expression can be viewed well on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;^start|end$&amp;quot; will match &amp;quot;start&amp;quot; from the start of the string or &amp;quot;end&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(start|end)$&amp;quot; using brackets to match exactly with &amp;quot;start&amp;quot; or &amp;quot;end&amp;quot;;&lt;br /&gt;
* &amp;quot;^start$|^end$&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This project is free software, so it&#039;s hard to get any feedback. You shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
This software is considered a scientific project and such things could be really useful and appreciated:&lt;br /&gt;
* an evidence that the results of our work (i.e. Preg questoin type) are really useful to people and were used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you use Preg and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or a journal article;&lt;br /&gt;
* cooperating in writing article or help publishing it in English-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 though. 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 - there are many settings in the question, and regexes can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106763</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106763"/>
		<updated>2013-10-05T22:41:08Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Testing tool */ - added information about selection and icons&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting, error reporting, regular expression testing (authoring tool) - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, matchers testing, backup&amp;amp;restore, unicode support, selection in regex text (in authoring tools) - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine (deprecated for now) - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Syntax tree (authoring tool) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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. &lt;br /&gt;
Thanks to:&lt;br /&gt;
*  Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that have been implemented in Preg question type;&lt;br /&gt;
* Tim Hunt - for his polite and useful answers and commentaries that helped writing this question, also for joint work on extra_question_fields and extra_answer_fields code, that is useful to many question type developers;&lt;br /&gt;
* Bondarenko Vitaly - for conversion of a vast set of regular expression matching tests.&lt;br /&gt;
You, too, could aways [[#The ways to give back|help us]] a lot - regardless of the way you use Preg and your capabilities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;Pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers&lt;br /&gt;
* &#039;&#039;&#039;Hinting&#039;&#039;&#039; - when students are stuck doing the question, you may allow them to ask for a next correct word (lexem) or a character (with possible penalty)&lt;br /&gt;
 &lt;br /&gt;
===Settings affecting question work===&lt;br /&gt;
Sets the case sensitivity for all regular expressions you specify as answers. Note that you can also [[#Local case-sensitivity modifiers|set the case sensitivity for regex parts]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Exact matching&#039;&#039;&#039; affects the question in the following way:&lt;br /&gt;
; &#039;&#039;Yes&#039;&#039; : the &#039;&#039;entire&#039;&#039; student&#039;s response, from the first to the last letter, should match your regular expression&lt;br /&gt;
; &#039;&#039;No&#039;&#039; : student&#039;s response can just contain a &#039;&#039;part&#039;&#039; that matches your regex: for example, if the correct answer is &amp;quot;whole&amp;quot; then &amp;quot;the whole string&amp;quot; will be a correct student response&lt;br /&gt;
 &lt;br /&gt;
You still can set some of your regexes to match the whole student&#039;s response using [[#Anchoring|special regex syntax]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Notations&#039;&#039;&#039; specify the &amp;quot;language&amp;quot; of your answers.&lt;br /&gt;
; &#039;&#039;Regular expression&#039;&#039; : a usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;Regular expression (extended)&#039;&#039; : useful for really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not inside character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything after &#039;#&#039; character untill the end of string is treated as commentary (# should not be escaped and should not be inside a character class).&lt;br /&gt;
; &#039;&#039;Moodle shortanswer&#039;&#039; : use it to avoid regex syntax at all: just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA engine you can get access to the hinting features. You can skip all that is said on regexes 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;
&#039;&#039;&#039;Matching engine&#039;&#039;&#039; specifies the program module that performs the regex matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use. Engines have different stability and offer different features to use.&lt;br /&gt;
 &lt;br /&gt;
; &#039;&#039;PHP preg extension&#039;&#039; : should be used when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. It is based on the native PHP preg_ functions. It supports 100% perl-compatible regex features, it is very stable and thoroughly tested. But it doesn&#039;t support partial matching, so (unless we storm PHP developers to add support of partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039;. However it supports subpattern capturing. Choose it when you need complex regex features that other engines don&#039;t support.&lt;br /&gt;
; &#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039; : can be used to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for your students. NFA engine is a custom PHP code, it allows many (but not all) regex features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but still may contain bugs in rare cases. Unsupported features for now are lookaround assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;Deterministic finite state automata (DFA)&#039;&#039; : WARNING - this engine lacking support the past year. Use NFA engine instead if you can (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine does, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Hinting is supported by NFA and DFA engines in adaptive and interactive behaviours.&lt;br /&gt;
&lt;br /&gt;
====Partial matching====&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. By partially correct response we understand a string that starts with correct characters (matching your regex) but on some character the match breaks. Assume you entered the regex&lt;br /&gt;
  &amp;quot;&#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&amp;quot;&lt;br /&gt;
and a student answered&lt;br /&gt;
  &amp;quot;they are blue, vhite and red&amp;quot;&lt;br /&gt;
In this situation the partial match is&lt;br /&gt;
  &amp;quot;are blue, &amp;quot;&lt;br /&gt;
Note that the regex is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) so the match may not start with the first character of the student&#039;s response (like in the example above: &amp;quot;they &amp;quot; is skipped). While using just partial matching the student will see the 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;
====General hinting rules====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the REGEXP 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 a hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not usually a desirable behaviour.&lt;br /&gt;
When possible, 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: &amp;quot;,&amp;quot; or &amp;quot; &amp;quot; (leading to the &amp;quot; and&amp;quot; path). The question will choose &amp;quot;,&amp;quot; because it leads to the shortest path to complete the match, while &amp;quot; &amp;quot; 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 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 the 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 grades from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 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 the 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 (word) 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 can be 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 student either completion of the current lexem (if partial match ends inside it) or next one (if student complete the 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 these languages (there will be more):&lt;br /&gt;
; &#039;&#039;simple english&#039;&#039; : english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
; &#039;&#039;C/C++ language&#039;&#039; : a programming language C (or C++);&lt;br /&gt;
; &#039;&#039;printf language&#039;&#039; : a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t the word you would like your students to see on the hinting button. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how your expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regex&lt;br /&gt;
&lt;br /&gt;
===Installation note and known technical issues===&lt;br /&gt;
To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you (or your site admin) have to install Graphviz[http://www.graphviz.org/Graphviz] on the server and fill the &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. Graphviz is used to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
Syntax tree and explaining graph may not work correctly in old Opera versions - for some reason the images are not updated on user actions. Fortunately, there&#039;s a newer version 16 for Windows which works with authoring tools pretty well. On Linux you will have to use something else.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
Here you can edit your regular expression. Clicking on &amp;quot;Update&amp;quot; sends the regex to all tools - so syntax tree, explaining graph, description and testing results are updated. &amp;quot;Save&amp;quot; closes the authoring tools form and saves the regex in the main question editing form. &amp;quot;Cancel&amp;quot; closes the authoring tools form and discards all changes made there. The last, &amp;quot;TODO&amp;quot; button, helps you to trace the interrelation between the regex itself (text representation) and the other representations: you can select a regex part and see where it is located in the syntax tree and in the graph.&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see a coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expected. You will also see green check mark for the strings that match entire regular expressions and red crosses for the strings (and will be graded for that regex), that doesn&#039;t give full match. PHP preg matcher can&#039;t show partial matches, so it only shows full matches or nothing (to not mislead you that entire string is wrong).&lt;br /&gt;
&lt;br /&gt;
If you selected part of regex, you will be able to see what part of strings matches with that part of regex (usually in yellow color, but that may depend on you theme). NFA matcher will show that for any part of regex, PHP preg matcher - only for the capturing subexpressions (subpattern).&lt;br /&gt;
&lt;br /&gt;
The strings for testing will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button) and (later) question.&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;[[:alpha:]]&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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;many times*&amp;quot; matches &amp;quot;manytime&amp;quot; followed by zero or more &amp;quot;s&amp;quot;;&lt;br /&gt;
#* &amp;quot;(many times)*&amp;quot; matches &amp;quot;many times&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;first|second|third&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot; or &amp;quot;third&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first |second |)part&amp;quot; matches &amp;quot;first part&amp;quot; or &amp;quot;second part&amp;quot; or just &amp;quot;part&amp;quot; - typical use of an empty alternative (note that space is in alternative to not require it before just &amp;quot;part&amp;quot;);&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;first|second*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;secon&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;secondddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first|second)*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot;, repeated zero or more time in any order, like &amp;quot;firstsecondfirstfirst&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(1|2){2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;12&amp;quot; or &amp;quot;21&amp;quot; or &amp;quot;22&amp;quot;, not just &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot;;&lt;br /&gt;
#* &amp;quot;1{2}|2{2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
An internal structure of regular expression can be viewed well on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;^start|end$&amp;quot; will match &amp;quot;start&amp;quot; from the start of the string or &amp;quot;end&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(start|end)$&amp;quot; using brackets to match exactly with &amp;quot;start&amp;quot; or &amp;quot;end&amp;quot;;&lt;br /&gt;
* &amp;quot;^start$|^end$&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This project is free software, so it&#039;s hard to get any feedback. You shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
This software is considered a scientific project and such things could be really useful and appreciated:&lt;br /&gt;
* an evidence that the results of our work (i.e. Preg questoin type) are really useful to people and were used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you use Preg and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or a journal article;&lt;br /&gt;
* cooperating in writing article or help publishing it in English-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 though. 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 - there are many settings in the question, and regexes can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106762</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106762"/>
		<updated>2013-10-05T22:22:35Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Changed developers information&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting, error reporting, regular expression testing (authoring tool) - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, matchers testing, backup&amp;amp;restore, unicode support, selection in regex text (in authoring tools) - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine (deprecated for now) - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Syntax tree (authoring tool) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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. &lt;br /&gt;
Thanks to:&lt;br /&gt;
*  Joseph Rezeau for being devoted tester of Preg question type releases and being the original author of many ideas that have been implemented in Preg question type;&lt;br /&gt;
* Tim Hunt - for his polite and useful answers and commentaries that helped writing this question, also for joint work on extra_question_fields and extra_answer_fields code, that is useful to many question type developers;&lt;br /&gt;
* Bondarenko Vitaly - for conversion of a vast set of regular expression matching tests.&lt;br /&gt;
You, too, could aways [[#The ways to give back|help us]] a lot - regardless of the way you use Preg and your capabilities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;Pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers&lt;br /&gt;
* &#039;&#039;&#039;Hinting&#039;&#039;&#039; - when students are stuck doing the question, you may allow them to ask for a next correct word (lexem) or a character (with possible penalty)&lt;br /&gt;
 &lt;br /&gt;
===Settings affecting question work===&lt;br /&gt;
Sets the case sensitivity for all regular expressions you specify as answers. Note that you can also [[#Local case-sensitivity modifiers|set the case sensitivity for regex parts]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Exact matching&#039;&#039;&#039; affects the question in the following way:&lt;br /&gt;
; &#039;&#039;Yes&#039;&#039; : the &#039;&#039;entire&#039;&#039; student&#039;s response, from the first to the last letter, should match your regular expression&lt;br /&gt;
; &#039;&#039;No&#039;&#039; : student&#039;s response can just contain a &#039;&#039;part&#039;&#039; that matches your regex: for example, if the correct answer is &amp;quot;whole&amp;quot; then &amp;quot;the whole string&amp;quot; will be a correct student response&lt;br /&gt;
 &lt;br /&gt;
You still can set some of your regexes to match the whole student&#039;s response using [[#Anchoring|special regex syntax]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Notations&#039;&#039;&#039; specify the &amp;quot;language&amp;quot; of your answers.&lt;br /&gt;
; &#039;&#039;Regular expression&#039;&#039; : a usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;Regular expression (extended)&#039;&#039; : useful for really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not inside character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything after &#039;#&#039; character untill the end of string is treated as commentary (# should not be escaped and should not be inside a character class).&lt;br /&gt;
; &#039;&#039;Moodle shortanswer&#039;&#039; : use it to avoid regex syntax at all: just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA engine you can get access to the hinting features. You can skip all that is said on regexes 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;
&#039;&#039;&#039;Matching engine&#039;&#039;&#039; specifies the program module that performs the regex matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use. Engines have different stability and offer different features to use.&lt;br /&gt;
 &lt;br /&gt;
; &#039;&#039;PHP preg extension&#039;&#039; : should be used when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. It is based on the native PHP preg_ functions. It supports 100% perl-compatible regex features, it is very stable and thoroughly tested. But it doesn&#039;t support partial matching, so (unless we storm PHP developers to add support of partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039;. However it supports subpattern capturing. Choose it when you need complex regex features that other engines don&#039;t support.&lt;br /&gt;
; &#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039; : can be used to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for your students. NFA engine is a custom PHP code, it allows many (but not all) regex features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but still may contain bugs in rare cases. Unsupported features for now are lookaround assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;Deterministic finite state automata (DFA)&#039;&#039; : WARNING - this engine lacking support the past year. Use NFA engine instead if you can (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine does, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Hinting is supported by NFA and DFA engines in adaptive and interactive behaviours.&lt;br /&gt;
&lt;br /&gt;
====Partial matching====&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. By partially correct response we understand a string that starts with correct characters (matching your regex) but on some character the match breaks. Assume you entered the regex&lt;br /&gt;
  &amp;quot;&#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&amp;quot;&lt;br /&gt;
and a student answered&lt;br /&gt;
  &amp;quot;they are blue, vhite and red&amp;quot;&lt;br /&gt;
In this situation the partial match is&lt;br /&gt;
  &amp;quot;are blue, &amp;quot;&lt;br /&gt;
Note that the regex is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) so the match may not start with the first character of the student&#039;s response (like in the example above: &amp;quot;they &amp;quot; is skipped). While using just partial matching the student will see the 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;
====General hinting rules====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the REGEXP 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 a hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not usually a desirable behaviour.&lt;br /&gt;
When possible, 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: &amp;quot;,&amp;quot; or &amp;quot; &amp;quot; (leading to the &amp;quot; and&amp;quot; path). The question will choose &amp;quot;,&amp;quot; because it leads to the shortest path to complete the match, while &amp;quot; &amp;quot; 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 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 the 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 grades from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 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 the 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 (word) 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 can be 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 student either completion of the current lexem (if partial match ends inside it) or next one (if student complete the 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 these languages (there will be more):&lt;br /&gt;
; &#039;&#039;simple english&#039;&#039; : english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
; &#039;&#039;C/C++ language&#039;&#039; : a programming language C (or C++);&lt;br /&gt;
; &#039;&#039;printf language&#039;&#039; : a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t the word you would like your students to see on the hinting button. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how your expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regex&lt;br /&gt;
&lt;br /&gt;
===Installation note and known technical issues===&lt;br /&gt;
To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you (or your site admin) have to install Graphviz[http://www.graphviz.org/Graphviz] on the server and fill the &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. Graphviz is used to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
Syntax tree and explaining graph may not work correctly in old Opera versions - for some reason the images are not updated on user actions. Fortunately, there&#039;s a newer version 16 for Windows which works with authoring tools pretty well. On Linux you will have to use something else.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
Here you can edit your regular expression. Clicking on &amp;quot;Update&amp;quot; sends the regex to all tools - so syntax tree, explaining graph, description and testing results are updated. &amp;quot;Save&amp;quot; closes the authoring tools form and saves the regex in the main question editing form. &amp;quot;Cancel&amp;quot; closes the authoring tools form and discards all changes made there. The last, &amp;quot;TODO&amp;quot; button, helps you to trace the interrelation between the regex itself (text representation) and the other representations: you can select a regex part and see where it is located in the syntax tree and in the graph.&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see a coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;[[:alpha:]]&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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;many times*&amp;quot; matches &amp;quot;manytime&amp;quot; followed by zero or more &amp;quot;s&amp;quot;;&lt;br /&gt;
#* &amp;quot;(many times)*&amp;quot; matches &amp;quot;many times&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;first|second|third&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot; or &amp;quot;third&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first |second |)part&amp;quot; matches &amp;quot;first part&amp;quot; or &amp;quot;second part&amp;quot; or just &amp;quot;part&amp;quot; - typical use of an empty alternative (note that space is in alternative to not require it before just &amp;quot;part&amp;quot;);&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;first|second*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;secon&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;secondddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first|second)*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot;, repeated zero or more time in any order, like &amp;quot;firstsecondfirstfirst&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(1|2){2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;12&amp;quot; or &amp;quot;21&amp;quot; or &amp;quot;22&amp;quot;, not just &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot;;&lt;br /&gt;
#* &amp;quot;1{2}|2{2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
An internal structure of regular expression can be viewed well on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;^start|end$&amp;quot; will match &amp;quot;start&amp;quot; from the start of the string or &amp;quot;end&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(start|end)$&amp;quot; using brackets to match exactly with &amp;quot;start&amp;quot; or &amp;quot;end&amp;quot;;&lt;br /&gt;
* &amp;quot;^start$|^end$&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This project is free software, so it&#039;s hard to get any feedback. You shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
This software is considered a scientific project and such things could be really useful and appreciated:&lt;br /&gt;
* an evidence that the results of our work (i.e. Preg questoin type) are really useful to people and were used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you use Preg and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or a journal article;&lt;br /&gt;
* cooperating in writing article or help publishing it in English-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 though. 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 - there are many settings in the question, and regexes can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106558</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106558"/>
		<updated>2013-09-07T19:26:32Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Syntax tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
You, too, could aways [[#The ways to give back|help us]] a lot - regardless of the way you use Preg and your capabilities.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;Pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers&lt;br /&gt;
* &#039;&#039;&#039;Hinting&#039;&#039;&#039; - when students are stuck doing the question, you may allow them to ask for a next correct word (lexem) or a character (with possible penalty)&lt;br /&gt;
 &lt;br /&gt;
===Settings affecting question work===&lt;br /&gt;
Sets the case sensitivity for all regular expressions you specify as answers. Note that you can also [[#Local case-sensitivity modifiers|set the case sensitivity for regex parts]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Exact matching&#039;&#039;&#039; affects the question in the following way:&lt;br /&gt;
; &#039;&#039;Yes&#039;&#039; : the &#039;&#039;entire&#039;&#039; student&#039;s response, from the first to the last letter, should match your regular expression&lt;br /&gt;
; &#039;&#039;No&#039;&#039; : student&#039;s response can just contain a &#039;&#039;part&#039;&#039; that matches your regex: for example, if the correct answer is &amp;quot;whole&amp;quot; then &amp;quot;the whole string&amp;quot; will be a correct student response&lt;br /&gt;
 &lt;br /&gt;
You still can set some of your regexes to match the whole student&#039;s response using [[#Anchoring|special regex syntax]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Notations&#039;&#039;&#039; specify the &amp;quot;language&amp;quot; of your answers.&lt;br /&gt;
; &#039;&#039;Regular expression&#039;&#039; : a usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;Regular expression (extended)&#039;&#039; : useful for really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not inside character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything after &#039;#&#039; character untill the end of string is treated as commentary (# should not be escaped and should not be inside a character class).&lt;br /&gt;
; &#039;&#039;Moodle shortanswer&#039;&#039; : use it to avoid regex syntax at all: just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA engine you can get access to the hinting features. You can skip all that is said on regexes 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;
&#039;&#039;&#039;Matching engine&#039;&#039;&#039; specifies the program module that performs the regex matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use. Engines have different stability and offer different features to use.&lt;br /&gt;
 &lt;br /&gt;
; &#039;&#039;PHP preg extension&#039;&#039; : should be used when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. It is based on the native PHP preg_ functions. It supports 100% perl-compatible regex features, it is very stable and thoroughly tested. But it doesn&#039;t support partial matching, so (unless we storm PHP developers to add support of partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039;. However it supports subpattern capturing. Choose it when you need complex regex features that other engines don&#039;t support.&lt;br /&gt;
; &#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039; : can be used to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for your students. NFA engine is a custom PHP code, it allows many (but not all) regex features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but still may contain bugs in rare cases. Unsupported features for now are lookaround assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;Deterministic finite state automata (DFA)&#039;&#039; : WARNING - this engine lacking support the past year. Use NFA engine instead if you can (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine does, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
Hinting is supported by NFA and DFA engines in adaptive and interactive behaviours.&lt;br /&gt;
&lt;br /&gt;
====Partial matching====&lt;br /&gt;
Hinting starts with &#039;&#039;&#039;partial matching&#039;&#039;&#039;. By partially correct response we understand a string that starts with correct characters (matching your regex) but on some character the match breaks. Assume you entered the regex&lt;br /&gt;
  &amp;quot;&#039;&#039;&#039;are blue, white(,| and) red&#039;&#039;&#039;&amp;quot;&lt;br /&gt;
and a student answered&lt;br /&gt;
  &amp;quot;they are blue, vhite and red&amp;quot;&lt;br /&gt;
In this situation the partial match is&lt;br /&gt;
  &amp;quot;are blue, &amp;quot;&lt;br /&gt;
Note that the regex is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) so the match may not start with the first character of the student&#039;s response (like in the example above: &amp;quot;they &amp;quot; is skipped). While using just partial matching the student will see the 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;
====General hinting rules====&lt;br /&gt;
Preg question type doesn&#039;t add hinted characters to the student&#039;s response (unlike the REGEXP 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 a hint, since when the response is modified it is too easy to repeatedly press &#039;&#039;&#039;hint&#039;&#039;&#039;, which is not usually a desirable behaviour.&lt;br /&gt;
When possible, 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: &amp;quot;,&amp;quot; or &amp;quot; &amp;quot; (leading to the &amp;quot; and&amp;quot; path). The question will choose &amp;quot;,&amp;quot; because it leads to the shortest path to complete the match, while &amp;quot; &amp;quot; 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 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 the 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 grades from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 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 the 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 (word) 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 can be 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 student either completion of the current lexem (if partial match ends inside it) or next one (if student complete the 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 these languages (there will be more):&lt;br /&gt;
; &#039;&#039;simple english&#039;&#039; : english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
; &#039;&#039;C/C++ language&#039;&#039; : a programming language C (or C++);&lt;br /&gt;
; &#039;&#039;printf language&#039;&#039; : a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&lt;br /&gt;
&lt;br /&gt;
Note that &amp;quot;lexem&amp;quot; typically isn&#039;t the word you would like your students to see on the hinting button. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how your expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regex&lt;br /&gt;
&lt;br /&gt;
===Installation note and known technical issues===&lt;br /&gt;
To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you (or your site admin) have to install Graphviz[http://www.graphviz.org/Graphviz] on the server and fill the &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. Graphviz is used to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
Syntax tree and explaining graph may not work correctly in old Opera versions - for some reason the images are not updated on user actions. Fortunately, there&#039;s a newer version 16 for Windows which works with authoring tools pretty well. On Linux you will have to use something else.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
Here you can edit your regular expression. Clicking on &amp;quot;Update&amp;quot; sends the regex to all tools - so syntax tree, explaining graph, description and testing results are updated. &amp;quot;Save&amp;quot; closes the authoring tools form and saves the regex in the main question editing form. &amp;quot;Cancel&amp;quot; closes the authoring tools form and discards all changes made there. The last, &amp;quot;TODO&amp;quot; button, helps you to trace the interrelation between the regex itself (text representation) and the other representations: you can select a regex part and see where it is located in the syntax tree and in the graph.&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see a coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;[[:alpha:]]&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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;many times*&amp;quot; matches &amp;quot;manytime&amp;quot; followed by zero or more &amp;quot;s&amp;quot;;&lt;br /&gt;
#* &amp;quot;(many times)*&amp;quot; matches &amp;quot;many times&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;first|second|third&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot; or &amp;quot;third&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first |second |)part&amp;quot; matches &amp;quot;first part&amp;quot; or &amp;quot;second part&amp;quot; or just &amp;quot;part&amp;quot; - typical use of an empty alternative (note that space is in alternative to not require it before just &amp;quot;part&amp;quot;);&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;first|second*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;secon&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;secondddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first|second)*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot;, repeated zero or more time in any order, like &amp;quot;firstsecondfirstfirst&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(1|2){2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;12&amp;quot; or &amp;quot;21&amp;quot; or &amp;quot;22&amp;quot;, not just &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot;;&lt;br /&gt;
#* &amp;quot;1{2}|2{2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
An internal structure of regular expression can be viewed well on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;^start|end$&amp;quot; will match &amp;quot;start&amp;quot; from the start of the string or &amp;quot;end&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(start|end)$&amp;quot; using brackets to match exactly with &amp;quot;start&amp;quot; or &amp;quot;end&amp;quot;;&lt;br /&gt;
* &amp;quot;^start$|^end$&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This project is free software, so it&#039;s hard to get any feedback. You shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
This software is considered a scientific project and such things could be really useful and appreciated:&lt;br /&gt;
* an evidence that the results of our work (i.e. Preg questoin type) are really useful to people and were used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you use Preg and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or a journal article;&lt;br /&gt;
* cooperating in writing article or help publishing it in English-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 though. 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 - there are many settings in the question, and regexes can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106472</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106472"/>
		<updated>2013-08-28T20:43:30Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Anchoring */ more human-readable examples&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
A little foreword: regardless of the way you use Preg and your capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any feedback to freely distributed software. But you shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;Pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers&lt;br /&gt;
* &#039;&#039;&#039;Hinting&#039;&#039;&#039; - when students are stuck doing the question, you may allow them to ask for a next correct word (lexem) or a character (with possible penalty)&lt;br /&gt;
 &lt;br /&gt;
===Settings affecting question work===&lt;br /&gt;
Sets the case sensitivity for all regular expressions you specify as answers. Note that you can also [[#Local case-sensitivity modifiers|set the case sensitivity for regex parts]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Exact matching&#039;&#039;&#039; affects the question in the following way:&lt;br /&gt;
; &#039;&#039;Yes&#039;&#039; : the &#039;&#039;entire&#039;&#039; student&#039;s response, from the first to the last letter, should match your regular expression&lt;br /&gt;
; &#039;&#039;No&#039;&#039; : student&#039;s response can just contain a &#039;&#039;part&#039;&#039; that matches your regex: for example, if the correct answer is &amp;quot;whole&amp;quot; then &amp;quot;the whole string&amp;quot; will be a correct student response&lt;br /&gt;
 &lt;br /&gt;
You still can set some of your regexes to match the whole student&#039;s response using [[#Anchoring|special regex syntax]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Notations&#039;&#039;&#039; specify the &amp;quot;language&amp;quot; of your answers.&lt;br /&gt;
; &#039;&#039;Regular expression&#039;&#039; : a usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;Regular expression (extended)&#039;&#039; : useful for really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not inside character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything after &#039;#&#039; character untill the end of string is treated as commentary (# should not be escaped and should not be inside a character class).&lt;br /&gt;
; &#039;&#039;Moodle shortanswer&#039;&#039; : use it to avoid regex syntax at all: just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA engine you can get access to the hinting features. You can skip all that is said on regexes 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;
&#039;&#039;&#039;Matching engine&#039;&#039;&#039; specifies the program module that performs the regex matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use. Engines have different stability and offer different features to use.&lt;br /&gt;
 &lt;br /&gt;
; &#039;&#039;PHP preg extension&#039;&#039; : should be used when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. It is based on the native PHP preg_ functions. It supports 100% perl-compatible regex features, it is very stable and thoroughly tested. But it doesn&#039;t support partial matching, so (unless we storm PHP developers to add support of partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039;. However it supports subpattern capturing. Choose it when you need complex regex features that other engines don&#039;t support.&lt;br /&gt;
; &#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039; : can be used to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for your students. NFA engine is a custom PHP code, it allows many (but not all) regex features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but still may contain bugs in rare cases. Unsupported features for now are lookaround assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;Deterministic finite state automata (DFA)&#039;&#039; : WARNING - this engine lacking support the past year. Use NFA engine instead if you can (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine does, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
; &#039;&#039;simple english&#039;&#039; : english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
; &#039;&#039;C/C++ language&#039;&#039; : a programming language C (or C++);&lt;br /&gt;
; &#039;&#039;prinf language&#039;&#039; : a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how your expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regex&lt;br /&gt;
&lt;br /&gt;
INSTALLATION NOTE. To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you need to have the [http://www.graphviz.org/Graphviz] package installed on your server and fill &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. It is used by authoring tools code to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
Here you can edit your regular expression. Clicking on &amp;quot;Update&amp;quot; sends the regex to all tools - so syntax tree, explaining graph, description and testing results are updated. &amp;quot;Save&amp;quot; closes the authoring tools form and saves the regex in the main question editing form. &amp;quot;Cancel&amp;quot; closes the authoring tools form and discards all changes made there. The last, &amp;quot;TODO&amp;quot; button, helps you to trace the interrelation between the regex itself (text representation) and the other representations: you can select a regex part and see where it is located in the syntax tree and in the graph.&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see a coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;[[:alpha:]]&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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;many times*&amp;quot; matches &amp;quot;manytime&amp;quot; followed by zero or more &amp;quot;s&amp;quot;;&lt;br /&gt;
#* &amp;quot;(many times)*&amp;quot; matches &amp;quot;many times&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;first|second|third&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot; or &amp;quot;third&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first |second |)part&amp;quot; matches &amp;quot;first part&amp;quot; or &amp;quot;second part&amp;quot; or just &amp;quot;part&amp;quot; - typical use of an empty alternative (note that space is in alternative to not require it before just &amp;quot;part&amp;quot;);&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;first|second*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;secon&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;secondddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first|second)*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot;, repeated zero or more time in any order, like &amp;quot;firstsecondfirstfirst&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(1|2){2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;12&amp;quot; or &amp;quot;21&amp;quot; or &amp;quot;22&amp;quot;, not just &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot;;&lt;br /&gt;
#* &amp;quot;1{2}|2{2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
An internal structure of regular expression can be viewed well on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;^start|end$&amp;quot; will match &amp;quot;start&amp;quot; from the start of the string or &amp;quot;end&amp;quot; at the end of it;&lt;br /&gt;
* &amp;quot;^(start|end)$&amp;quot; using brackets to match exactly with &amp;quot;start&amp;quot; or &amp;quot;end&amp;quot;;&lt;br /&gt;
* &amp;quot;^start$|^end$&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106470</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106470"/>
		<updated>2013-08-28T20:42:07Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Precedence and order of evaluation */ - made more human-readable examples&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
A little foreword: regardless of the way you use Preg and your capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any feedback to freely distributed software. But you shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;Pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers&lt;br /&gt;
* &#039;&#039;&#039;Hinting&#039;&#039;&#039; - when students are stuck doing the question, you may allow them to ask for a next correct word (lexem) or a character (with possible penalty)&lt;br /&gt;
 &lt;br /&gt;
===Settings affecting question work===&lt;br /&gt;
Sets the case sensitivity for all regular expressions you specify as answers. Note that you can also [[#Local case-sensitivity modifiers|set the case sensitivity for regex parts]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Exact matching&#039;&#039;&#039; affects the question in the following way:&lt;br /&gt;
; &#039;&#039;Yes&#039;&#039; : the &#039;&#039;entire&#039;&#039; student&#039;s response, from the first to the last letter, should match your regular expression&lt;br /&gt;
; &#039;&#039;No&#039;&#039; : student&#039;s response can just contain a &#039;&#039;part&#039;&#039; that matches your regex: for example, if the correct answer is &amp;quot;whole&amp;quot; then &amp;quot;the whole string&amp;quot; will be a correct student response&lt;br /&gt;
 &lt;br /&gt;
You still can set some of your regexes to match the whole student&#039;s response using [[#Anchoring|special regex syntax]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Notations&#039;&#039;&#039; specify the &amp;quot;language&amp;quot; of your answers.&lt;br /&gt;
; &#039;&#039;Regular expression&#039;&#039; : a usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;Regular expression (extended)&#039;&#039; : useful for really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not inside character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything after &#039;#&#039; character untill the end of string is treated as commentary (# should not be escaped and should not be inside a character class).&lt;br /&gt;
; &#039;&#039;Moodle shortanswer&#039;&#039; : use it to avoid regex syntax at all: just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA engine you can get access to the hinting features. You can skip all that is said on regexes 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;
&#039;&#039;&#039;Matching engine&#039;&#039;&#039; specifies the program module that performs the regex matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use. Engines have different stability and offer different features to use.&lt;br /&gt;
 &lt;br /&gt;
; &#039;&#039;PHP preg extension&#039;&#039; : should be used when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. It is based on the native PHP preg_ functions. It supports 100% perl-compatible regex features, it is very stable and thoroughly tested. But it doesn&#039;t support partial matching, so (unless we storm PHP developers to add support of partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039;. However it supports subpattern capturing. Choose it when you need complex regex features that other engines don&#039;t support.&lt;br /&gt;
; &#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039; : can be used to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for your students. NFA engine is a custom PHP code, it allows many (but not all) regex features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but still may contain bugs in rare cases. Unsupported features for now are lookaround assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;Deterministic finite state automata (DFA)&#039;&#039; : WARNING - this engine lacking support the past year. Use NFA engine instead if you can (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine does, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
; &#039;&#039;simple english&#039;&#039; : english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
; &#039;&#039;C/C++ language&#039;&#039; : a programming language C (or C++);&lt;br /&gt;
; &#039;&#039;prinf language&#039;&#039; : a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how you expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regexes&lt;br /&gt;
&lt;br /&gt;
INSTALLATION NOTE. To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you need to have the [http://www.graphviz.org/Graphviz] package installed on your server and fill &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. It is used by authoring tools code to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you can enter (or edit) regular expression and refresh all the tools when done.&lt;br /&gt;
&lt;br /&gt;
  TODO You can also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see a coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;[[:alpha:]]&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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;many times*&amp;quot; matches &amp;quot;manytime&amp;quot; followed by zero or more &amp;quot;s&amp;quot;;&lt;br /&gt;
#* &amp;quot;(many times)*&amp;quot; matches &amp;quot;many times&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;first|second|third&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot; or &amp;quot;third&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first |second |)part&amp;quot; matches &amp;quot;first part&amp;quot; or &amp;quot;second part&amp;quot; or just &amp;quot;part&amp;quot; - typical use of an empty alternative (note that space is in alternative to not require it before just &amp;quot;part&amp;quot;);&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;first|second*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;secon&amp;quot; followed by zero or more &amp;quot;d&amp;quot; like &amp;quot;secondddddd&amp;quot;;&lt;br /&gt;
#* &amp;quot;(first|second)*&amp;quot; matches &amp;quot;first&amp;quot; or &amp;quot;second&amp;quot;, repeated zero or more time in any order, like &amp;quot;firstsecondfirstfirst&amp;quot;. Note that quantifiers repeat the whole alternative, not a definite selection from it, i.e.:&lt;br /&gt;
#* &amp;quot;(1|2){2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;12&amp;quot; or &amp;quot;21&amp;quot; or &amp;quot;22&amp;quot;, not just &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot;;&lt;br /&gt;
#* &amp;quot;1{2}|2{2}&amp;quot; matches &amp;quot;11&amp;quot; or &amp;quot;22&amp;quot; only.&lt;br /&gt;
&lt;br /&gt;
An internal structure of regular expression can be viewed well on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106469</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106469"/>
		<updated>2013-08-28T20:25:48Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Regular expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
A little foreword: regardless of the way you use Preg and your capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any feedback to freely distributed software. But you shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;Pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers&lt;br /&gt;
* &#039;&#039;&#039;Hinting&#039;&#039;&#039; - when students are stuck doing the question, you may allow them to ask for a next correct word (lexem) or a character (with possible penalty)&lt;br /&gt;
 &lt;br /&gt;
===Settings affecting question work===&lt;br /&gt;
Sets the case sensitivity for all regular expressions you specify as answers. Note that you can also [[#Local case-sensitivity modifiers|set the case sensitivity for regex parts]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Exact matching&#039;&#039;&#039; affects the question in the following way:&lt;br /&gt;
; &#039;&#039;Yes&#039;&#039; : the &#039;&#039;entire&#039;&#039; student&#039;s response, from the first to the last letter, should match your regular expression&lt;br /&gt;
; &#039;&#039;No&#039;&#039; : student&#039;s response can just contain a &#039;&#039;part&#039;&#039; that matches your regex: for example, if the correct answer is &amp;quot;whole&amp;quot; then &amp;quot;the whole string&amp;quot; will be a correct student response&lt;br /&gt;
 &lt;br /&gt;
You still can set some of your regexes to match the whole student&#039;s response using [[#Anchoring|special regex syntax]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Notations&#039;&#039;&#039; specify the &amp;quot;language&amp;quot; of your answers.&lt;br /&gt;
; &#039;&#039;Regular expression&#039;&#039; : a usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;Regular expression (extended)&#039;&#039; : useful for really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not inside character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything after &#039;#&#039; character untill the end of string is treated as commentary (# should not be escaped and should not be inside a character class).&lt;br /&gt;
; &#039;&#039;Moodle shortanswer&#039;&#039; : use it to avoid regex syntax at all: just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA engine you can get access to the hinting features. You can skip all that is said on regexes 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;
&#039;&#039;&#039;Matching engine&#039;&#039;&#039; specifies the program module that performs the regex matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use. Engines have different stability and offer different features to use.&lt;br /&gt;
 &lt;br /&gt;
; &#039;&#039;PHP preg extension&#039;&#039; : should be used when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. It is based on the native PHP preg_ functions. It supports 100% perl-compatible regex features, it is very stable and thoroughly tested. But it doesn&#039;t support partial matching, so (unless we storm PHP developers to add support of partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039;. However it supports subpattern capturing. Choose it when you need complex regex features that other engines don&#039;t support.&lt;br /&gt;
; &#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039; : can be used to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for your students. NFA engine is a custom PHP code, it allows many (but not all) regex features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but still may contain bugs in rare cases. Unsupported features for now are lookaround assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;Deterministic finite state automata (DFA)&#039;&#039; : WARNING - this engine lacking support the past year. Use NFA engine instead if you can (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine does, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
; &#039;&#039;simple english&#039;&#039; : english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
; &#039;&#039;C/C++ language&#039;&#039; : a programming language C (or C++);&lt;br /&gt;
; &#039;&#039;prinf language&#039;&#039; : a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how you expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regexes&lt;br /&gt;
&lt;br /&gt;
INSTALLATION NOTE. To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you need to have the [http://www.graphviz.org/Graphviz] package installed on your server and fill &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. It is used by authoring tools code to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you can enter (or edit) regular expression and refresh all the tools when done.&lt;br /&gt;
&lt;br /&gt;
  TODO You can also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see a coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;[[:alpha:]]&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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 just &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;
An internal structure of regular expression can be viewed well on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106468</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106468"/>
		<updated>2013-08-28T20:23:15Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Settings affecting question work */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
A little foreword: regardless of the way you use Preg and your capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any feedback to freely distributed software. But you shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;Pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers&lt;br /&gt;
* &#039;&#039;&#039;Hinting&#039;&#039;&#039; - when students are stuck doing the question, you may allow them to ask for a next correct word (lexem) or a character (with possible penalty)&lt;br /&gt;
 &lt;br /&gt;
===Settings affecting question work===&lt;br /&gt;
Sets the case sensitivity for all regular expressions you specify as answers. Note that you can also [[#Local case-sensitivity modifiers|set the case sensitivity for regex parts]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Exact matching&#039;&#039;&#039; affects the question in the following way:&lt;br /&gt;
; &#039;&#039;Yes&#039;&#039; : the &#039;&#039;entire&#039;&#039; student&#039;s response, from the first to the last letter, should match your regular expression&lt;br /&gt;
; &#039;&#039;No&#039;&#039; : student&#039;s response can just contain a &#039;&#039;part&#039;&#039; that matches your regex: for example, if the correct answer is &amp;quot;whole&amp;quot; then &amp;quot;the whole string&amp;quot; will be a correct student response&lt;br /&gt;
 &lt;br /&gt;
You still can set some of your regexes to match the whole student&#039;s response using [[#Anchoring|special regex syntax]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Notations&#039;&#039;&#039; specify the &amp;quot;language&amp;quot; of your answers.&lt;br /&gt;
; &#039;&#039;Regular expression&#039;&#039; : a usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;Regular expression (extended)&#039;&#039; : useful for really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not inside character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything after &#039;#&#039; character untill the end of string is treated as commentary (# should not be escaped and should not be inside a character class).&lt;br /&gt;
; &#039;&#039;Moodle shortanswer&#039;&#039; : use it to avoid regex syntax at all: just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA engine you can get access to the hinting features. You can skip all that is said on regexes 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;
&#039;&#039;&#039;Matching engine&#039;&#039;&#039; specifies the program module that performs the regex matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use. Engines have different stability and offer different features to use.&lt;br /&gt;
 &lt;br /&gt;
; &#039;&#039;PHP preg extension&#039;&#039; : should be used when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. It is based on the native PHP preg_ functions. It supports 100% perl-compatible regex features, it is very stable and thoroughly tested. But it doesn&#039;t support partial matching, so (unless we storm PHP developers to add support of partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039;. However it supports subpattern capturing. Choose it when you need complex regex features that other engines don&#039;t support.&lt;br /&gt;
; &#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039; : can be used to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for your students. NFA engine is a custom PHP code, it allows many (but not all) regex features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but still may contain bugs in rare cases. Unsupported features for now are lookaround assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;Deterministic finite state automata (DFA)&#039;&#039; : WARNING - this engine lacking support the past year. Use NFA engine instead if you can (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine does, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
; &#039;&#039;simple english&#039;&#039; : english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
; &#039;&#039;C/C++ language&#039;&#039; : a programming language C (or C++);&lt;br /&gt;
; &#039;&#039;prinf language&#039;&#039; : a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how you expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regexes&lt;br /&gt;
&lt;br /&gt;
INSTALLATION NOTE. To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you need to have the [http://www.graphviz.org/Graphviz] package installed on your server and fill &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. It is used by authoring tools code to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you can enter (or edit) regular expression and refresh all the tools when done.&lt;br /&gt;
&lt;br /&gt;
  TODO You can also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see a coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, [[:alpha:]] any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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 just &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;
An internal structure of regular expression can be viewed well on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106467</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106467"/>
		<updated>2013-08-28T20:22:39Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Settings affecting question work */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
A little foreword: regardless of the way you use Preg and your capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any feedback to freely distributed software. But you shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;Pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers&lt;br /&gt;
* &#039;&#039;&#039;Hinting&#039;&#039;&#039; - when students are stuck doing the question, you may allow them to ask for a next correct word (lexem) or a character (with possible penalty)&lt;br /&gt;
 &lt;br /&gt;
===Settings affecting question work===&lt;br /&gt;
Sets the case sensitivity for all regular expressions you specify as answers. Note that you can also [[#Local case-sensitivity modifiers|set the case sensitivity for regex parts]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Exact matching&#039;&#039;&#039; affects the question in the following way:&lt;br /&gt;
; &#039;&#039;Yes&#039;&#039; : the &#039;&#039;entire&#039;&#039; student&#039;s response, from the first to the last letter, should match your regular expression&lt;br /&gt;
; &#039;&#039;No&#039;&#039; : student&#039;s response can just contain a &#039;&#039;part&#039;&#039; that matches your regex: for example, if the correct answer is &amp;quot;whole&amp;quot; then &amp;quot;the whole regex&amp;quot; will be a correct student response&lt;br /&gt;
 &lt;br /&gt;
You still can set some of your regexes to match the whole student&#039;s response using [[#Anchoring|special regex syntax]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Notations&#039;&#039;&#039; specify the &amp;quot;language&amp;quot; of your answers.&lt;br /&gt;
; &#039;&#039;Regular expression&#039;&#039; : a usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;Regular expression (extended)&#039;&#039; : useful for really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not inside character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything after &#039;#&#039; character untill the end of string is treated as commentary (# should not be escaped and should not be inside a character class).&lt;br /&gt;
; &#039;&#039;Moodle shortanswer&#039;&#039; : use it to avoid regex syntax at all: just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA engine you can get access to the hinting features. You can skip all that is said on regexes 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;
&#039;&#039;&#039;Matching engine&#039;&#039;&#039; specifies the program module that performs the regex matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use. Engines have different stability and offer different features to use.&lt;br /&gt;
 &lt;br /&gt;
; &#039;&#039;PHP preg extension&#039;&#039; : should be used when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. It is based on the native PHP preg_ functions. It supports 100% perl-compatible regex features, it is very stable and thoroughly tested. But it doesn&#039;t support partial matching, so (unless we storm PHP developers to add support of partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039;. However it supports subpattern capturing. Choose it when you need complex regex features that other engines don&#039;t support.&lt;br /&gt;
; &#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039; : can be used to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for your students. NFA engine is a custom PHP code, it allows many (but not all) regex features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but still may contain bugs in rare cases. Unsupported features for now are lookaround assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;Deterministic finite state automata (DFA)&#039;&#039; : WARNING - this engine lacking support the past year. Use NFA engine instead if you can (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine does, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
; &#039;&#039;simple english&#039;&#039; : english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
; &#039;&#039;C/C++ language&#039;&#039; : a programming language C (or C++);&lt;br /&gt;
; &#039;&#039;prinf language&#039;&#039; : a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how you expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regexes&lt;br /&gt;
&lt;br /&gt;
INSTALLATION NOTE. To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you need to have the [http://www.graphviz.org/Graphviz] package installed on your server and fill &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. It is used by authoring tools code to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you can enter (or edit) regular expression and refresh all the tools when done.&lt;br /&gt;
&lt;br /&gt;
  TODO You can also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see a coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, [[:alpha:]] any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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 just &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;
An internal structure of regular expression can be viewed well on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106466</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106466"/>
		<updated>2013-08-28T20:17:34Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* How Preg questions work */ - исправление жирности и различных ошибок&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
A little foreword: regardless of the way you use Preg and your capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any feedback to freely distributed software. But you shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;Pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers&lt;br /&gt;
* &#039;&#039;&#039;Hinting&#039;&#039;&#039; - when students are stuck doing the question, you may allow them to ask for a next correct word (lexem) or a character (with possible penalty)&lt;br /&gt;
 &lt;br /&gt;
===Settings affecting question work===&lt;br /&gt;
Sets the case sensitivity for all regular expressions you specify as answers. Note that you can also [[#Local case-sensitivity modifiers|set the case sensitivity for regex parts]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Exact matching&#039;&#039;&#039; affects the question in the following way:&lt;br /&gt;
; &#039;&#039;Yes&#039;&#039; : the &#039;&#039;entire&#039;&#039; student&#039;s response, from the first untill the last letter, should match your regular expression&lt;br /&gt;
; &#039;&#039;No&#039;&#039; : student&#039;s response can just contain a &#039;&#039;part&#039;&#039; that matches your regex: for example, if the correct answer is &amp;quot;whole&amp;quot; then &amp;quot;the whole regex&amp;quot; will be a correct student response&lt;br /&gt;
 &lt;br /&gt;
You still can set some of your regexes to match the whole student&#039;s response using [[#Anchoring|special regex syntax]].&lt;br /&gt;
 &lt;br /&gt;
&#039;&#039;&#039;Notations&#039;&#039;&#039; specify the &amp;quot;language&amp;quot; of your answers.&lt;br /&gt;
; &#039;&#039;Regular expression&#039;&#039; : a usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;Regular expression (extended)&#039;&#039; : useful for really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not inside character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything after &#039;#&#039; character untill the end of string is treated as commentary (# should not be escaped and should not be inside a character class).&lt;br /&gt;
; &#039;&#039;Moodle shortanswer&#039;&#039; : use it to avoid regex syntax at all: just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA engine you can get access to the hinting features. You can skip all that is said on regexes 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;
&#039;&#039;&#039;Matching engine&#039;&#039;&#039; specifies the program module that performs the regex matching. There is no &#039;best&#039; matching engine - it depends on the features you want to use. Engines have different stability and offer different features to use.&lt;br /&gt;
 &lt;br /&gt;
; &#039;&#039;PHP preg extension&#039;&#039; : should be used when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. It is based on the native PHP preg_ functions. It supports 100% perl-compatible regex features, it is very stable and thoroughly tested. But it doesn&#039;t support partial matching, so (unless we storm PHP developers to add support of partial matching) there is &#039;&#039;&#039;no hinting&#039;&#039;&#039;. However it supports subpattern capturing. Choose it when you need complex regex features that other engines don&#039;t support.&lt;br /&gt;
; &#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039; : can be used to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for your students. NFA engine is a custom PHP code, it allows many (but not all) regex features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but still may contain bugs in rare cases. Unsupported features for now are lookaround assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;Deterministic finite state automata (DFA)&#039;&#039; : WARNING - this engine lacking support the past year. Use NFA engine instead if you can (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine does, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
; &#039;&#039;simple english&#039;&#039; : english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
; &#039;&#039;C/C++ language&#039;&#039; : a programming language C (or C++);&lt;br /&gt;
; &#039;&#039;prinf language&#039;&#039; : a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how you expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regexes&lt;br /&gt;
&lt;br /&gt;
INSTALLATION NOTE. To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you need to have the [http://www.graphviz.org/Graphviz] package installed on your server and fill &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. It is used by authoring tools code to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you can enter (or edit) regular expression and refresh all the tools when done.&lt;br /&gt;
&lt;br /&gt;
  TODO You can also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see a coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, [[:alpha:]] any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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 just &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;
An internal structure of regular expression can be viewed well on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106440</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106440"/>
		<updated>2013-08-23T20:18:47Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Precedence and order of evaluation */ - added info about syntax tree and order of evaluation&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
A little foreword: regardless of the way you use Preg and your capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any feedback to freely distributed software. But you shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers;&lt;br /&gt;
* &#039;&#039;&#039;hinting&#039;&#039;&#039; - when your students are stuck doing the question, you may allow them to ask for next correct word (lexem) or character (with penalty if you wish so).&lt;br /&gt;
&lt;br /&gt;
===Settings, that affects how question will work===&lt;br /&gt;
====Case sensitivity====&lt;br /&gt;
You should know this setting from core Shortanswer question type. Note, however, that you can [[#Local case-sensitivity modifiers|change case sensitivity inside you regular expressions]], making only parts of it case sensitive.&lt;br /&gt;
&lt;br /&gt;
====Exact matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student&#039;s answer that suits the regular expression (or you answer). This part called &#039;&#039;&#039;match&#039;&#039;&#039;. Traditionally, regular expressions were used to look for matches &#039;&#039;&#039;inside&#039;&#039;&#039; strings, i.e. &#039;&#039;&#039;all&#039;&#039;&#039; &#039;&#039;regular expression&#039;&#039; should match, but it could match with a &#039;&#039;&#039;part of&#039;&#039;&#039; &#039;&#039;students response&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Yes&#039;&#039;&#039; : entire students response should match with regular expression.&lt;br /&gt;
; &#039;&#039;&#039;No&#039;&#039;&#039; : any part of students response could match with regular expression. You could still set some of you regex matching with whole student&#039;s response using [[#Anchoring|regular expression features]].&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Notation is the way you write you regexes. Or choose &amp;quot;Moodle shortanswer&amp;quot; notation to avoid regexes at all, still use hinting features.&lt;br /&gt;
; &#039;&#039;&#039;Regular expression&#039;&#039;&#039; : This is usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; : This notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
; &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; : Choose this notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA 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;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs regular expression execution. 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;
&lt;br /&gt;
; &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; : Use it when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. 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;
; &#039;&#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039;&#039; : Use NFA engine to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for you students if it can handle you regular expressions. NFA engine is a custom PHP code that uses finite automata to perform matching. It is allow many (but not all) regular expression features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but may still contain bugs in rare cases. Not supported features for now include complex assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;&#039;Deterministic finite state automata (DFA)&#039;&#039;&#039; : WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine could, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting (not an easy thing to do using PHP at all) in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++);&lt;br /&gt;
* &#039;&#039;&#039;prinf language&#039;&#039;&#039; - a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how you expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regexes&lt;br /&gt;
&lt;br /&gt;
INSTALLATION NOTE. To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you need to have the [http://www.graphviz.org/Graphviz] package installed on your server and fill &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. It is used by authoring tools code to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you can enter (or edit) regular expression and refresh all the tools when done.&lt;br /&gt;
&lt;br /&gt;
  TODO You can also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see a coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, [[:alpha:]] any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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 just &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;
An internal structure of regular expression can be viewed vell on the [[#Syntax tree|syntax tree]] (authoring tool). The operators that executed first are placed lower on the tree (or to the right on horizontal view), the operator that executed last is the root of the tree. You can compare tree and explaining graphs for the examples above in authoring tools if this section doesn&#039;t seems too clear to you. Remember, that &amp;quot;execution&amp;quot; of regular expression operator means linking them in the string: sequental, alternative linking, or repeating.&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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106439</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106439"/>
		<updated>2013-08-23T18:03:57Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Understanding regular expressions */ - written an introduction about operands and operator&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Questions}}Preg is a question type that uses regular expressions (regexes) to check student&#039;s responses (though you can use it without regexes for its hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. [[#Ways to use Preg questions and this docs|First section]] should guide you to using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regex manuals, I&#039;m not going to repeat them here.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
A little foreword: regardless of the way you use Preg and your capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any feedback to freely distributed software. But you shouldn&#039;t expect to get software which ideally suits you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want to) know anything about regular expressions but next word (character) hinting seems useful===&lt;br /&gt;
Then you can use Preg question type just as Shortanswer with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read the section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regular expressions, but want to use pattern matching===&lt;br /&gt;
If writing regular expressions is hard for you, but you want to use their strength as patterns, authoring tools may help you a lot to create your questions. The tools show you the meaning of your regex in different ways: internal structure of the expression (syntax tree), visual path of matching (explaining graph) and a text description. They also allow you to test you regex against several strings and see if it works as expected. Experiment and play with your regexes, see corresponding changes in the authoring tools, and eventually you&#039;ll get the regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on your own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How Preg questions work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I can make some effort to learn regular expressions well and be able to do anything they allow===&lt;br /&gt;
Well, you don&#039;t know regexes but want to understand them and create complex expressions easily. Then, instad of blunt trying, you better spend some time and effort reading and understanding [[#Understanding regular expressions|this section]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you can see if you really understand them well and they behave as expected. Syntax tree may be especially useful when you try to get the right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand  the principles of regexes well, read sections about [[#How Preg questions work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know your possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write regexes without much use of authoring tools, except the testing tool to test your expressions.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How Preg questions work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in the [[#Authoring tools|authoring tools]] section. Finally, [[#Regular expressions reference|regular expression reference]] may be of some use for you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How Preg questions work==&lt;br /&gt;
Basically, this question type is an extended version of Shortanswer. It extends its features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;pattern matching&#039;&#039;&#039; - using regular expressions you can create powerful patterns describing possible students answers;&lt;br /&gt;
* &#039;&#039;&#039;hinting&#039;&#039;&#039; - when your students are stuck doing the question, you may allow them to ask for next correct word (lexem) or character (with penalty if you wish so).&lt;br /&gt;
&lt;br /&gt;
===Settings, that affects how question will work===&lt;br /&gt;
====Case sensitivity====&lt;br /&gt;
You should know this setting from core Shortanswer question type. Note, however, that you can [[#Local case-sensitivity modifiers|change case sensitivity inside you regular expressions]], making only parts of it case sensitive.&lt;br /&gt;
&lt;br /&gt;
====Exact matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student&#039;s answer that suits the regular expression (or you answer). This part called &#039;&#039;&#039;match&#039;&#039;&#039;. Traditionally, regular expressions were used to look for matches &#039;&#039;&#039;inside&#039;&#039;&#039; strings, i.e. &#039;&#039;&#039;all&#039;&#039;&#039; &#039;&#039;regular expression&#039;&#039; should match, but it could match with a &#039;&#039;&#039;part of&#039;&#039;&#039; &#039;&#039;students response&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Yes&#039;&#039;&#039; : entire students response should match with regular expression.&lt;br /&gt;
; &#039;&#039;&#039;No&#039;&#039;&#039; : any part of students response could match with regular expression. You could still set some of you regex matching with whole student&#039;s response using [[#Anchoring|regular expression features]].&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Notation is the way you write you regexes. Or choose &amp;quot;Moodle shortanswer&amp;quot; notation to avoid regexes at all, still use hinting features.&lt;br /&gt;
; &#039;&#039;&#039;Regular expression&#039;&#039;&#039; : This is usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; : This notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
; &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; : Choose this notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA 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;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs regular expression execution. 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;
&lt;br /&gt;
; &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; : Use it when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. 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;
; &#039;&#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039;&#039; : Use NFA engine to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for you students if it can handle you regular expressions. NFA engine is a custom PHP code that uses finite automata to perform matching. It is allow many (but not all) regular expression features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but may still contain bugs in rare cases. Not supported features for now include complex assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;&#039;Deterministic finite state automata (DFA)&#039;&#039;&#039; : WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine could, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting (not an easy thing to do using PHP at all) in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++);&lt;br /&gt;
* &#039;&#039;&#039;prinf language&#039;&#039;&#039; - a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and its parts), and test it. Authoring tools are activated by pressing the &amp;quot;edit&amp;quot; icon near the regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you the inner structure of regular expressions&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how you expression will work in a graphical way&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulates the meaning of your expression in English&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match your regexes&lt;br /&gt;
&lt;br /&gt;
INSTALLATION NOTE. To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you need to have the [http://www.graphviz.org/Graphviz] package installed on your server and fill &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. It is used by authoring tools code to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you can enter (or edit) regular expression and refresh all the tools when done.&lt;br /&gt;
&lt;br /&gt;
  TODO You can also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expressions, like all expressions, are trees of operators and operands. Syntax tree shows the inner structure of expression graphically: what is inside what. This will be the most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parentheses: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. Its nodes are matched characters, its edges show paths throught the nodes from the beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes are shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers which part of the string matched each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember subexpressions, you may use (?:  ) instead of (  ) parentheses, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You can enter a set of strings there, one per line. These strings will be matched against your expression. You&#039;ll see a coloured strings, showing which parts of your strings matched the expression, so you can test if it works as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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 a sets of characters, that is allowed in particular position. &#039;&#039;&#039;A&#039;&#039;&#039; is a regular expressions that matches a single character &#039;A&#039;. The &#039;&#039;&#039;operators&#039;&#039;&#039; in regular expressions define a way to combine individual characters in the pattern: sequence (&#039;&#039;concatenation&#039;&#039; operator), alternative and repeating (it is called &#039;&#039;quantifier&#039;&#039;). The concatenation is so simple operator, that it doesn&#039;t have any character for it at all - just write some characters in sequence, and they&#039;ll be concatenated. But it is still have precedence, so that the question can see, did you want to repeat a single character or a sequence of them. Alternative is written as vertical bar. There are many form of quantifiers - most commonly used are question mark (repeat zero or one times), asterisk (zero or more times) and plus (one or more times). You may specify mininimum and maximum number of repeats in curly braces - this is a quantifier too.&lt;br /&gt;
&lt;br /&gt;
The special characters that define operators should be &#039;&#039;&#039;escaped&#039;&#039;&#039; when used as operands - preceded by a backslash.  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;
Character classes allows you to specify several possible characters for one place. They can be defined in many different ways: by enumeration of characters in square brackets &#039;&#039;&#039;[as3]&#039;&#039;&#039;, by ranges in square brackets &#039;&#039;&#039;[a-z]&#039;&#039;&#039;, by special sequences (&#039;&#039;&#039;\d&#039;&#039;&#039; means any digit, &#039;&#039;&#039;\W&#039;&#039;&#039; anything except a letter, digit and underscore, [[:alpha:]] any letter etc). An important type of operand is a &#039;&#039;simple assertions&#039;&#039;: they allow you to test some conditions - start of the string &#039;&#039;&#039;^&#039;&#039;&#039;, end of the string &#039;&#039;&#039;$&#039;&#039;&#039; or word border &#039;&#039;&#039;\b&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
You could find a list and more examples of operands and operators in [[#Regular expressions reference|reference]] section.&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 just &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;
===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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106432</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106432"/>
		<updated>2013-08-23T10:15:04Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Authoring tools */ - added information about graphviz installation&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 (thought you can use it without regexes for it&#039;s hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. First section should guide you in using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regular expression manuals, I&#039;m not going to repeat it there.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
Regardless of the way you use Preg question and you capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any sort of feedback with freely distributed software. But you shoudn&#039;t expect to get software which ideally suitting you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type in it may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want) to know anything about regular expressions but next word(character) hinting seems useful===&lt;br /&gt;
You can use Preg question type just as Shortanswer questions with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regexes, but want to use pattern matching===&lt;br /&gt;
If creating regular expressions is a hard task for you, but you want to use their strength as patterns, you may make heavy use of authoring tools to create you questions. Authoring tools shows you a meaning of you expression in different way: an internal structure of expression(syntax tree), a visual path of matching (explaining graph) and a text description. They also allows you to test you regex against several strings and see, whether it work as expected. Experiment and play changing you regexes, see corresponding changes in authoring tools, and eventually you may get regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on you own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How question work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I could spend some effort to learn regular expressions well and be able to do anything I they could===&lt;br /&gt;
If you don&#039;t know well regular expression, but want to understand them really well and create complex regexes easy; if you want to know what you doing writing you regexes, instead of blunt trying, you should spent some time and effort understanding it. Do not worry - it&#039;s not as hard as it sounds.&lt;br /&gt;
&lt;br /&gt;
If you want to do that, read section about [[#Understanding regular expressions|understanding regular expressions]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you could see, if you really understand them well and they behave as expected. Syntax tree may be of special use for you, when you try to getting right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand well principles of regular expression, read sections about [[#How question work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know you possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write you regexes without much use of authoring tools, except testing tool to test you regexes.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How question work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in [[#Authoring tools|authoring tools]] section, . Finally, [[#Regular expressions reference|regular expression reference]] may be of some use to you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How question work==&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer. It extends it features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;pattern matching&#039;&#039;&#039; - using regular expressions you could create a powerful patterns describing possible students answers;&lt;br /&gt;
* &#039;&#039;&#039;hinting&#039;&#039;&#039; - when you students are stuck doing the question, you may allow them to ask it for next correct word (lexem) or character (with penalty if you wish so).&lt;br /&gt;
&lt;br /&gt;
===Settings, that affects how question will work===&lt;br /&gt;
====Case sensitivity====&lt;br /&gt;
You should know this setting from core Shortanswer question type. Note, however, that you can [[#Local case-sensitivity modifiers|change case sensitivity inside you regular expressions]], making only parts of it case sensitive.&lt;br /&gt;
&lt;br /&gt;
====Exact matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student&#039;s answer that suits the regular expression (or you answer). This part called &#039;&#039;&#039;match&#039;&#039;&#039;. Traditionally, regular expressions were used to look for matches &#039;&#039;&#039;inside&#039;&#039;&#039; strings, i.e. &#039;&#039;&#039;all&#039;&#039;&#039; &#039;&#039;regular expression&#039;&#039; should match, but it could match with a &#039;&#039;&#039;part of&#039;&#039;&#039; &#039;&#039;students response&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Yes&#039;&#039;&#039; : entire students response should match with regular expression.&lt;br /&gt;
; &#039;&#039;&#039;No&#039;&#039;&#039; : any part of students response could match with regular expression. You could still set some of you regex matching with whole student&#039;s response using [[#Anchoring|regular expression features]].&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Notation is the way you write you regexes. Or choose &amp;quot;Moodle shortanswer&amp;quot; notation to avoid regexes at all, still use hinting features.&lt;br /&gt;
; &#039;&#039;&#039;Regular expression&#039;&#039;&#039; : This is usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; : This notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
; &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; : Choose this notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA 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;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs regular expression execution. 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;
&lt;br /&gt;
; &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; : Use it when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. 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;
; &#039;&#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039;&#039; : Use NFA engine to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for you students if it can handle you regular expressions. NFA engine is a custom PHP code that uses finite automata to perform matching. It is allow many (but not all) regular expression features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but may still contain bugs in rare cases. Not supported features for now include complex assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;&#039;Deterministic finite state automata (DFA)&#039;&#039;&#039; : WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine could, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting (not an easy thing to do using PHP at all) in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++);&lt;br /&gt;
* &#039;&#039;&#039;prinf language&#039;&#039;&#039; - a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you an inner structure of regular expression;&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how you expression will work in a graphical way;&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulate the meaning of you expression in the english language;&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
INSTALLATION NOTE. To have &#039;&#039;syntax tree&#039;&#039; and &#039;&#039;explaining graph&#039;&#039; tools working you need to have  [http://www.graphviz.org/ Graphviz] open source packet installed on you server and fill &#039;pathtodot&#039; setting on you Moodle installation at Site Administration &amp;gt; Server &amp;gt; System Paths. It is used by authoring tools code to draw pictures for you.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools when done it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what. This will be most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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. &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;
  TODO - write more about operands and operators, but simple.&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;
===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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106431</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106431"/>
		<updated>2013-08-23T10:06:59Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* I don&amp;#039;t (want) know anything about regular expressions but next word(character) hinting seems useful */&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 (thought you can use it without regexes for it&#039;s hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. First section should guide you in using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regular expression manuals, I&#039;m not going to repeat it there.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
Regardless of the way you use Preg question and you capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any sort of feedback with freely distributed software. But you shoudn&#039;t expect to get software which ideally suitting you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type in it may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want) to know anything about regular expressions but next word(character) hinting seems useful===&lt;br /&gt;
You can use Preg question type just as Shortanswer questions with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read section about [[#Hinting|hinting]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regexes, but want to use pattern matching===&lt;br /&gt;
If creating regular expressions is a hard task for you, but you want to use their strength as patterns, you may make heavy use of authoring tools to create you questions. Authoring tools shows you a meaning of you expression in different way: an internal structure of expression(syntax tree), a visual path of matching (explaining graph) and a text description. They also allows you to test you regex against several strings and see, whether it work as expected. Experiment and play changing you regexes, see corresponding changes in authoring tools, and eventually you may get regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on you own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How question work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I could spend some effort to learn regular expressions well and be able to do anything I they could===&lt;br /&gt;
If you don&#039;t know well regular expression, but want to understand them really well and create complex regexes easy; if you want to know what you doing writing you regexes, instead of blunt trying, you should spent some time and effort understanding it. Do not worry - it&#039;s not as hard as it sounds.&lt;br /&gt;
&lt;br /&gt;
If you want to do that, read section about [[#Understanding regular expressions|understanding regular expressions]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you could see, if you really understand them well and they behave as expected. Syntax tree may be of special use for you, when you try to getting right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand well principles of regular expression, read sections about [[#How question work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know you possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write you regexes without much use of authoring tools, except testing tool to test you regexes.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How question work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in [[#Authoring tools|authoring tools]] section, . Finally, [[#Regular expressions reference|regular expression reference]] may be of some use to you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How question work==&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer. It extends it features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;pattern matching&#039;&#039;&#039; - using regular expressions you could create a powerful patterns describing possible students answers;&lt;br /&gt;
* &#039;&#039;&#039;hinting&#039;&#039;&#039; - when you students are stuck doing the question, you may allow them to ask it for next correct word (lexem) or character (with penalty if you wish so).&lt;br /&gt;
&lt;br /&gt;
===Settings, that affects how question will work===&lt;br /&gt;
====Case sensitivity====&lt;br /&gt;
You should know this setting from core Shortanswer question type. Note, however, that you can [[#Local case-sensitivity modifiers|change case sensitivity inside you regular expressions]], making only parts of it case sensitive.&lt;br /&gt;
&lt;br /&gt;
====Exact matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student&#039;s answer that suits the regular expression (or you answer). This part called &#039;&#039;&#039;match&#039;&#039;&#039;. Traditionally, regular expressions were used to look for matches &#039;&#039;&#039;inside&#039;&#039;&#039; strings, i.e. &#039;&#039;&#039;all&#039;&#039;&#039; &#039;&#039;regular expression&#039;&#039; should match, but it could match with a &#039;&#039;&#039;part of&#039;&#039;&#039; &#039;&#039;students response&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Yes&#039;&#039;&#039; : entire students response should match with regular expression.&lt;br /&gt;
; &#039;&#039;&#039;No&#039;&#039;&#039; : any part of students response could match with regular expression. You could still set some of you regex matching with whole student&#039;s response using [[#Anchoring|regular expression features]].&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Notation is the way you write you regexes. Or choose &amp;quot;Moodle shortanswer&amp;quot; notation to avoid regexes at all, still use hinting features.&lt;br /&gt;
; &#039;&#039;&#039;Regular expression&#039;&#039;&#039; : This is usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; : This notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
; &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; : Choose this notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA 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;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs regular expression execution. 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;
&lt;br /&gt;
; &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; : Use it when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. 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;
; &#039;&#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039;&#039; : Use NFA engine to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for you students if it can handle you regular expressions. NFA engine is a custom PHP code that uses finite automata to perform matching. It is allow many (but not all) regular expression features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but may still contain bugs in rare cases. Not supported features for now include complex assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;&#039;Deterministic finite state automata (DFA)&#039;&#039;&#039; : WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine could, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting (not an easy thing to do using PHP at all) in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++);&lt;br /&gt;
* &#039;&#039;&#039;prinf language&#039;&#039;&#039; - a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you an inner structure of regular expression;&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how you expression will work in a graphical way;&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulate the meaning of you expression in the english language;&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools when done it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what. This will be most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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. &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;
  TODO - write more about operands and operators, but simple.&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;
===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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106409</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106409"/>
		<updated>2013-08-22T21:32:28Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Authoring tools */ - minor reformatting&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 (thought you can use it without regexes for it&#039;s hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. First section should guide you in using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regular expression manuals, I&#039;m not going to repeat it there.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
Regardless of the way you use Preg question and you capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any sort of feedback with freely distributed software. But you shoudn&#039;t expect to get software which ideally suitting you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type in it may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want) know anything about regular expressions but next word(character) hinting seems useful===&lt;br /&gt;
You can use Preg question type just as Shortanswer questions with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read section about [[#How question work|question working]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regexes, but want to use pattern matching===&lt;br /&gt;
If creating regular expressions is a hard task for you, but you want to use their strength as patterns, you may make heavy use of authoring tools to create you questions. Authoring tools shows you a meaning of you expression in different way: an internal structure of expression(syntax tree), a visual path of matching (explaining graph) and a text description. They also allows you to test you regex against several strings and see, whether it work as expected. Experiment and play changing you regexes, see corresponding changes in authoring tools, and eventually you may get regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on you own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How question work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I could spend some effort to learn regular expressions well and be able to do anything I they could===&lt;br /&gt;
If you don&#039;t know well regular expression, but want to understand them really well and create complex regexes easy; if you want to know what you doing writing you regexes, instead of blunt trying, you should spent some time and effort understanding it. Do not worry - it&#039;s not as hard as it sounds.&lt;br /&gt;
&lt;br /&gt;
If you want to do that, read section about [[#Understanding regular expressions|understanding regular expressions]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you could see, if you really understand them well and they behave as expected. Syntax tree may be of special use for you, when you try to getting right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand well principles of regular expression, read sections about [[#How question work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know you possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write you regexes without much use of authoring tools, except testing tool to test you regexes.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How question work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in [[#Authoring tools|authoring tools]] section, . Finally, [[#Regular expressions reference|regular expression reference]] may be of some use to you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How question work==&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer. It extends it features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;pattern matching&#039;&#039;&#039; - using regular expressions you could create a powerful patterns describing possible students answers;&lt;br /&gt;
* &#039;&#039;&#039;hinting&#039;&#039;&#039; - when you students are stuck doing the question, you may allow them to ask it for next correct word (lexem) or character (with penalty if you wish so).&lt;br /&gt;
&lt;br /&gt;
===Settings, that affects how question will work===&lt;br /&gt;
====Case sensitivity====&lt;br /&gt;
You should know this setting from core Shortanswer question type. Note, however, that you can [[#Local case-sensitivity modifiers|change case sensitivity inside you regular expressions]], making only parts of it case sensitive.&lt;br /&gt;
&lt;br /&gt;
====Exact matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student&#039;s answer that suits the regular expression (or you answer). This part called &#039;&#039;&#039;match&#039;&#039;&#039;. Traditionally, regular expressions were used to look for matches &#039;&#039;&#039;inside&#039;&#039;&#039; strings, i.e. &#039;&#039;&#039;all&#039;&#039;&#039; &#039;&#039;regular expression&#039;&#039; should match, but it could match with a &#039;&#039;&#039;part of&#039;&#039;&#039; &#039;&#039;students response&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Yes&#039;&#039;&#039; : entire students response should match with regular expression.&lt;br /&gt;
; &#039;&#039;&#039;No&#039;&#039;&#039; : any part of students response could match with regular expression. You could still set some of you regex matching with whole student&#039;s response using [[#Anchoring|regular expression features]].&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Notation is the way you write you regexes. Or choose &amp;quot;Moodle shortanswer&amp;quot; notation to avoid regexes at all, still use hinting features.&lt;br /&gt;
; &#039;&#039;&#039;Regular expression&#039;&#039;&#039; : This is usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; : This notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
; &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; : Choose this notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA 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;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs regular expression execution. 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;
&lt;br /&gt;
; &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; : Use it when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. 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;
; &#039;&#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039;&#039; : Use NFA engine to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for you students if it can handle you regular expressions. NFA engine is a custom PHP code that uses finite automata to perform matching. It is allow many (but not all) regular expression features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but may still contain bugs in rare cases. Not supported features for now include complex assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;&#039;Deterministic finite state automata (DFA)&#039;&#039;&#039; : WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine could, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting (not an easy thing to do using PHP at all) in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++);&lt;br /&gt;
* &#039;&#039;&#039;prinf language&#039;&#039;&#039; - a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
; &#039;&#039;&#039;syntax tree&#039;&#039;&#039; : shows you an inner structure of regular expression;&lt;br /&gt;
; &#039;&#039;&#039;explaining graph&#039;&#039;&#039; : shows you how you expression will work in a graphical way;&lt;br /&gt;
; &#039;&#039;&#039;description&#039;&#039;&#039; : formulate the meaning of you expression in the english language;&lt;br /&gt;
; &#039;&#039;&#039;testing tool&#039;&#039;&#039; : allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools when done it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what. This will be most useful if you know how to understand regular expressions or [[#Understanding regular expressions|learning to do this]].&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operators and precedence conception well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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. &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;
  TODO - write more about operands and operators, but simple.&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;
===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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106408</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106408"/>
		<updated>2013-08-22T21:28:40Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* How question work */ - rewritten section on question working&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 (thought you can use it without regexes for it&#039;s hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. First section should guide you in using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regular expression manuals, I&#039;m not going to repeat it there.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
Regardless of the way you use Preg question and you capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any sort of feedback with freely distributed software. But you shoudn&#039;t expect to get software which ideally suitting you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type in it may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want) know anything about regular expressions but next word(character) hinting seems useful===&lt;br /&gt;
You can use Preg question type just as Shortanswer questions with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read section about [[#How question work|question working]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regexes, but want to use pattern matching===&lt;br /&gt;
If creating regular expressions is a hard task for you, but you want to use their strength as patterns, you may make heavy use of authoring tools to create you questions. Authoring tools shows you a meaning of you expression in different way: an internal structure of expression(syntax tree), a visual path of matching (explaining graph) and a text description. They also allows you to test you regex against several strings and see, whether it work as expected. Experiment and play changing you regexes, see corresponding changes in authoring tools, and eventually you may get regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on you own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How question work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I could spend some effort to learn regular expressions well and be able to do anything I they could===&lt;br /&gt;
If you don&#039;t know well regular expression, but want to understand them really well and create complex regexes easy; if you want to know what you doing writing you regexes, instead of blunt trying, you should spent some time and effort understanding it. Do not worry - it&#039;s not as hard as it sounds.&lt;br /&gt;
&lt;br /&gt;
If you want to do that, read section about [[#Understanding regular expressions|understanding regular expressions]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you could see, if you really understand them well and they behave as expected. Syntax tree may be of special use for you, when you try to getting right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand well principles of regular expression, read sections about [[#How question work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know you possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write you regexes without much use of authoring tools, except testing tool to test you regexes.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How question work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in [[#Authoring tools|authoring tools]] section, . Finally, [[#Regular expressions reference|regular expression reference]] may be of some use to you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How question work==&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer. It extends it features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* &#039;&#039;&#039;pattern matching&#039;&#039;&#039; - using regular expressions you could create a powerful patterns describing possible students answers;&lt;br /&gt;
* &#039;&#039;&#039;hinting&#039;&#039;&#039; - when you students are stuck doing the question, you may allow them to ask it for next correct word (lexem) or character (with penalty if you wish so).&lt;br /&gt;
&lt;br /&gt;
===Settings, that affects how question will work===&lt;br /&gt;
====Case sensitivity====&lt;br /&gt;
You should know this setting from core Shortanswer question type. Note, however, that you can [[#Local case-sensitivity modifiers|change case sensitivity inside you regular expressions]], making only parts of it case sensitive.&lt;br /&gt;
&lt;br /&gt;
====Exact matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student&#039;s answer that suits the regular expression (or you answer). This part called &#039;&#039;&#039;match&#039;&#039;&#039;. Traditionally, regular expressions were used to look for matches &#039;&#039;&#039;inside&#039;&#039;&#039; strings, i.e. &#039;&#039;&#039;all&#039;&#039;&#039; &#039;&#039;regular expression&#039;&#039; should match, but it could match with a &#039;&#039;&#039;part of&#039;&#039;&#039; &#039;&#039;students response&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
; &#039;&#039;&#039;Yes&#039;&#039;&#039; : entire students response should match with regular expression.&lt;br /&gt;
; &#039;&#039;&#039;No&#039;&#039;&#039; : any part of students response could match with regular expression. You could still set some of you regex matching with whole student&#039;s response using [[#Anchoring|regular expression features]].&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Notation is the way you write you regexes. Or choose &amp;quot;Moodle shortanswer&amp;quot; notation to avoid regexes at all, still use hinting features.&lt;br /&gt;
; &#039;&#039;&#039;Regular expression&#039;&#039;&#039; : This is usual notation for regular expression. Precisely it is Perl-compatible regex dialect. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
; &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; : This notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
; &#039;&#039;&#039;Moodle shortanswer&#039;&#039;&#039; : Choose this notation and you can just copy answers from you shortanswer questions. The &#039;*&#039; wildcard is supported. By choosing NFA 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;
====Matching engines====&lt;br /&gt;
A matching engine means different program code that performs regular expression execution. 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;
&lt;br /&gt;
; &#039;&#039;&#039;PHP preg extension&#039;&#039;&#039; : Use it when you &#039;&#039;&#039;don&#039;t need hinting&#039;&#039;&#039; and &#039;&#039;&#039;other engines are rejecting you expressions&#039;&#039;&#039; as too difficult or you encounter bugs in them. 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;
; &#039;&#039;&#039;Non-deterministing finite state automata(NFA)&#039;&#039;&#039; : Use NFA engine to &#039;&#039;&#039;perform hinting&#039;&#039;&#039; for you students if it can handle you regular expressions. NFA engine is a custom PHP code that uses finite automata to perform matching. It is allow many (but not all) regular expression features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but may still contain bugs in rare cases. Not supported features for now include complex assertions, recursion and conditional subpatterns.&lt;br /&gt;
; &#039;&#039;&#039;Deterministic finite state automata (DFA)&#039;&#039;&#039; : WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine could, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting (not an easy thing to do using PHP at all) in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++);&lt;br /&gt;
* &#039;&#039;&#039;prinf language&#039;&#039;&#039; - a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
# **syntax tree** - shows you an inner structure of regular expression;&lt;br /&gt;
# **explaining graph** - shows you how you expression will work in a graphical way;&lt;br /&gt;
# **description** - formulate the meaning of you expression in the english language;&lt;br /&gt;
# **testing tool** - allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools from it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operator and precedence well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database when saving the question, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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. &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;
  TODO - write more about operands and operators, but simple.&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;
===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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106407</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106407"/>
		<updated>2013-08-22T21:18:47Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* How question work */&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 (thought you can use it without regexes for it&#039;s hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. First section should guide you in using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regular expression manuals, I&#039;m not going to repeat it there.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
Regardless of the way you use Preg question and you capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any sort of feedback with freely distributed software. But you shoudn&#039;t expect to get software which ideally suitting you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type in it may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want) know anything about regular expressions but next word(character) hinting seems useful===&lt;br /&gt;
You can use Preg question type just as Shortanswer questions with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read section about [[#How question work|question working]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regexes, but want to use pattern matching===&lt;br /&gt;
If creating regular expressions is a hard task for you, but you want to use their strength as patterns, you may make heavy use of authoring tools to create you questions. Authoring tools shows you a meaning of you expression in different way: an internal structure of expression(syntax tree), a visual path of matching (explaining graph) and a text description. They also allows you to test you regex against several strings and see, whether it work as expected. Experiment and play changing you regexes, see corresponding changes in authoring tools, and eventually you may get regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on you own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How question work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I could spend some effort to learn regular expressions well and be able to do anything I they could===&lt;br /&gt;
If you don&#039;t know well regular expression, but want to understand them really well and create complex regexes easy; if you want to know what you doing writing you regexes, instead of blunt trying, you should spent some time and effort understanding it. Do not worry - it&#039;s not as hard as it sounds.&lt;br /&gt;
&lt;br /&gt;
If you want to do that, read section about [[#Understanding regular expressions|understanding regular expressions]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you could see, if you really understand them well and they behave as expected. Syntax tree may be of special use for you, when you try to getting right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand well principles of regular expression, read sections about [[#How question work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know you possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write you regexes without much use of authoring tools, except testing tool to test you regexes.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How question work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in [[#Authoring tools|authoring tools]] section, . Finally, [[#Regular expressions reference|regular expression reference]] may be of some use to you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How question work==&lt;br /&gt;
Basically, this question type is an extended version of the Shortanswer. It extends it features in several different ways (you could use them in almost any combination):&lt;br /&gt;
* pattern matching - using regular expressions you could create a powerful patterns describing possible students answers;&lt;br /&gt;
* hinting - when you students are stuck doing the question, you may allow them to ask it for next correct word (lexem) or character (with penalty if you wish so).&lt;br /&gt;
&lt;br /&gt;
===Settings, that affects how question will work===&lt;br /&gt;
====Case sensitivity====&lt;br /&gt;
You should know this setting from core Shortanswer question type. Note, however, that you can [[#Local case-sensitivity modifiers|change case sensitivity inside you regular expressions]], making only parts of it case sensitive.&lt;br /&gt;
&lt;br /&gt;
====Exact matching====&lt;br /&gt;
&#039;&#039;&#039;Matching&#039;&#039;&#039; means finding a part of the student&#039;s answer that suits the regular expression (or you answer). This part called &#039;&#039;&#039;match&#039;&#039;&#039;. Traditionally, regular expressions were used to look for matches &#039;&#039;&#039;inside&#039;&#039;&#039; strings, i.e. &#039;&#039;&#039;all&#039;&#039;&#039; &#039;&#039;regular expression&#039;&#039; should match, but it could match with a &#039;&#039;&#039;part of&#039;&#039;&#039; &#039;&#039;students response&#039;&#039;. If you want such behaviour, you may set &amp;quot;Exact matching&amp;quot; to &amp;quot;No&amp;quot;. You could still set some of you answers matching with whole student&#039;s response using [[#Anchoring|regular expression features]].&lt;br /&gt;
&lt;br /&gt;
If you like you answers to match only with the whole student response, and don&#039;t know all regex mumbo-jumbo needed to do it, you may just set &amp;quot;Exact matching&amp;quot; to &amp;quot;Yes&amp;quot;, and question will do all the work for you. That is usual way when creating question, so &amp;quot;Yes&amp;quot; is default there.&lt;br /&gt;
&lt;br /&gt;
====Notations====&lt;br /&gt;
Notation is the way you write you regexes. Or choose &amp;quot;Moodle shortanswer&amp;quot; notation to avoid regexes at all, still use hinting features.&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. You may write regex on multiple strings for better reading - line breaks will be ignored.&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
* 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 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;
===Matching engines===&lt;br /&gt;
A matching engine means different program code that performs regular expression execution. 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;
&lt;br /&gt;
====PHP preg extension====&lt;br /&gt;
Use it when you don&#039;t need hinting and other engines are rejecting you expressions as too difficult or you encouner bugs in this engine.&lt;br /&gt;
&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;
&lt;br /&gt;
====Non-deterministing finite state automata(NFA)====&lt;br /&gt;
Use NFA engine to perform hinting for you students if it can handle you regular expressions.&lt;br /&gt;
&lt;br /&gt;
NFA engine is a custom PHP code that uses finite automata to perform matching. It is allow many (but not all) regular expression features and is thoroughly tested (it passes all tests from AT&amp;amp;T testregex suite and most tests from PCRE testinput1 suite for the features it supports, which means quite much), but may still contain bugs in rare cases.&lt;br /&gt;
&lt;br /&gt;
Not supported features for now include: &lt;br /&gt;
* complex assertions;&lt;br /&gt;
* recursion;&lt;br /&gt;
* conditional subpatterns.&lt;br /&gt;
&lt;br /&gt;
====Deterministic finite state automata (DFA)====&lt;br /&gt;
WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes that this engine accepts), it allows almost anything DFA engine could, but NFA engine much more tested and stable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Hinting===&lt;br /&gt;
NFA and DFA matching engines support hinting (not an easy thing to do using PHP at all) in the adaptive and interactive behaviours.&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 is unanchored (&amp;quot;Exact match&amp;quot; is set to &amp;quot;No&amp;quot;) 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;
====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 usually a desirable behaviour.&lt;br /&gt;
When possible, 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 graeds from 50% to 100% will be used for hinting and 0%-49% would not. Regular expressions not used for hinting work only when they have a full match with the student response.&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 (word) 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 they are 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 these languages (but there will be more):&lt;br /&gt;
* &#039;&#039;&#039;simple english&#039;&#039;&#039; - english language scanner recognize words, numbers and punctuation marks;&lt;br /&gt;
* &#039;&#039;&#039;C/C++ language&#039;&#039;&#039; - a programming language C (or C++);&lt;br /&gt;
* &#039;&#039;&#039;prinf language&#039;&#039;&#039; - a special language for formatting strings in C/C++ programming language, you will have it disabled probably.&lt;br /&gt;
&lt;br /&gt;
Administrator of the site can control what languages are available to the teachers, to avoid confusion. See the settings of the block &amp;quot;Formal languages&amp;quot; in the plugin settings menu.&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. Each language define their own word for it. You can enter another word in the question description, if you don&#039;t like default ones.&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), 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; will be 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 in 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 this, 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;
===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;
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 not problem but a feature.&lt;br /&gt;
&lt;br /&gt;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
# **syntax tree** - shows you an inner structure of regular expression;&lt;br /&gt;
# **explaining graph** - shows you how you expression will work in a graphical way;&lt;br /&gt;
# **description** - formulate the meaning of you expression in the english language;&lt;br /&gt;
# **testing tool** - allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools from it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operator and precedence well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database when saving the question, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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. &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;
  TODO - write more about operands and operators, but simple.&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;
===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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106406</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106406"/>
		<updated>2013-08-22T20:35:20Z</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 (thought you can use it without regexes for it&#039;s hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. First section should guide you in using of this docs, please use it with discretion. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regular expression manuals, I&#039;m not going to repeat it there.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
Regardless of the way you use Preg question and you capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any sort of feedback with freely distributed software. But you shoudn&#039;t expect to get software which ideally suitting you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type in it may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want) know anything about regular expressions but next word(character) hinting seems useful===&lt;br /&gt;
You can use Preg question type just as Shortanswer questions with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read section about [[#How question work|question working]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regexes, but want to use pattern matching===&lt;br /&gt;
If creating regular expressions is a hard task for you, but you want to use their strength as patterns, you may make heavy use of authoring tools to create you questions. Authoring tools shows you a meaning of you expression in different way: an internal structure of expression(syntax tree), a visual path of matching (explaining graph) and a text description. They also allows you to test you regex against several strings and see, whether it work as expected. Experiment and play changing you regexes, see corresponding changes in authoring tools, and eventually you may get regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on you own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How question work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I could spend some effort to learn regular expressions well and be able to do anything I they could===&lt;br /&gt;
If you don&#039;t know well regular expression, but want to understand them really well and create complex regexes easy; if you want to know what you doing writing you regexes, instead of blunt trying, you should spent some time and effort understanding it. Do not worry - it&#039;s not as hard as it sounds.&lt;br /&gt;
&lt;br /&gt;
If you want to do that, read section about [[#Understanding regular expressions|understanding regular expressions]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you could see, if you really understand them well and they behave as expected. Syntax tree may be of special use for you, when you try to getting right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand well principles of regular expression, read sections about [[#How question work|question working]] and [[#Regular expressions reference|regular expression reference]] (to know you possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write you regexes without much use of authoring tools, except testing tool to test you regexes.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How question work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in [[#Authoring tools|authoring tools]] section, . Finally, [[#Regular expressions reference|regular expression reference]] may be of some use to you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How question work==&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;
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 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!&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. Line breaks will be ignored - you can use them freely to structure big regexes.&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
* 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 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 they are 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;
===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;
WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes), it allows almost anything DFA engine could, but much more tested and stable.&lt;br /&gt;
&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;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
# **syntax tree** - shows you an inner structure of regular expression;&lt;br /&gt;
# **explaining graph** - shows you how you expression will work in a graphical way;&lt;br /&gt;
# **description** - formulate the meaning of you expression in the english language;&lt;br /&gt;
# **testing tool** - allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools from it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operator and precedence well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database when saving the question, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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. &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;
  TODO - write more about operands and operators, but simple.&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;
===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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106405</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106405"/>
		<updated>2013-08-22T20:32:58Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Major rearranging of material in the new sections, links to sections added to &amp;quot;how to use&amp;quot; section&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 (thought you can use it without regexes for it&#039;s hinting features). Regular expressions give vast capabilities and flexibility to both teachers when making questions and students when writing answers to them. First section should guide you in using of this docs, please use it with discreption. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regular expression manuals, I&#039;m not going to repeat it there.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
Regardless of the way you use Preg question and you capabilities, you could aways [[#The ways to give back|give back]]. It is hard to get any sort of feedback with freely distributed software. But you shoudn&#039;t expect to get software which ideally suitting you needs without telling anyone about these needs, or encouragement, or some non-difficult support to the authors. Sometimes as little as writing where you work and how you use (or what prevents you from using) Preg question type in it may help a lot.&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want) know anything about regular expressions but next word(character) hinting seems useful===&lt;br /&gt;
You can use Preg question type just as Shortanswer questions with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
* &#039;&#039;&#039;Exact matching&#039;&#039;&#039; =&amp;gt; Yes&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read section about [[#How question work|question working]] to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regexes, but want to use pattern matching===&lt;br /&gt;
If creating regular expressions is a hard task for you, but you want to use their strength as patterns, you may make heavy use of authoring tools to create you questions. Authoring tools shows you a meaning of you expression in different way: an internal structure of expression(syntax tree), a visual path of matching (explaining graph) and a text description. They also allows you to test you regex against several strings and see, whether it work as expected. Experiment and play changing you regexes, see corresponding changes in authoring tools, and eventually you may get regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on [[#Authoring tools|authoring tools]], than (probably after some experimenting with tools on you own) a start of section about [[#Understanding regular expressions|understanding regular expressions]] (this is optional, but may be interesting and help a lot). You should also read a section about [[#How question work|question working]] to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I could spend some effort to learn regular expressions well and be able to do anything I they could===&lt;br /&gt;
If you don&#039;t know well regular expression, but want to understand them really well and create complex regexes easy; if you want to know what you doing writing you regexes, instead of blunt trying, you should spent some time and effort understanding it. Do not worry - it&#039;s not as hard as it sounds.&lt;br /&gt;
&lt;br /&gt;
If you want to do that, read section about [[#Understanding regular expressions|understanding regular expressions]]. Then read slightly about [[#Authoring tools|authoring tools]] and use them to experiment creating regexes. With these tools you could see, if you really understand them well and they behave as expected. Syntax tree may be of special use for you, when you try to getting right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand well principles of regular expression, read sections about [[#How question work|question working]] and [[#Regular expression reference|regular expression reference]] (to know you possibilities, don&#039;t bother to understand or remember them all - just look there periodically for something new to learn). Now you should be able to write you regexes without much use of authoring tools, except testing tool to test you regexes.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about [[#How question work|question working]] to understand various settings and question behaviour under them. You also may be interested in regex testing in [[#Authoring tools|authoring tools]] section, . Finally, [[#Regular expression reference|regular expression reference]] may be of some use to you.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How question work==&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;
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 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!&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. Line breaks will be ignored - you can use them freely to structure big regexes.&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
* 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 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 they are 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;
===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;
WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes), it allows almost anything DFA engine could, but much more tested and stable.&lt;br /&gt;
&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;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
# **syntax tree** - shows you an inner structure of regular expression;&lt;br /&gt;
# **explaining graph** - shows you how you expression will work in a graphical way;&lt;br /&gt;
# **description** - formulate the meaning of you expression in the english language;&lt;br /&gt;
# **testing tool** - allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools from it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operator and precedence well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database when saving the question, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Understanding regular expressions==&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions in general===&lt;br /&gt;
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 at 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 also 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. &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;
  TODO - write more about operands and operators, but simple.&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;
===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;
==Regular expressions reference==&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;
#* full list of characters needs escaping &#039;&#039;&#039;\ ^ $ . [ ] | ( ) ? * + { }&#039;&#039;&#039;&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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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;
===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;
===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;
==The ways to give back==&lt;br /&gt;
This software is considered scientific project and as such needs help any scientific project can use:&lt;br /&gt;
* an evidence, that results of our work (i.e. Preg questoin type) is really useful to people and was used in production environment;&lt;br /&gt;
* a cooperative work to research it&#039;s effectiveness for various applications - basically you need to write about how you used this question type and make some survey with you teachers and/or students about it - but it can include co-authoring a conference thesis or journal article;&lt;br /&gt;
* cooperating in writing article or help in publishing it in english-language journals (information and help in grants for further work is welcome too).&lt;br /&gt;
&lt;br /&gt;
If you consider any way of helping, do not hesitate to write me about it and ask any questions about details. You may receive individual help during such work too (for example, doing cooperative research I may give you tips how to improve you regexes, etc).&lt;br /&gt;
&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 - there are many settings in the question, and regex can be quite complex, so it&#039;s hard to do all testing by developers themselves.&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;
* Improve 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 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/pl/index.php?title=Preg_question_type&amp;diff=106404</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106404"/>
		<updated>2013-08-22T19:56:42Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Wrote a draft of section about docs usage, change level of headers&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 regular expressions as a particular case of expressions, and a part about Preg question type itself. If you are familiar with regex syntax you may skip the first two parts and go to [[#Usage of the Preg question type|usage of the Preg question type]]. More details about regex syntax can be found at http://www.nusphere.com/kb/phpmanual/reference.pcre.pattern.syntax.htm. There are many good regular expression manuals, I&#039;m not going to repeat it there.&lt;br /&gt;
&lt;br /&gt;
Authors:&lt;br /&gt;
# Idea, design, question type and behaviours code, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
==Ways to use Preg questions and this docs==&lt;br /&gt;
&lt;br /&gt;
===I don&#039;t (want) know anything about regular expressions but next word(character) hinting seems useful===&lt;br /&gt;
You can use Preg question type just as Shortanswer questions with advanced hinting, without any knowledge about regular expressions. To do this, you need to choose &lt;br /&gt;
* &#039;&#039;&#039;Notation&#039;&#039;&#039; =&amp;gt; Moodle shortanswer&lt;br /&gt;
* &#039;&#039;&#039;Engine&#039;&#039;&#039; =&amp;gt; Non-deterministic finite state automata&lt;br /&gt;
&lt;br /&gt;
After that, you can just copy answers from you shortanswer questions. You may want to read section about question working to understand more about hinting settings.&lt;br /&gt;
&lt;br /&gt;
===I have a vague knowledge of regexes, but want to use pattern matching===&lt;br /&gt;
If creating regular expressions is a hard task for you, but you want to use their strength as patterns, you may make heavy use of authoring tools to create you questions. Authoring tools shows you a meaning of you expression in different way: an internal structure of expression(syntax tree), a visual path of matching (explaining graph) and a text description. They also allows you to test you regex against several strings and see, whether it work as expected. Experiment and play changing you regexes, see corresponding changes in authoring tools, and eventually you may get regex you want.&lt;br /&gt;
&lt;br /&gt;
Read the section on authoring tools, than (maybe after some experimenting) a start of section about understanding regular expressions. You should also read a section about question working to better understand various settings and how they affects you questions.&lt;br /&gt;
&lt;br /&gt;
===I could spend some effort to learn regular expressions well and be able to do anything I they could===&lt;br /&gt;
If you don&#039;t know well regular expression, but want to understand them really well and create complex regexes easy; if you want to know what you doing writing you regexes, instead of blunt trying, you should spent some time and effort understanding it. Do not worry - it&#039;s not as hard as it sounds.&lt;br /&gt;
&lt;br /&gt;
If you want to do that, read section about understanding regular expressions. Then read slightly about authoring tools and use them to experiment creating regexes to see, if you really understand them well and they behave as expected. Syntax tree may be of special use for you, when you try to getting right meaning of &#039;&#039;precedence&#039;&#039; and &#039;&#039;arity&#039;&#039;. After you understand well principles of regular expression, read sections about question working and regular expression reference (to know you possibilities). Now you should be able to write you regexes without much use of authoring tools, except testing tool to test you regexes.&lt;br /&gt;
&lt;br /&gt;
===I know regular expressions well enought to write them on my own without further guidance===&lt;br /&gt;
You should read about question working to understand various settings and question behaviour under them. You also may be interested in regex testing in authoring tools section. Finally, regular expression reference may be of some use to you.&lt;br /&gt;
&lt;br /&gt;
==Understanding expressions==&lt;br /&gt;
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 at 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 also 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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
===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;
===Complex 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 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!&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. Line breaks will be ignored - you can use them freely to structure big regexes.&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
* 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 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 they are 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;
===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;
WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes), it allows almost anything DFA engine could, but much more tested and stable.&lt;br /&gt;
&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;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
# **syntax tree** - shows you an inner structure of regular expression;&lt;br /&gt;
# **explaining graph** - shows you how you expression will work in a graphical way;&lt;br /&gt;
# **description** - formulate the meaning of you expression in the english language;&lt;br /&gt;
# **testing tool** - allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools from it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operator and precedence well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database when saving the question, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&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;
==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;
* Improve 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 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/pl/index.php?title=Plik:qtype_preg_authortools4.png&amp;diff=106400</id>
		<title>Plik:qtype preg authortools4.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools4.png&amp;diff=106400"/>
		<updated>2013-08-22T14:34:52Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Oasychev uploaded a new version of &amp;amp;quot;File:qtype preg authortools4.png&amp;amp;quot;: Deleted wide unused area on the left.&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/pl/index.php?title=Preg_question_type&amp;diff=106399</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106399"/>
		<updated>2013-08-22T14:31:14Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Explaining graph */ - fixing image link&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 regular expressions as a particular case of expressions, and a part about Preg question type itself. If you are familiar with regex syntax you may skip the first two parts and go to [[#Usage of the Preg question type|usage of the Preg question type]]. More details about 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, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
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 at 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 also 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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
====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 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!&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. Line breaks will be ignored - you can use them freely to structure big regexes.&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
* 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 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;
WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes), it allows almost anything DFA engine could, but much more tested and stable.&lt;br /&gt;
&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;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
# **syntax tree** - shows you an inner structure of regular expression;&lt;br /&gt;
# **explaining graph** - shows you how you expression will work in a graphical way;&lt;br /&gt;
# **description** - formulate the meaning of you expression in the english language;&lt;br /&gt;
# **testing tool** - allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools from it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operator and precedence well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.png|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database when saving the question, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&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;
==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/pl/index.php?title=Preg_question_type&amp;diff=106196</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106196"/>
		<updated>2013-07-26T15:53:08Z</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 regular expressions as a particular case of expressions, and a part about Preg question type itself. If you are familiar with regex syntax you may skip the first two parts and go to [[#Usage of the Preg question type|usage of the Preg question type]]. More details about 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, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
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 at 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 also 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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
====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 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!&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. Line breaks will be ignored - you can use them freely to structure big regexes.&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
* 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 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;
WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes), it allows almost anything DFA engine could, but much more tested and stable.&lt;br /&gt;
&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;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
# **syntax tree** - shows you an inner structure of regular expression;&lt;br /&gt;
# **explaining graph** - shows you how you expression will work in a graphical way;&lt;br /&gt;
# **description** - formulate the meaning of you expression in the english language;&lt;br /&gt;
# **testing tool** - allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools from it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operator and precedence well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.jpg|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database when saving the question, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&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;
==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/pl/index.php?title=Preg_question_type&amp;diff=106195</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106195"/>
		<updated>2013-07-26T15:51:21Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Development plans */&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 regular expressions as a particular case of expressions, and a part about Preg question type itself. If you are familiar with regex syntax you may skip the first two parts and go to [[#Usage of the Preg question type|usage of the Preg question type]]. More details about 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, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support, explaining tree (authoring tool) - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
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 at 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 also 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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
====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 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!&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. Line breaks will be ignored - you can use them freely to structure big regexes.&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
* 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 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;
WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes), it allows almost anything DFA engine could, but much more tested and stable.&lt;br /&gt;
&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;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
# **syntax tree** - shows you an inner structure of regular expression;&lt;br /&gt;
# **explaining graph** - shows you how you expression will work in a graphical way;&lt;br /&gt;
# **description** - formulate the meaning of you expression in the english language;&lt;br /&gt;
# **testing tool** - allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools from it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operator and precedence well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.jpg|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database when saving the question, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&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;
==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/pl/index.php?title=Preg_question_type&amp;diff=106194</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106194"/>
		<updated>2013-07-26T15:50:50Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* The ways to give back */&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 regular expressions as a particular case of expressions, and a part about Preg question type itself. If you are familiar with regex syntax you may skip the first two parts and go to [[#Usage of the Preg question type|usage of the Preg question type]]. More details about 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, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support, explaining tree (authoring tool) - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
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 at 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 also 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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
====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 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!&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. Line breaks will be ignored - you can use them freely to structure big regexes.&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
* 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 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;
WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes), it allows almost anything DFA engine could, but much more tested and stable.&lt;br /&gt;
&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;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
# **syntax tree** - shows you an inner structure of regular expression;&lt;br /&gt;
# **explaining graph** - shows you how you expression will work in a graphical way;&lt;br /&gt;
# **description** - formulate the meaning of you expression in the english language;&lt;br /&gt;
# **testing tool** - allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools from it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operator and precedence well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.jpg|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database when saving the question, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&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;
===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/pl/index.php?title=Plik:qtype_preg_authortools71.png&amp;diff=106193</id>
		<title>Plik:qtype preg authortools71.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools71.png&amp;diff=106193"/>
		<updated>2013-07-26T15:48:44Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Oasychev uploaded a new version of &amp;amp;quot;File:qtype preg authortools71.png&amp;amp;quot;&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/pl/index.php?title=Dyskusja_pliku:qtype_preg_authortools7.jpg&amp;diff=106192</id>
		<title>Dyskusja pliku:qtype preg authortools7.jpg</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Dyskusja_pliku:qtype_preg_authortools7.jpg&amp;diff=106192"/>
		<updated>2013-07-26T15:48:26Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: Created page with &amp;quot;this file is obsolete and could be deleted, only I don&amp;#039;t seems to have rights to do it. Its replacement has png extension, so wiki don&amp;#039;t allow to upload it as a new version. -...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;this file is obsolete and could be deleted, only I don&#039;t seems to have rights to do it.&lt;br /&gt;
Its replacement has png extension, so wiki don&#039;t allow to upload it as a new version.&lt;br /&gt;
--[[User:Oleg Sychev|Oleg Sychev]] ([[User talk:Oleg Sychev|talk]]) 23:48, 26 July 2013 (WST)&lt;/div&gt;</summary>
		<author><name>Oasychev</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106191</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106191"/>
		<updated>2013-07-26T15:44:32Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Explaining graph */ - updated picture 7&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 regular expressions as a particular case of expressions, and a part about Preg question type itself. If you are familiar with regex syntax you may skip the first two parts and go to [[#Usage of the Preg question type|usage of the Preg question type]]. More details about 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, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support, explaining tree (authoring tool) - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
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 at 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 also 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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
====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 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!&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. Line breaks will be ignored - you can use them freely to structure big regexes.&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
* 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 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;
WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes), it allows almost anything DFA engine could, but much more tested and stable.&lt;br /&gt;
&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;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
# **syntax tree** - shows you an inner structure of regular expression;&lt;br /&gt;
# **explaining graph** - shows you how you expression will work in a graphical way;&lt;br /&gt;
# **description** - formulate the meaning of you expression in the english language;&lt;br /&gt;
# **testing tool** - allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools from it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operator and precedence well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.jpg|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools71.png|graph for regex (?:(abc)|de)f ]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database when saving the question, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&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/pl/index.php?title=Plik:qtype_preg_authortools71.png&amp;diff=106190</id>
		<title>Plik:qtype preg authortools71.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools71.png&amp;diff=106190"/>
		<updated>2013-07-26T15:42:45Z</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/pl/index.php?title=Preg_question_type&amp;diff=106189</id>
		<title>Preg question type</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Preg_question_type&amp;diff=106189"/>
		<updated>2013-07-26T15:28:38Z</updated>

		<summary type="html">&lt;p&gt;Oasychev: /* Authoring tools */&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 regular expressions as a particular case of expressions, and a part about Preg question type itself. If you are familiar with regex syntax you may skip the first two parts and go to [[#Usage of the Preg question type|usage of the Preg question type]]. More details about 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, hinting and error reporting - Oleg Sychev.&lt;br /&gt;
# Regex parsing, NFA regex matching engine, testing of the matchers, backup&amp;amp;restore and unicode support, explaining tree (authoring tool) - Valeriy Streltsov.&lt;br /&gt;
# DFA regex matching engine - Dmitriy Kolesov.&lt;br /&gt;
# Explaining graph (authoring tool) - Vladimir Ivanov.&lt;br /&gt;
# Explaining tree, regular expression testing (authoring tools) - Grigory Terekhov.&lt;br /&gt;
# Regex description (authoring tool) - Dmitriy Pahomov.&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 have been implemented in Preg question type.&lt;br /&gt;
&lt;br /&gt;
===Understanding expressions===&lt;br /&gt;
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 at 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 also 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 come the &#039;&#039;&#039;operators&#039;&#039;&#039; that allow us to define an expression that 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;
====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 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!&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression&#039;&#039;&#039; which means Perl-compatible regex dialect is the default one. Line breaks will be ignored - you can use them freely to structure big regexes.&lt;br /&gt;
* The &#039;&#039;&#039;Regular expression (extended)&#039;&#039;&#039; notation is there for a really complex regexes. It is similar to the PHP &#039;x&#039; modifier. It will ignore any unescaped whitespaces in you regexes, that are not part of character classes (use \s instead) - so that you may freely format you regexes with spaces. It will also ignore line breaks with one useful exception: everything from (unescaped and not part of character class) # character to the end of that string is treated as commentary.&lt;br /&gt;
* 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 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;
WARNING: This engine lacking support in the past year. Use NFA engine instead if you could (i.e. don&#039;t get rejected regexes), it allows almost anything DFA engine could, but much more tested and stable.&lt;br /&gt;
&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;
==Authoring tools==&lt;br /&gt;
&lt;br /&gt;
Authoring tools are there to help you write, test and understand you regexes. For now they can show you the meaning of written regex (and it&#039;s parts), and test it. Authoring tools are activated by pressing &amp;quot;edit&amp;quot; icon near regex field. &lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools1.png|authoring tools icon]]&lt;br /&gt;
&lt;br /&gt;
There are four authoring tools available:&lt;br /&gt;
# **syntax tree** - shows you an inner structure of regular expression;&lt;br /&gt;
# **explaining graph** - shows you how you expression will work in a graphical way;&lt;br /&gt;
# **description** - formulate the meaning of you expression in the english language;&lt;br /&gt;
# **testing tool** - allows you to enter strings and see how they match with you regexes.&lt;br /&gt;
&lt;br /&gt;
===Regular expression area===&lt;br /&gt;
There you cold enter (or edit) regular expression and refresh all the tools from it.&lt;br /&gt;
&lt;br /&gt;
  TODO You could also select a part of regular expression and it will be mapped on the tree, graph and description.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Syntax tree===&lt;br /&gt;
As was said above, regular expression is in fact expression - a tree of operators and operands. Syntax tree shows graphically this inner structure of expression: what is inside what.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t understand operator and precedence well, it may have a small meaning to you. But it is still useful to find out, where you need parenthesis: cf. trees for &#039;&#039;ab+&#039;&#039; (a) and &#039;&#039;(ab)+&#039;&#039; (b) on the picture below.&lt;br /&gt;
[[Image:qtype preg authortools2.png|parenthesis in the structure of regex]]&lt;br /&gt;
&lt;br /&gt;
The part of expression you selected is shown by dotted part of the tree.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools3.jpg|leftmost node of the tree is selected]]&lt;br /&gt;
&lt;br /&gt;
===Explaining graph===&lt;br /&gt;
The graph shows how regular expression works. It&#039;s nodes are matched characters, it&#039;s edges shows paths throught the nodes from beginning to the end.&lt;br /&gt;
[[Image:qtype preg authortools4.jpg|alternatives and concatenation]]&lt;br /&gt;
&lt;br /&gt;
Oval nodes represent individual characters, character sequences (so that graph isn&#039;t extremly big) or single special character classes (in which case they change line colour). Complex character classes shown as rectangles. Simple assertions are checked between nodes, so they are written on the edges.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools5.jpg|graph for regex ^\dabc[!,0-9]$]]&lt;br /&gt;
&lt;br /&gt;
Dotted rectangles shows you repeated parts of you expression.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools6.jpg|graph for regex \d*]]&lt;br /&gt;
&lt;br /&gt;
Solid line rectangles show you subexpressions. When expression is matched, it remembers what part of string matched with each subexpression. You could insert it in the feedback or use in backreference in expression. If you do not need to remember part of the match, you may speed up you expression using (?:  ) instead of (  ) parenthesis, that will speed up matching.&lt;br /&gt;
&lt;br /&gt;
[[Image:qtype preg authortools7.jpg|graph for regex (abc)|(?:abc)]]&lt;br /&gt;
&lt;br /&gt;
  TODO Green rectangle shows you selected part of expression.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Description===&lt;br /&gt;
Description try to formulate a sentence, describing you how expression is supposed to work.&lt;br /&gt;
&lt;br /&gt;
===Testing tool===&lt;br /&gt;
You may enter a set of strings there, for matching against you expression. You&#039;ll see a coloured strings, showing which part of you string matched with expressions, so you could test, if it performing as you expcted.&lt;br /&gt;
&lt;br /&gt;
 TODO The strings will be saved in database when saving the question, if you save regex (they will be lost if you close window with &amp;quot;cancel&amp;quot; button).&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/pl/index.php?title=Plik:qtype_preg_authortools4.png&amp;diff=106188</id>
		<title>Plik:qtype preg authortools4.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools4.png&amp;diff=106188"/>
		<updated>2013-07-26T15:26:33Z</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/pl/index.php?title=Plik:qtype_preg_authortools2.png&amp;diff=106187</id>
		<title>Plik:qtype preg authortools2.png</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/pl/index.php?title=Plik:qtype_preg_authortools2.png&amp;diff=106187"/>
		<updated>2013-07-26T15:24:54Z</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>
</feed>