I'd Like to match "(" ")" using QRegExp.
I am trying to remove the occurance of a substring (PTY) from my string but the program only matches the "PTY" and leaves out the parenthesis. please see my QRegExp instance below.
Code:
QRegExp regXp
("\\b(PTY|pty|Pty|pTy|ptY|PTy|pTY|(PTY)|\\(Pty\\)|LTD|ltd|\\(LTD\\)|\\(ltd\\)|Ltd)\\b");
Re: I'd Like to match "(" ")" using QRegExp.
Instead of all the case variations you could just construct the object with Qt::CaseInsensitive.
Should reduce it to two cases, with and without parentheses.
You can then more easily experiement, e.g. order of patterns.
Cheers,
_
Re: I'd Like to match "(" ")" using QRegExp.
"(" and ")" are special characters in regular expressions. If you want to match them, then you have to escape them in your QRegExp string, the same way you have escaped "\\b".
Your regular expression has lots of mistakes, including parentheses that are escaped mixed up with parentheses that are not. The only things that will match correctly as far as I can see are "PTY" (and the different case variations you list in the first part of the reg exp, excluding "(PTY)"), "(Pty)", and these variations on "LTD": LTD|ltd|\\(LTD\\)|\\(ltd\\)|Ltd.
As anda_skoa suggested, use Qt::CaseInsensitive when you construct the QRegExp, and then you only need to look for "(pty)" and "(ltd)".
Note also that your matching will fail if either (PTY) or (LTD) is followed by a ".", ",", or any other character except a blank.
Re: I'd Like to match "(" ")" using QRegExp.
Thank you for your replies. I am going to use but I still don't understand how to escape the parentheses. When I did this in my regular expression, I thought I was escaping the parenthesis. I've also tried using one "\" and that doesn't work either.
Re: I'd Like to match "(" ")" using QRegExp.
Quote:
in my regular expression, I thought I was escaping the parenthesis.
Yes, that should be what you need to do. But your original reg exp:
Code:
QRegExp regXp
("\\b(PTY|pty|Pty|pTy|ptY|PTy|pTY|(PTY)|\\(Pty\\)|LTD|ltd|\\(LTD\\)|\\(ltd\\)|Ltd)\\b");
starts out by saying, "match one blank followed by one of "PTY" or "pty" or "Pty", ... and ending in a blank". The only place where you actually tell it to match parens is for the case "\\(Pty\\)" and no other variant on the case of the characters. And all of these cases will fail if the string you are trying to match to " (Pty) " does not start -and- end with a blank.
The QRegExp syntax is mostly the same as the regular expression syntax used in Perl. Read up on it here. Remember that in a C/C++ string, you need to put "\\" to escape a character. Perl needs only a single "\".