Results 1 to 12 of 12

Thread: Does QRegExp::lastIndexIn work correct?

  1. #1
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    7
    Qt products
    Qt4

    Default Does QRegExp::lastIndexIn work correct?

    Qt Code:
    1. #include <QCoreApplication>
    2.  
    3. #include <QRegExp>
    4. #include <QDebug>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10.  
    11. QRegExp reg("(.*)");
    12.  
    13. QString str = "_1234_4567_";
    14.  
    15. int pos1 = reg.indexIn(str, 5);
    16. qDebug() << "F" << pos1 << reg.cap();
    17.  
    18. int pos2 = reg.lastIndexIn(str, 5);
    19. qDebug() << "B" << pos2 << reg.cap();
    20.  
    21. return a.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

    Generates the following output:

    Qt Code:
    1. F 5 "_4567_"
    2. B 5 "_4567_"
    To copy to clipboard, switch view to plain text mode 

    But I think output of reg.lastIndexIn(str, 5) must be like this:

    Qt Code:
    1. B 0 "_1234_"
    To copy to clipboard, switch view to plain text mode 

    Hm....
    Last edited by Nebelig; 20th June 2011 at 10:52.

  2. #2
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Does QRegExp::lastIndexIn work correct?

    It is totally correct, since the search in the string starts from after 5th character.

  3. #3
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    7
    Qt products
    Qt4

    Default Re: Does QRegExp::lastIndexIn work correct?

    Quote Originally Posted by Rachol View Post
    It is totally correct, since the search in the string starts from after 5th character.
    Thanks.
    But, lastIndexIn() not find a match backwards from offset?

  4. #4
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Does QRegExp::lastIndexIn work correct?

    Unfortunately, not.

    Edit. Sorry I was thinking about indexIn...

    So, yes it is searching backwards, but it doesn't disqualify the characters after the offset. So in your case the regular expresion is matching the string starting from offset = 5, and that's what is returned.
    Last edited by Rachol; 20th June 2011 at 11:10.

  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Does QRegExp::lastIndexIn work correct?

    Qt Code:
    1. reg.lastIndexIn(str, -6);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Does QRegExp::lastIndexIn work correct?

    Quote Originally Posted by MarekR22 View Post
    Qt Code:
    1. reg.lastIndexIn(str, -6);
    To copy to clipboard, switch view to plain text mode 
    It is equal to
    Qt Code:
    1. reg.lastIndexIn(str, 5);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    7
    Qt products
    Qt4

    Default Re: Does QRegExp::lastIndexIn work correct?

    OK. Any suggestion about backward capturing using QRegExp? In my example I want get "_1234_" from position 5.

  8. #8
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Does QRegExp::lastIndexIn work correct?

    What exactly do you want to capture?

  9. #9
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Does QRegExp::lastIndexIn work correct?

    Exactly explain what you are try to do (not how you are trying to achieve that).
    I have gut feeling that you just need something like that (especially that your regular excretion is just a wild card):
    Qt Code:
    1. QStringList items = str.split("_", QString::SkipEmptyParts);
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    7
    Qt products
    Qt4

    Default Re: Does QRegExp::lastIndexIn work correct?

    An example, a cursor in texteditor is inside the keyword:

    Qt Code:
    1. blablabla get-deffunct|ion-list blalblabla (| <- cursor)
    To copy to clipboard, switch view to plain text mode 

    and I want detect is the word correct (get-deffunction-list) or not, if it is correct I need capture it.

    It was a good idea for use reg expression for capturing left part and if it was correct get right part of word.

    Searching word boundary (space, tabs etc) not a good idea, becouse:

    Qt Code:
    1. bla blablabla>=get-deffunction-list!=blalblabla bla
    To copy to clipboard, switch view to plain text mode 

    is correct to.
    Last edited by Nebelig; 20th June 2011 at 14:25.

  11. #11
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Does QRegExp::lastIndexIn work correct?

    1. if you have editor then you should use QTextDocument::find-4
    2. in regular expression there is assertion "\b" which means word boundary (you don't have to use space or other character to detect that), see QRegExp documentation.
    example: QRegExp("\\b\\w+\\b");
    3. check qt examples there is something similar to your problem.

  12. #12
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    7
    Qt products
    Qt4

    Default Re: Does QRegExp::lastIndexIn work correct?

    MarekR22, thanks for the advice.

Similar Threads

  1. Is this sentence correct?
    By SWEngineer in forum Newbie
    Replies: 6
    Last Post: 21st June 2011, 01:56
  2. Replies: 1
    Last Post: 10th March 2010, 18:19
  3. QRegExp doesn't seem to work as it should
    By thomaspu in forum Qt Programming
    Replies: 3
    Last Post: 21st December 2007, 07:49
  4. how to integrate qt into kdevelop correct way
    By amit_pansuria in forum KDE Forum
    Replies: 1
    Last Post: 25th November 2006, 08:58
  5. QSetting value() not correct
    By bpetty in forum Newbie
    Replies: 1
    Last Post: 14th August 2006, 19:58

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.