[英]Port QRegExp::exactMatch() in Qt6
I'm porting a Qt5 application to Qt6.我正在将 Qt5 应用程序移植到 Qt6。 I want to move away from Qt5CoreCompat module of Qt6 as soon as possible.我想尽快离开 Qt6 的 Qt5CoreCompat 模块。 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.我的问题是QRegExp
class,应该用QRegularExpression
class 替换。大多数补丁相对微不足道,但我如何在 Qt6 中移植QRegExp::exactMatch()
。 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:我没有在QRegularExpressionMatch
class 中看到执行此操作的方法。我想解决方案可能是这样的:
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.我想要和以前一样的行为。
The documentation suggests using the anchoredPattern
helper function to do the anchoring from the regular expression itself:该文档建议使用anchoredPattern 帮助程序anchoredPattern
从正则表达式本身进行锚定:
QRegularExpression version(QRegularExpression::anchoredPattern(QLatin1String("(.+)_v(\\d+)")));
migrate QRegExp::exactMatch
to qt6 QRegularExpression::match
将QRegExp::exactMatch
迁移到 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 ^...$
这假设正则表达式是用^...$
“锚定”的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.