12

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
Evan Carroll
  • 7,071
  • 2
  • 32
  • 58

1 Answers1

19

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.

Evan Carroll
  • 7,071
  • 2
  • 32
  • 58
  • Thank you, this saved me a lot of rooting around to get the information I needed. Source code may be the definitive documentation, but this information is too important not to be documented elsewhere in QGIS online documentation. – bixb0012 Dec 20 '22 at 23:37
  • @bixb0012 np, upvote the question/answer if you found it useful. I asked and self-answered to save everyone time. – Evan Carroll Dec 21 '22 at 00:46