Search code examples
c++qtqregexp

Regular expression of Qt don't match as expected


Why is the regular expression of Qt a little different? I can match correctly in regular matching software, but not in Qt. For example.

QString tstring = "scale(1.1) rotate(180) translate(1,0)";
QRegExp re("(?<=[\\)])."); 
QStringList tlist = tstring.split(re);

I want to separate the three with spaces to get the three QString "scale(1.1)", "rotate(180)" "translate(1,0)"


Solution

  • Different syntaxes of regular expressions exist. Qt implements some of them:

    • QRegularExpression (>= Qt5): implements Perl-compatible regular expressions

    • QRegExp: implements multiple regular expression forms, see QRegExp::PatternSyntax

      • QRegExp::RegExp (the default): Qt's regexp language modeled on Perl's regexp language
      • QRegExp::Wildcard: similar to wildcards used by shells.
      • QRegExp::W3CXmlSchema11: implementation of the W3C XML Schema 1.1 specification.
      • ...

    Note that QRegularExpression is the recommended one:

    The QRegularExpression class introduced in Qt 5 is a big improvement upon QRegExp, in terms of APIs offered, supported pattern syntax and speed of execution.

    A detailed overview of the differences between QRegExp and QRegularExpression can be found in the docs.