Matching a regular expression in string.
-
I am trying something like below,
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);QString str = "((Hello1 and Hello2) in Hello.World)";
QString matchWord = "Hello;
qDebug() << str.contains(QRegExp("\\b" + matchWord + "\\b"));
return a.exec();
}I am getting output as true. I want to what's wrong??
My intention is to match only a complete word, like "Hello".
Can anyone help me with this?
-
I think the engine matches the "Hello.World", treating the dot (.) as a word boundary.
You can check where the match happened using QRegExp API to be sure.
BTW. It is recommended to use QRegularExpression in Qt5 - QRegExp is just a leftover from Qt4 times.
-
Sure, they are in the docs: http://doc.qt.io/qt-5/qregexp.html#code-examples
indexIn() method should interest you in this case.