正则表达式c++ Qt

Regular Expressions C++ Qt

本文关键字:Qt c++ 正则表达式      更新时间:2023-10-16
QRegExp rx("\btest\b");
rx.indexIn("this is a test string");
QString captured = rx.cap(1);
std::string capturedstr = captured.toUtf8().constData();
std::cout << capturedstr;

我希望上面打印出test并匹配字符串中的单词test,但它似乎没有这样做。有人能解释一下吗?使用QT。

您的正则表达式中没有任何捕获父级,因此没有捕获组1。试试这个:

QRegExp rx("\b(test)\b");

rx.cap(1)替换为rx.cap(0)