Sunday, 11 September 2016

What regular expression engine does QGIS use?


Where can I find the documentation about QGIS regular expression engine and how to craft a pattern with it. I see the following regex functions documented




  • regexp_replace

  • regexp_substr

  • regexp_matches

  • regexp_match



Answer



QGIS, uses QT's QRegularExpression. which is itself just PCRE.



QRegularExpression implements Perl-compatible regular expressions. It fully supports Unicode. For an overview of the regular expression syntax supported by QRegularExpression, please refer to the aforementioned pcrepattern(3) man page. A regular expression is made up of two things: a pattern string and a set of pattern options that change the meaning of the pattern string.




You can see the use of it in src/core/qgsexpression.cpp


Note one Gotcha with the QT C++ implementation is that you have to escape \,



Note that due to C++ literal strings rules, you must escape all backslashes inside the pattern string with another backslash:



// matches two digits followed by a space and a word
QRegularExpression re("\\d\\d \\w+");

// matches a backslash
QRegularExpression re2("\\\\");


Another but very minor non-gotcha, is



QRegularExpression does not support all the features available in Perl-compatible regular expressions. The most notable one is the fact that duplicated names for capturing groups are not supported, and using them can lead to undefined behaviour.



No comments:

Post a Comment