QRegularExpression
是否在其正则表达式引擎中提供先行断言?
我已经测试了这个示例,但没有匹配的字符串。
QString s = "px1 pt 2px 3em 4px";
QRegularExpression re("\\d(?=px)");
auto match = re.match(s);
qDebug()<< match.lastCapturedIndex();
结果是 0。
最佳答案
QRegularExpression
使用 PCRE 正则表达式,因此它支持向前查找、向后查找,甚至是惰性量词。在您的情况下,只有整个匹配值,没有捕获,因此使用 match.captured(0)
来访问该值。
使用
QRegularExpression re("\\d+(?=px)");
QRegularExpressionMatchIterator i = re.globalMatch("px1 pt 2px 3em 4px");
QStringList words;
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString word = match.captured(0);
words << word;
}
// words contains "2", "4"
仅当后跟 px
文字字符序列时,\d+(?=px)
模式才匹配 1+ 位数字。
关于c++ - QregularExpression 中的前瞻性断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41620665/