Port QRegExp::exactMatch() in Qt6
Asked Answered
P

2

9

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.

Paganini answered 29/7, 2021 at 13:49 Comment(0)
S
9

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

QRegularExpression version(QRegularExpression::anchoredPattern(QLatin1String("(.+)_v(\\d+)")));
Suwannee answered 29/7, 2021 at 14:14 Comment(0)
O
6

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 ^...$

Obryant answered 27/1, 2023 at 15:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.