Perl-Compatible Regular Expression

Question types ::: qtype_preg
Maintained by Oleg Sychev
A question that could check student's response against several regular expressions and could give next character and next word hints. If you do not know what regular expression is, it could also work like Moodle Shortanswer question with hints, just choose "Moodle shortanswer" as notation and you could enter answers just like in shortanswer question type.
Latest release:
43 sites
5 fans
Current versions available: 9

This question requires 5 other plugins to work. If you want easy install, download full archive from our BitBucket site. You could unpack it into Moodle core folder (the one with config.php) and it will place anything where it belongs.

If you want to fully use Authoring Tools, you should also have Open Source Graphviz packet installed and path to it specified into Moodle. It is necessary to draw Syntax Tree and Explaining Graph.


This plugin is part of set Perl-Compatible Regular Expression Set.


Screenshot #0
Screenshot #1
Screenshot #2


Oleg Sychev (Lead maintainer): Idea, design, question type and behaviours code, hinting, error reporting, regular expression testing (authoring tool)
Valeriy Streltsov: Regex parsing, NFA regex matching engine, matchers testing, backup&restore, unicode support, selection in regex text (in authoring tools)
Dmitry Pahomov: Regex description (authoring tool)
Vladimir Ivanov: Explaining graph (authoring tool)
Grigory Terekhov: Syntax tree (authoring tool)
Elena Lepilkina: Assertions support
Artem Prokudin: Typo detection
Please login to view contributors details and/or to contact them

Comments RSS


  • Sat, Oct 18, 2014, 8:52 PM
    Fatal error: Cannot redeclare qtype_preg_fa_avoid_wordbreaks_test::test_two_lines() in /warehouse/lampp/htdocs/moodle/question/type/preg/tests/fa_intersection_test.php on line 540
  • Sat, Oct 18, 2014, 9:48 PM
    Thank for the notion, Jaroslaw, but some uint-tests there (like fa_intersection_test.php) are part of ongoing development features for future releases and code under testing doesn't called anywhere from released production code.
  • Tue, Nov 18, 2014, 12:17 AM
    Thanks for the nice plugin. A typical problem is an correct answer ( as in your example) like
    "blue, white, red". Is it possible to add an matching engine which goes through all answers and calcs additive the matching percents, eg.:
    33% for .*blue.*
    33% for .*white.*
    33% for .*red.*
    (-10% for not .*,.*and.*)
    ? This would strongly simplify the permutations of partly correct answers.

  • Tue, Nov 18, 2014, 6:56 AM
    Hi, Sepp. This is more another grading algorithm than matching engine. It will also require separate interface to enter answers, as you will need to enter both complete (for hinting generation) and partial answers. Also, not in every case you would want to allow elements of the answer in any order. This will require thorought discussion. Please create an issue on to discuss this.
  • Tue, Nov 18, 2014, 7:28 AM
    Sepp, if you are really into creating an asnwers from some words in some order, this (my) question type may suit you much better - . It can break up answer to words, detect missing, extraneous and misplaced words and grade it accordingly, giving hints.

    Regular expressions are used to write all kind of nasty thing, they can be non-linear, use repetitions, backreferences, recursion etc. I can not imagine right now how what you want should behave for complex regular expression. If you still wish to discuss this to example - let's do it on the tracker (link above). But I think than CorrectWriting question will give you what you want without regular expressions.
  • Fri, Nov 21, 2014, 2:40 AM
    Thanks for your quick response. I had a look on your correct writing plugin and it seems very useful for learning.
    I think, to grade fully correct answers with regex is a trivial thing. The difficult job is to grade partially correct answers.
    My idea was to search (with regex) in the student's answer for special expressions and have points for it.
    e.g: in Java: for(int i=0;i<10;i++)
    One regex checks for fully correctness; if not a serie of regexes looks for correct parts like
    "int i=0" or "i<10|i<=9" or "i++|i+=1|i=i+1|i=1+i" or "for(.*;.*;)"
    and have 30%+30%+30%+5% points for it. A student answer can have a combination of errors, but the correct parts give percents of points.
    This is what I meant. Each regex needs two fields for successor regexes for matched / unmatched. (like in the math plugin STACK). So it is possible to build up a decision tree with matching or unmatching regexes. (Sure, I can use use the math plugin and take usage of maxima's regex capabilities, but this plugin is more performant)
  • Fri, Jul 3, 2015, 5:47 AM
    Sepp, it's not that easy. You example with Java would accept "for(i++;int i=0; i<10)" as a correct answer which is far from what Java expects. It also will accept something like for(int i=0 some_strange_additionally_garbage_can_be_inserted_in_many_places;i<10;i++)" as a correct too,
  • Fri, Jul 3, 2015, 5:48 AM
    Well, not correct but 95% for such answers is too much IMHO.
  • Thu, Jul 27, 2017, 4:19 AM
    Hi Oleg, do you plan to provide Moodle 3.1+ support, I am in the process of upgrading a Moodle from 2.8 to 3.1, and it seems to be breaking Unit tests. So I am willing to know about 3.1 compatibility.
  • Thu, Aug 17, 2017, 4:22 AM
    Hi, Pramith. Yes, we are currently testing Preg 3.1 and working on support for Moodle 3.2 (the big UI changes affects Javascript question editing interface too much, but core questions should work fine). We are currently on vacations now, but I hope to do this in September.

    What unit tests file gives failure to you? If you run cross_tests.php (more than 2500 tests) there are tests that are expected to break for several very obscure cases. There are also 1-2 obsolete unit-tests files probably too.
  • Thank for the notion
Please login to post comments