6

I'm porting a Qt5 application to Qt6. I want to move away from Qt5CoreCompat module of Qt6 as soon as possible. My problem is with QRegExp class which should be replaced with QRegularExpression class. Most patches are relatively trivial but how can I port QRegExp::exactMatch() in Qt6. Here is some code from the application:

QRegExp version(QLatin1String("(.+)_v(\\d+)"));
if (version.exactMatch(completeBaseName/*QString*/))
{
        // some code
}

I don't see a way to do this in QRegularExpressionMatch class. I guess solution might be something like this:

QRegularExpression version(QLatin1String("(.+)_v(\\d+)"));
QRegularExpressionMatch match = version.match(completeBaseName);
if (match.hasMatch())
{
        // Find exact match or not
}

I want to have the same behavior as before.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
anjanik012
  • 149
  • 3
  • 11

2 Answers2

6

The documentation suggests using the anchoredPattern helper function to do the anchoring from the regular expression itself:

QRegularExpression version(QRegularExpression::anchoredPattern(QLatin1String("(.+)_v(\\d+)")));
Botje
  • 26,269
  • 3
  • 31
  • 41
1

migrate qt5 QRegExp::exactMatch to qt6 QRegularExpression::match

find . -name '*.cpp' |
xargs grep -l '\.exactMatch(' |
xargs sed -i -E 's/(.*?)\.exactMatch\((.*?)\)/\1.match(\2).hasMatch()/'

example

-    return someRegex.exactMatch(str);
+    return someRegex.match(str).hasMatch();

this assumes the regex is "anchored" with ^...$

milahu
  • 2,447
  • 1
  • 18
  • 25