Results 1 to 4 of 4

Thread: QRegExp lastIndexOf always minimal?

  1. #1
    Join Date
    Nov 2006
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QRegExp lastIndexOf always minimal?

    Hi All,

    I think this may be a but, but perhaps it's something I've missed.

    It seems that when you use lastIndexOf it won't be greedy. I think it's because it's the way it works - checking each time from the an index working it's way backwards then when it hits a match it stops.

    So say I've got a filename:
    coolfilename02.153.ext
    I want to split this into
    "coolfilename02." , "153", ".ext"
    So I used the expression ([0-9]+)(\..*)
    running from the last index of the expression [0-9]+\.

    Problem is as soon as the last expression hits 3.ext it returns as successful - whereas to be greedy it should keep going until it hits 153.ext

    Any suggestions for a clean way to fix it or should I just dig into the implementation of QRegExp and submit it upstream? I know I could just implement the backward searching myself, but doing it such an ugly way seems like a bad choice compared with fixing it in Qt. (Assuming it's broken and I haven't just missed something).

    Cheers,

    Alan.

  2. #2
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QRegExp lastIndexOf always minimal?

    You could search for last index of \\.[0-9]+\\. and add 1 to the result:
    Qt Code:
    1. #include <QString>
    2. #include <QRegExp>
    3. #include <QtDebug>
    4.  
    5. int main() {
    6. QString filename = "terefere.123.ext";
    7. qDebug() << ( filename.lastIndexOf(QRegExp("[0-9]+\\.")) );
    8. qDebug() << ( filename.lastIndexOf(QRegExp("\\.[0-9]+\\.")) + 1 );
    9. }
    To copy to clipboard, switch view to plain text mode 
    As a result you get:

    danadam@lappy]$ ./test
    11
    9
    The Wheel weaves as the Wheel wills.

  3. #3
    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 lastIndexOf always minimal?

    Hmm... this indeed is strange but I can believe the behaviour is intentional.

    You may obtain the result you want using this code:
    Qt Code:
    1. #include <QString>
    2. #include <QRegExp>
    3.  
    4. int main(int argc, char **argv){
    5. QString string = "coolfilename02.153.ext";
    6. QRegExp rx("([0-9]+)(\\.[^\\.]*)$");
    7. rx.setMinimal(true);
    8. rx.indexIn(string);
    9. qDebug(qPrintable(QString("0: %1 1: %2 2: %3").arg(rx.cap(0)).arg(rx.cap(1)).arg(rx.cap(2))));
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2006
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QRegExp lastIndexOf always minimal?

    Thanks Guys - seems I've got a whole bunch of solutions now :-)

    It also hit me last night while trying to fall asleep (why does code not working do that to me?)

    [^0-9][0-9]+\.

    Thanks again,

    Alan.

Similar Threads

  1. QRegExp progblem
    By high_flyer in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2006, 12:12
  2. QRegExp?
    By Marco812 in forum Qt Programming
    Replies: 3
    Last Post: 4th August 2006, 08:31
  3. need help for my QRegExp
    By patcito in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2006, 16:29
  4. Trouble parsing using simple QRegExp
    By johnny_sparx in forum Qt Programming
    Replies: 4
    Last Post: 24th February 2006, 00:42

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.