PDA

View Full Version : I'd Like to match "(" ")" using QRegExp.



ayanda83
1st November 2016, 09:59
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.
QRegExp regXp("\\b(PTY|pty|Pty|pTy|ptY|PTy|pTY|(PTY)|\\(Pty\\)|LT D|ltd|\\(LTD\\)|\\(ltd\\)|Ltd)\\b");

anda_skoa
1st November 2016, 11:26
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,
_

d_stranz
1st November 2016, 14:29
"(" 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.

ayanda83
1st November 2016, 16:03
Thank you for your replies. I am going to use
Qt:CaseInsensitive but I still don't understand how to escape the parentheses. When I did this
\\(Pty\\) in my regular expression, I thought I was escaping the parenthesis. I've also tried using one "\" and that doesn't work either.

d_stranz
1st November 2016, 17:25
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:


QRegExp regXp("\\b(PTY|pty|Pty|pTy|ptY|PTy|pTY|(PTY)|\\(Pty\\)|LT D|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. (http://perldoc.perl.org/perlre.html) Remember that in a C/C++ string, you need to put "\\" to escape a character. Perl needs only a single "\".