I see there is a new class for regular expressions - QRegularExpression
. Is it just a typedef for QRegExp, or a new class, or what? And why do we need it, we already have QRegExp?
2 Answers
Ok, after some more digging into the docs, I found it really is a new class, it has improvements, but it is only available in Qt5, so you can't use it if you want to compile on both Qt4 and Qt5:
Notes for QRegExp Users
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. The biggest difference is that QRegularExpression simply holds a regular expression, and it's not modified when a match is requested. Instead, a QRegularExpressionMatch object is returned, in order to check the result of a match and extract the captured substring. The same applies with global matching and QRegularExpressionMatchIterator.
-
4Yeah, well, I spent some time trying to write comprehensive documentation for QRegularExpession, so there's that :) – peppe Apr 12 '15 at 21:50
-
29I wrote the entire class. If you think docs could be improved, please submit such a suggestion on the bugreport. – peppe Apr 14 '15 at 10:39
-
22"- You wrote that part of the doc?" "- I wrote the entire class." This made me lol. – Gras Double Nov 26 '15 at 18:27
-
3Thanks @peppe both for writing the entire class and the doc! That beaing said, I really think that the first line of the the following two URLs: http://doc.qt.io/qt-4.8/qregexp.html & http://doc.qt.io/qt-5/qregularexpression.html should say "please note, there are TWO regex systems in Qt [...]" – Mr. Developerdude May 17 '16 at 21:57
-
Well, there's a note in QRegExp documentation saying that QRegularExpression is the recommended class to use in Qt 5; OTOH QRegularExpression docs treat extensively the problem of porting away from QRegExp. Feel free to submit suggestions on how to improve the wording on Qt's bugtracker. – peppe May 18 '16 at 07:28
-
@peppe, Does it support look behinds? I can't seem to find any references for this in the docs. – Iulian Onofrei Aug 19 '16 at 12:19
-
@IulianOnofrei: yes, it does support fixed-length look behinds. It supports anything on the [pcresyntax(3) manual page](http://www.pcre.org/original/doc/html/pcresyntax.html), with the exception of callouts, duplicated named capture groups and other such small limitations (as listed in the class docs). – peppe Aug 25 '16 at 12:27
At least for Qt 4.8. I can give a very practical reason to use QRegularExpressions
instead of QRegExp
:
Do these look dangerous to you?
int index = myQString.indexOf(myQRegExp);
bool okay = myQString.contains(myQRegExp);
Both lines can corrupt your heap, crash or hang your application. I experienced heap corruption and hang with Qt 4.8. The blog post QString::indexOf() versus Qt 4.5 explains that QString::indexOf()
modifies a const QRegExp
object. QString::contains()
inlines QString::indexOf()
so it's the same problem.
If you're stuck with Qt4 and thus QRegExp, you could use
int index = myQRegExp.indexIn(myQString);
bool okay = (myQRegExp.indexIn(myQString) != -1);
in your sources instead. Or patch the Qt Sources.

- 986
- 10
- 30