我知道如何在特定字符串中查找正则表达式。如何找到第一个与正则表达式匹配的元素?
这是我的代码:
QString mangledText;
QRegExp rx("string");
while ((pos = rx.indexIn(mangledText)) != -1){
mangledText.replace(pos, rx.matchedLength(), "replaced string");
}
我想替换第一个匹配结果(或第二个或第三个),而不是全部。
有什么建议吗?
最佳答案
I want to replace first match result instead of all of that.
使用 if
而不是 while
。
if ((pos = rx.indexIn(mangledText)) != -1){
mangledText.replace(pos, rx.matchedLength(), "replaced string");
}
关于C++、正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33538298/