Results 1 to 4 of 4

Thread: QRegExp doesn't seem to work as it should

  1. #1
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QRegExp doesn't seem to work as it should

    I gotta be doing something stupid here. Can anyone lend another pair of eyes?

    QString crap = "[01;32msshtest@router[01;34m / $[00m";
    Given the above text, I wish to parse out the numbers right after the initial '['
    In other words, I want to match this list:
    01;
    32m
    01;
    34m
    00m

    Here's the code:
    Qt Code:
    1. QRegExp regEx( "(([0-9]+)[;|m])+");
    2. int pos = 0;
    3. QStringList matches;
    4. while ( (pos = regEx.indexIn( crap, pos)) != -1)
    5. {
    6. matches << regEx.cap(1);
    7. printf( "Matched: %s\n", regEx.cap(1).toLatin1().constData() );
    8. pos += regEx.matchedLength() -1;
    9. }
    To copy to clipboard, switch view to plain text mode 
    This is what I get when its run:
    Matched: 32m
    Matched: 34m
    Matched: 00m

    Umm.... why in the world is it not matching the stuff with ';'s ?

    Paul

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QRegExp doesn't seem to work as it should

    Your regexp says : Match at least sequence of at least one digit followed by "m", "|" or ";". What you probably want is "match a number of digits followed by either "m" or ";". To me the expression should be "\d+(m|;)" and you should iterate over the string until you can't match the expression.

  3. #3
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QRegExp doesn't seem to work as it should

    Yeah, looks like me regex was off. Using ([0-9]+)[;m]{1,1} works.

    Thanks,
    Paul

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QRegExp doesn't seem to work as it should

    "{1,1}" - this part is not needed.

Similar Threads

  1. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 01:04
  2. Change work area OS
    By pakulo in forum Qt Programming
    Replies: 15
    Last Post: 15th May 2007, 07:20
  3. Work offline and Archive web page in Konqueror
    By jamadagni in forum KDE Forum
    Replies: 0
    Last Post: 22nd February 2006, 09:18
  4. QTextEdit Justify align making work
    By dec0ding in forum Qt Programming
    Replies: 2
    Last Post: 13th January 2006, 12:02

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.