Results 1 to 3 of 3

Thread: QRegExp parsing matched quoted strings

  1. #1
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Question QRegExp parsing matched quoted strings

    I'm trying to use QRegExp to parse a string, where each token is either separated by a space or, if the token contains a space, is enclosed in double quotes. If the token contains a double quote, the double quote is escaped with \".

    so from this string
    Qt Code:
    1. test "a string" "string \" escaped" 1 2
    To copy to clipboard, switch view to plain text mode 
    I'd like to extract
    Qt Code:
    1. [test], [a string], [string " escaped], [1], [2]
    To copy to clipboard, switch view to plain text mode 
    I was previously using a technique stolen from this bloc post ( which apparently looks to be down right at this point ) and was using the following regex in some Flex code
    Qt Code:
    1. /((")(?:\\?.)*?\2) | \S+/gsx
    To copy to clipboard, switch view to plain text mode 
    Translating that directly to QT didn't work - a assume because it uses features not available in QRexExp. Here's my little test app
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QRegExp>
    3. #include <QStringList>
    4. #include <QDebug>
    5. void test(const QString& text, const QString& pattern)
    6. {
    7. qDebug() << "testing " << text << " against " << pattern;
    8. QRegExp rx(pattern);
    9. int pos = 0;
    10. while ((pos = rx.indexIn(text, pos)) != -1) {
    11. qDebug() << rx.capturedTexts();
    12. qDebug() << rx.cap(1);
    13. pos += rx.matchedLength();
    14. }
    15. }
    16.  
    17. int main(int argc, char *argv[])
    18. {
    19. test( "test \"a string\" \"string \\\" escaped\" 1 2", "([\"])(?:.)*([\"])");
    20. test( "test \"a string\" \"string \\\" escaped\" 1 2", "((\")(?:\\\\?.)*?\\2)");
    21. QCoreApplication a(argc, argv);
    22. return a.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 
    Any ideas on how to accomplish this? Or do I need to switch to using a character based parser?

  2. #2
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: QRegExp parsing matched quoted strings

    The following pattern works for me with the input you provide and a few more I tested:

    Qt Code:
    1. test(
    2. "test \"a string\" \"string \\\" escaped\" 1 2",
    3. "((?:[^\\s\"]+)|(?:\"(?:\\\\\"|[^\"])*\"))");
    To copy to clipboard, switch view to plain text mode 

    Output of qDebug() << rx.cap(1):

    testing "test "a string" "string \" escaped" 1 2" against "((?:[^\s"]+)|(?:"(?:\\"|[^"])*"))"
    "test"
    ""a string""
    ""string \" escaped""
    "1"
    "2"

  3. The following user says thank you to mattc for this useful post:

    jhndnn (27th January 2010)

  4. #3
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QRegExp parsing matched quoted strings

    Thanks, that work perfectly.

Similar Threads

  1. Unicode strings int Qt 4.2.1
    By mkrentovskiy in forum Qt Programming
    Replies: 12
    Last Post: 29th December 2011, 10:02
  2. Replies: 1
    Last Post: 21st September 2009, 08:30
  3. Linguist and static strings
    By Equilibrium in forum Qt Tools
    Replies: 7
    Last Post: 5th March 2008, 14:08
  4. Adding strings in QTreeWidget ?
    By npc in forum Newbie
    Replies: 13
    Last Post: 30th January 2007, 10:24
  5. Trouble parsing using simple QRegExp
    By johnny_sparx in forum Qt Programming
    Replies: 4
    Last Post: 24th February 2006, 01: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.