C++、正则表达式

标签 c++ regex qt

我知道如何在特定字符串中查找正则表达式。如何找到第一个与正则表达式匹配的元素?

这是我的代码:

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/

相关文章:

c++ - 这是在做什么 : "input >> 4 & 0x0F"?

javascript - 非专辑字符中包含的字符串的正则表达式单词边界

c++ - 如何在存储速度非常慢的系统上处理大型 Qt 资源文件?

c++ - 从 QGraphicsItem Qt C++ 获取实际对象

c++ - GetModuleFilename 返回通过 VBScript 运行的错误文件夹名称

c++ - 容器的 const_casting 元素类型

c++ - 线程指针的 vector ,push_back导致崩溃

php - 如何用正则表达式匹配所有内容?

c# - 将网页转换为纯文本..?

c++ - 查询 Qt 资源集合 (.qrc) 中的资源数量