August 13, 2011

Ruzik Ruzik
Lab Rat
293 posts

[Solved]Wrong QRegExp

 

Hellow, i have this code:

  1.  QString str = "asin(";
  2.  str.replace(QRegExp("[^a]{0,1}sin\\("),"yes!");
  3.  qDebug() << str;

And i get “yes!”, why is it happened?
In advance thank for your help!

6 replies

August 13, 2011

loladiro loladiro
Lab Rat
596 posts

Your reg exp is saying match everything where there is everything except for an a one or zero times before the sin.

August 13, 2011

Ruzik Ruzik
Lab Rat
293 posts

How can i changed it, on the this site http://gskinner.com/RegExr/ all worked ok, why it is hapanned?

August 13, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

You should have “ayes!” based on this regexp. Are you sure you have “yes!” only?

August 13, 2011

Ruzik Ruzik
Lab Rat
293 posts

Sorry, but i do not fully understand what you said, this is full code whitch i used

  1. QString text = lineEdit->text();
  2. // text, for example  = "asin", and after replace it should be = "asin"
  3. text.replace(QRegExp("[^a]{0,1}sin\\("),"Math.sin(");

August 13, 2011

loladiro loladiro
Lab Rat
596 posts
Ruzik wrote:
How can i changed it, on the this site http://gskinner.com/RegExr/ all worked ok, why it is hapanned?

No it didn’t [regexr.com]
Just remove the {0,1} to make it work. However this will also select the character before the sin(. May I suggest something like \bsin\( or usage of subexpressions.

August 13, 2011

Ruzik Ruzik
Lab Rat
293 posts

Ok, thank you for help!

 
  ‹‹ [SOLVED] calling a function outside of the dialog class      QFileSystemWatcher not working with /etc/mtab ››

You must log in to post a reply. Not a member yet? Register here!