删除字符串贪婪正则表达式在QT
Remove string with greedy regular expression in QT
事先感谢您的帮助。我是一个新手,所以请原谅我的新手问题。
我想用字符串中的正则表达式"im"替换image1="jdjddsj"。在我的字符串中有更多这样的xx="…",所以我想运行QRegExp greedy,但不知何故似乎不起作用。
QString str_commando;
str_commando = "python python.py image1="sonstzweiteil" one! path="sonstwas" image2="sonsteinanderes" two!"
QString str(str_commando); // The initial string.
qDebug() << str_commando.remove(QRegExp ("age1="([^>]*)""));
/* Set field */
ui->lineeCommand->setText(str_commando);
结果是:pythonpythonpy,我两!
qDebug() << str_commando.remove(QRegExp ("age1="([^>]*)""));
我以前试过。相同的结果。
我哪里错了?提前感谢您的帮助!
解决方案:
qDebug() << str_commando.replace(QRegExp ("image1="([^"]*)""), "im");
字符集[^>]
包含"
。这意味着
str_commando.remove(QRegExp ("age1="([^>]*)""));
匹配age1="sonstzweiteil" one! path="sonstwas" image2="sonsteinanderes"
。如果您确定引号之间没有"
,则可以设置正则表达式minimal而不是greedy。另一个解决方案是将set [^>]
设置为[^>"]
。我也不知道你为什么要禁止>
。
在查看QString的文档后,我认为你也可以这样做:
str_commando.replace(QRegExp("image1="([^"]|\")*""), "im");
或者如果你想要im="..."
:
str_commando.replace(QRegExp("image1="((?:[^"]|\")*)""), "im="\1"");
相关文章:
- Qt - 在没有正则表达式的情况下,将 QString 拆分为几种类型的空格
- 如何在 QT 框架工作C++中使用变量作为正则表达式模式
- 带有Qt的正则表达式捕获一些文本
- 正则表达式传递正则表达式测试器,但在 qt 中失败
- Qt 中的不匹配数字正则表达式
- QT和正则表达式
- 在Qt Creator中使用正则表达式在多个文件中查找和替换
- Qt中的正则表达式
- Boost::qt Creator中的正则表达式
- 使用 c++ 或 Qt 将 SQL LIKE 表达式转换为正则表达式
- Qt验证器正则表达式
- 带有 Qt 的正则表达式 - indexIn(const QString &) 无法按预期工作
- Qt-检查正则表达式宽度
- 根据QT(C++)中SQLite数据库中的正则表达式检索记录
- 比较qt中的正则表达式
- 无效的正则表达式在Qt但在正则表达式教练这个相同的正则表达式是有效的
- Qt正则表达式不匹配
- Qt正则表达式增强正则表达式转换从字符串内部取数
- 我如何写一个Qt正则表达式来突出显示字符串开始和结束与非单词字符
- 如何在Qt正则表达式中匹配连字符