Results 1 to 5 of 5

Thread: I'd Like to match "(" ")" using QRegExp.

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.
    Qt Code:
    1. QRegExp regXp("\\b(PTY|pty|Pty|pTy|ptY|PTy|pTY|(PTY)|\\(Pty\\)|LTD|ltd|\\(LTD\\)|\\(ltd\\)|Ltd)\\b");
    To copy to clipboard, switch view to plain text mode 
    Last edited by ayanda83; 1st November 2016 at 10:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    ayanda83 (1st November 2016)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    ayanda83 (1st November 2016)

  6. #4
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I'd Like to match "(" ")" using QRegExp.

    Thank you for your replies. I am going to use
    Qt Code:
    1. Qt:CaseInsensitive
    To copy to clipboard, switch view to plain text mode 
    but I still don't understand how to escape the parentheses. When I did this
    Qt Code:
    1. \\(Pty\\)
    To copy to clipboard, switch view to plain text mode 
    in my regular expression, I thought I was escaping the parenthesis. I've also tried using one "\" and that doesn't work either.

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: I'd Like to match "(" ")" using QRegExp.

    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:

    Qt Code:
    1. QRegExp regXp("\\b(PTY|pty|Pty|pTy|ptY|PTy|pTY|(PTY)|\\(Pty\\)|LTD|ltd|\\(LTD\\)|\\(ltd\\)|Ltd)\\b");
    To copy to clipboard, switch view to plain text mode 

    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 "\".
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2015, 10:02
  2. QSqlError("", "Parameter count mismatch", "")
    By Alberto7 in forum Newbie
    Replies: 2
    Last Post: 9th October 2015, 22:09
  3. Replies: 3
    Last Post: 16th March 2015, 07:31
  4. QRegExp match all excpet "_"
    By mattia in forum Newbie
    Replies: 6
    Last Post: 28th March 2008, 12:53
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.