Reputation: 47
thanks for any help in advance. I'm quite new to qt so please excuse my newbie question.
I want to replace image1="jdjddsj" with "im" with a regular expression in a string. There are more of these partes with xx="..." in my string, so I wanted to run QRegExp greedy, which somehow doesn't seem to work.
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);
Result is unfortunately: python python.py im two!
qDebug() << str_commando.remove(QRegExp ("age1=\"([^>]*)\""));
I have tried it with before. Same result.
Where am I going wrong? Thanks for your help in advance!
SOLUTION:
qDebug() << str_commando.replace(QRegExp ("image1=\"([^\"]*)\""), "im");
Upvotes: 0
Views: 311
Reputation: 3296
The character set [^>]
includes "
. This mean the regex in
str_commando.remove(QRegExp ("age1=\"([^>]*)\""));
matches age1=\"sonstzweiteil\" one! path=\"sonstwas\" image2=\"sonsteinanderes\"
. If you are sure that there is no "
between the quotes, you could set the regex minimal instead of greedy. Another solution would be to set the set [^>]
to [^>"]
. I also doesn't know why you are forbidding >
.
After looking in the documentation of QString I think you could also do something like this:
str_commando.replace(QRegExp("image1=\"([^\"]|\\\")*\""), "im");
or if you want to have im="..."
:
str_commando.replace(QRegExp("image1=\"((?:[^\"]|\\\")*)\""), "im=\"\\1\"");
Upvotes: 2