0

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");
1
  • Simply use "age1=\"([^>\"]*)\"". If you'd like a nongreedy pattern, you may try "age1=\"([^>]*?)\"" But I don't know if QT supports that. Commented Oct 8, 2015 at 19:36

1 Answer 1

2

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\"");
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a ton! you solved it for me! qDebug() << str_commando.replace(QRegExp ("image1=\"([^\"]*)\""), "im"); is what I'm using succesfully. I shouldn't just copy'n' paste regular expressions from old projects :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.