Sign in to follow this  

QRegExp doubt !

Recommended Posts

QRegExp rxp;

case1 :: rxp.setPattern(val);
case2 :: rxp.setPattern("^" + val + "$");

suppose val is [cc dd]

now if my input string to be matched is dd , it matches in case1 but not in case2 ..

can someone tell why >>

thanks in advance

Share this post

Link to post
Share on other sites
I don't know QRegExp, but I will assume it follows the same regex rules as Perl (or close to it). Apologies if it what I say is not correct.

Anyway, when you use brackets '[]', it only matches a single literal character in the brace. So val = [cc dd] means, "match a 'c', a 'd', or a space".

This works in the first case, because it says "oh, I see a single d, I match".

In the second case, you're saying "there must be one and only one character between the start and end, either c, d, or space, which isn't true for "dd".

If you want to match "cc" or "dd", you want something like this: val = (cc|dd)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this