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?
我看到正则表达式有一个新类 - QRegularExpression。它只是QRegExp的typedef,还是新类,还是什么?为什么我们需要它,我们已经有了QRegExp?
2 个解决方案
#1
38
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:
好的,经过一些深入研究,我发现它确实是一个新类,它有改进,但它只在Qt5中可用,所以如果你想在Qt4和Qt5上编译你就不能使用它:
Notes for QRegExp Users
QRegExp用户注意事项
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.
Qt 5中引入的QRegularExpression类是QRegExp的一个重大改进,提供的API,支持的模式语法和执行速度。最大的区别是QRegularExpression只保存一个正则表达式,并且在请求匹配时不会修改它。而是返回一个QRegularExpressionMatch对象,以便检查匹配的结果并提取捕获的子字符串。这同样适用于全局匹配和QRegularExpressionMatchIterator。
#2
0
At least for Qt 4.8. I can give a very practical reason to use QRegularExpressions
instead of QRegExp
:
至少对于Qt 4.8。我可以给出一个非常实际的理由来使用QRegularExpressions而不是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.
这两行都可能破坏您的堆,崩溃或挂起您的应用程序。我经历了堆损坏并且在Qt 4.8中挂起。博客文章QString :: indexOf()与Qt 4.5相比,QString :: indexOf()修改了一个const QRegExp对象。 QString :: contains()内联QString :: indexOf(),因此它也是同样的问题。
If you're stuck with Qt4 and thus QRegExp, you could use
如果您坚持使用Qt4并因此使用QRegExp,那么您可以使用
int index = myQRegExp.indexIn(myQString);
bool okay = (myQRegExp.indexIn(myQString) != -1);
in your sources instead. Or patch the Qt Sources.
而是在你的来源。或修补Qt来源。
#1
38
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:
好的,经过一些深入研究,我发现它确实是一个新类,它有改进,但它只在Qt5中可用,所以如果你想在Qt4和Qt5上编译你就不能使用它:
Notes for QRegExp Users
QRegExp用户注意事项
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.
Qt 5中引入的QRegularExpression类是QRegExp的一个重大改进,提供的API,支持的模式语法和执行速度。最大的区别是QRegularExpression只保存一个正则表达式,并且在请求匹配时不会修改它。而是返回一个QRegularExpressionMatch对象,以便检查匹配的结果并提取捕获的子字符串。这同样适用于全局匹配和QRegularExpressionMatchIterator。
#2
0
At least for Qt 4.8. I can give a very practical reason to use QRegularExpressions
instead of QRegExp
:
至少对于Qt 4.8。我可以给出一个非常实际的理由来使用QRegularExpressions而不是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.
这两行都可能破坏您的堆,崩溃或挂起您的应用程序。我经历了堆损坏并且在Qt 4.8中挂起。博客文章QString :: indexOf()与Qt 4.5相比,QString :: indexOf()修改了一个const QRegExp对象。 QString :: contains()内联QString :: indexOf(),因此它也是同样的问题。
If you're stuck with Qt4 and thus QRegExp, you could use
如果您坚持使用Qt4并因此使用QRegExp,那么您可以使用
int index = myQRegExp.indexIn(myQString);
bool okay = (myQRegExp.indexIn(myQString) != -1);
in your sources instead. Or patch the Qt Sources.
而是在你的来源。或修补Qt来源。