Results 1 to 7 of 7

Thread: QRegExp: get the parsed string

  1. #1
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QRegExp: get the parsed string

    I have for example this string:
    "DLH3029 (LUFTHANSA THREE ZERO TWO NINER)"

    that should become:
    'LUFTHANSA THREE ZERO TWO NINER'

    so I use this regex:
    Qt Code:
    1. echo "DLH3029 (LUFTHANSA THREE ZERO TWO NINER)" | sed "s/[^(]*(\([^)]*\)).*/'\1'/"
    To copy to clipboard, switch view to plain text mode 

    How can now use it in a Qt program ?

    I've tried but I cannot find a way to make it work.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QRegExp: get the parsed string


  3. #3
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QRegExp: get the parsed string

    Thanks but:

    Qt Code:
    1. QRegExp rx("s/[^(]*(\([^)]*\)).*/'\1'/");
    2. rx.indexIn("DLH3029 (LUFTHANSA THREE ZERO TWO NINER)");
    3. QStringList list = rx.capturedTexts();
    4. qDebug() << "list: " << list;
    5. ::exit(1);
    To copy to clipboard, switch view to plain text mode 

    result:
    list: ("", "", "")
    What am I doing wrong ?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QRegExp: get the parsed string

    Ehm s/[^(]*(\([^)]*\)).*/'\1'/ are two "commands". One for searching and one for replacing. For Qt you have to split them. QRegExp expects the search part. And also note that they are not 100% Perl compatible, but that is all in the docs... (Qt5 supports 100% Perl compatible expressions, if you want to use the newly published Alpha release.)

  5. The following user says thank you to Lykurg for this useful post:

    trallallero (4th April 2012)

  6. #5
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QRegExp: get the parsed string

    Ah ok. The regex is a part of a bigger one so I have to split it further.
    My company is thinking about upgrading to Qt5 so this is a good reason to do it now.

    Thanks a lot Lykurg.

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

    Default Re: QRegExp: get the parsed string

    Upgrading a commercial development project to alpha software is probably not the best idea. Nothing in your requirement needs the Perl regex compatibility changes AFAICT.

    Also, be careful to escape backslashes in the string literals.

  8. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QRegExp: get the parsed string

    I don't have the URL's right now, but there was a test that the QRegExp class is really slow. That was also the reason for changing this class in Qt5. So even if you do not need the additional syntax a change to Qt5 (when ready) can pay off, if your application heavily rely on regular expressions.

    EDIT: That was not the URL I was thinking of but anyway: http://qt-project.org/wiki/Regexp_engine_in_Qt5
    Last edited by Lykurg; 5th April 2012 at 06:23.

Similar Threads

  1. Replies: 3
    Last Post: 8th June 2011, 06:36
  2. QRegExp for extracting the string between two HTML tags...
    By tuthmosis in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2010, 06:55
  3. std:string how to change into system:string?
    By yunpeng880 in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2009, 08:51
  4. qregexp to check string
    By mattia in forum Newbie
    Replies: 3
    Last Post: 19th February 2008, 14:13
  5. Int to String - manipulating string
    By mickey in forum General Programming
    Replies: 6
    Last Post: 5th November 2007, 20:11

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.