Results 1 to 3 of 3

Thread: A problem with RegExp

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default A problem with RegExp

    Hi,

    I have the string: "Valencia 7x17"

    I want to see:

    0
    "Valencia 7x17"
    "Valencia"
    "7"
    "17"
    But I see:

    0
    "Valencia 7x17"
    "Valencia"
    "17"
    ""
    main.cpp
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QRegExp>
    3. #include <QtDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. // ([\w]+) ([\d])x([\d]+)\/([\d]x[\d]+) ([A-Z]([\d.]+)) .+ ([\w]+)
    10. // Valencia 7x17/5x112 D70.1 ET48 Silver
    11.  
    12. QRegExp reCap("^([\\w]+)\\s([\\d])x([\\d]+)$");
    13. qDebug() << reCap.indexIn("Valencia 7x17");
    14. qDebug() << reCap.cap(0);
    15. qDebug() << reCap.cap(1);
    16. qDebug() << reCap.cap(3);
    17. qDebug() << reCap.cap(4);
    18.  
    19. return a.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 

    Thank you!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: A problem with RegExp

    Why would you expect anything else? There are only three capture groups in the regular expression and you are asking for groups 1,3, and 4!
    Qt Code:
    1. QRegExp reCap("^([\\w]+)\\s([\\d])x([\\d]+)$");
    2. qDebug() << reCap.indexIn("Valencia 7x17");
    3. qDebug() << reCap.cap(0);
    4. qDebug() << reCap.cap(1);
    5. qDebug() << reCap.cap(2);
    6. qDebug() << reCap.cap(3);
    To copy to clipboard, switch view to plain text mode 

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

    8Observer8 (16th August 2013)

  4. #3
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: A problem with RegExp

    Thank you very much

Similar Threads

  1. Negative regexp
    By mero in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2013, 21:59
  2. SQLITE and REGEXP
    By miraks in forum Qt Programming
    Replies: 3
    Last Post: 16th March 2011, 21:55
  3. RegExp Question
    By slava in forum Qt Programming
    Replies: 3
    Last Post: 12th July 2010, 19:33
  4. RegExp
    By pucara_faa in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2009, 13:41
  5. RegExp question
    By high_flyer in forum General Programming
    Replies: 1
    Last Post: 26th August 2007, 18:23

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
  •  
Qt is a trademark of The Qt Company.