谢谢你提前提供帮助。我对qt很陌生,所以请原谅我的新手问题。
我想用字符串中的正则表达式将image1="jdjddsj“替换为"im”。还有更多的xx=“.”的派对在我的字符串中,所以我想运行QRegExp贪婪,这似乎是不工作的。
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);结果不幸的是: python 2!
qDebug() << str_commando.remove(QRegExp ("age1=\"([^>]*)\""));我以前试过。同样的结果。
我哪里出问题了?谢谢你提前帮忙!
解决方案:
qDebug() << str_commando.replace(QRegExp ("image1=\"([^\"]*)\""), "im");发布于 2015-10-08 19:41:48
字符集[^>]包括"。这意味着
str_commando.remove(QRegExp ("age1=\"([^>]*)\""));匹配age1=\"sonstzweiteil\" one! path=\"sonstwas\" image2=\"sonsteinanderes\"。如果您确信引号之间没有",则可以将正则表达式设置为最小值而不是贪婪值。另一个解决方案是将set [^>]设置为[^>"]。我也不知道你为什么禁止>。
在查看了QString的文档之后,我认为您也可以这样做:
str_commando.replace(QRegExp("image1=\"([^\"]|\\\")*\""), "im");或者如果你想要im="..."
str_commando.replace(QRegExp("image1=\"((?:[^\"]|\\\")*)\""), "im=\"\\1\"");https://stackoverflow.com/questions/33024005
复制相似问题