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?