QT C++ QRegularExpression 多个匹配

bluesky3月前 ⋅ 38 阅读

ChatGPT的回答仅作参考:

在QRegularExpression中,可以使用`globalMatch()`函数来进行多个匹配。该函数返回一个`QRegularExpressionMatchIterator`对象,可以使用`next()`函数来逐个获取匹配结果。

以下是一个示例代码,演示如何使用`globalMatch()`函数进行多个匹配:

```cpp
QString text = "Hello, world! This is a test.";
QRegularExpression regex("\\b\\w+\\b");
QRegularExpressionMatchIterator i = regex.globalMatch(text);
while (i.hasNext()) {
    QRegularExpressionMatch match = i.next();
    QString word = match.captured(0);
    qDebug() << "Matched word:" << word;
}
```

在上面的代码中,我们使用正则表达式`\b\w+\b`来匹配单词。`globalMatch()`函数返回一个`QRegularExpressionMatchIterator`对象,我们使用`next()`函数来逐个获取匹配结果。对于每个匹配结果,我们使用`captured(0)`函数来获取完整的匹配字符串,并输出到控制台。

全部评论: 0

    相关推荐