Results 1 to 7 of 7

Thread: Search for QRegExp in a QString

  1. #1
    Join Date
    Jun 2008
    Posts
    22

    Default Search for QRegExp in a QString

    I have a tabbed interface in my application. I am getting the contents of the focused tab in a string variable as
    QString myString = currentTab->getTextEdit()->toPlainText();

    Now, I want to search this string for the occurrence of some other strings which I have defined as a regular expression and for each occurrence, I want to prepend a character to the occurrence. Please let me know how to find every occurrence of regular expression in myString.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Search for QRegExp in a QString


  3. #3
    Join Date
    Jun 2008
    Posts
    22

    Default Re: Search for QRegExp in a QString

    Using indexOf() I am able to prepend the required character only to the first occurrence of the RegExp in myString. How can I do the same for all the occurrences??

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Search for QRegExp in a QString

    Quote Originally Posted by Abc View Post
    How can I do the same for all the occurrences??
    Invoke indexOf() once more, but this time use the second parameter and return value to skip the previous occurence.

  5. #5
    Join Date
    Jun 2008
    Posts
    22

    Default Re: Search for QRegExp in a QString

    This does not work if there are multiple occurrences of the same RegExp and it is not even known how amny times a particular RegExp would occur. How can I find multiple occurrences and make the required change??

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Search for QRegExp in a QString

    did you see this example?
    Qt Code:
    1. QRegExp rx("(\\d+)");
    2. QString str = "Offsets: 12 14 99 231 7";
    3. int pos = 0;
    4.  
    5. while ((pos = rx.indexIn(str, pos)) != -1) {
    6. list << rx.cap(1);
    7. pos += rx.matchedLength();
    8. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jun 2008
    Posts
    22

    Default Re: Search for QRegExp in a QString

    Thanks works now!!

Similar Threads

  1. Qt 4.4.0 Help Module : Search not working
    By Ankitha Varsha in forum Qt Programming
    Replies: 0
    Last Post: 22nd May 2008, 09:45
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 18:59
  3. Replies: 2
    Last Post: 12th October 2006, 10:17
  4. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 23:10
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 21:41

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.