Results 1 to 6 of 6

Thread: Recover a QString with QRegExp

  1. #1
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Recover a QString with QRegExp

    I want to recover a QString between brakets.

    For example i have "Cos([Temperature1]*25)+58" and i want recover just "[Temperature1]*25" ,

    can i do it with a QRegExp or is there a best way?

    Thanks!

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

    Default Re: Recover a QString with QRegExp

    Yes, you can do it with a regular expression.

    The following regexp should match your case:
    Qt Code:
    1. QRegExp(".*\(([^)]*\).*");
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Recover a QString with QRegExp

    i tried it but it doesn't work

    i tried this :
    Qt Code:
    1. QString formule = "Cos([Temperature1]*25)+58" ;
    2. QStringList listMul;
    3. QRegExp regExp ("(\[(.*?)\])");
    4. listMul = formule.split(regExp);
    To copy to clipboard, switch view to plain text mode 

    and i find in the list Cos, [Temperature1] , 25 and +58 but when i changed * by + i find Cos, [Temperature1]+25 and +58.

    I want the same thing with * : Cos, [Temperature1]*25 and +58.
    Last edited by hassinoss; 26th February 2014 at 18:37.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,418
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Recover a QString with QRegExp

    You can also use indexOf() to search for the opening parenthese, then indexOf with that start index to search for the closing parentheses and then use QString::mid() to get the part that is of interest to you.

    Cheers,
    _

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

    Default Re: Recover a QString with QRegExp

    Quote Originally Posted by hassinoss View Post
    i tried it but it doesn't work
    Wysota's suggestion is not quite right but it really doesn't matter because what you asked for was not what you wanted anyway.

    You actually wanted to split the expression into tokens. This a non-trivial exercise in general cases, but for your specific case this works:
    Qt Code:
    1. QString formule = "Cos([Temperature1]*25)+58" ;
    2.  
    3. // The incredibly simple
    4. QStringList list = formule.split(QRegExp("[()]"));
    5. qDebug() << list;
    6.  
    7. // or the more involved but more flexible
    8. list.clear();
    9. const QRegExp re("[^()]+");
    10. int pos = 0;
    11. while ((pos = re.indexIn(formule, pos)) != -1) {
    12. list << re.cap(0);
    13. pos += re.matchedLength();
    14. };
    15. qDebug() << list;
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Recover a QString with QRegExp

    Thanx for your replies , they were very useful

Similar Threads

  1. Replies: 7
    Last Post: 3rd March 2011, 15:32
  2. Recover .ui file
    By Macok in forum Qt Programming
    Replies: 2
    Last Post: 28th February 2009, 15:41
  3. Search for QRegExp in a QString
    By Abc in forum Qt Programming
    Replies: 6
    Last Post: 13th August 2008, 10:31
  4. QString manipulation - QRegExp
    By mattia in forum Newbie
    Replies: 1
    Last Post: 18th March 2008, 12:21
  5. QString::replace() with QRegExp capture modification
    By Lykurg in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2008, 10:50

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.