Results 1 to 3 of 3

Thread: How to match "(" in a regexp?

  1. #1
    Join Date
    Feb 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default How to match "(" in a regexp?

    I want to extract the "6" from "Foo (6)" into a variable.

    In for example TCL/TK I have used "\(([0-9].)\)" in such situations. But this doesn't work, the compiler complains about an unknown backslash escape sequence (obviously the "\(" and "\)").

    So how can I quote the round braces?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to match "(" in a regexp?

    You have to double escape the \ . It looks very nasty but it must be done:
    Qt Code:
    1. QString s = "(123) (333s) (2)";
    2. QRegExp rx("\\(([0-9]+)\\)");
    3. int pos = 0;
    4. while ((pos = rx.indexIn(s, pos)) != -1) {
    5. qWarning() << rx.cap(1);
    6. pos += rx.matchedLength();
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to match "(" in a regexp?

    Of course - I have to quote the "\" so that after the compiler has finished there's still a "\" there for the regexp. Thanks a lot

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2010, 21:46
  2. Replies: 3
    Last Post: 8th July 2008, 19:37
  3. QRegExp match all excpet "_"
    By mattia in forum Newbie
    Replies: 6
    Last Post: 28th March 2008, 12:53
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58

Tags for this Thread

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.