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
test "a string" "string \" escaped" 1 2
test "a string" "string \" escaped" 1 2
To copy to clipboard, switch view to plain text mode
I'd like to extract
[test], [a string], [string " escaped], [1], [2]
[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
/((")(?:\\?.)*?\2) | \S+/gsx
/((")(?:\\?.)*?\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
#include <QtCore/QCoreApplication>
#include <QRegExp>
#include <QStringList>
#include <QDebug>
void test(const QString& text, const QString& pattern)
{
qDebug() << "testing " << text << " against " << pattern;
int pos = 0;
while ((pos = rx.indexIn(text, pos)) != -1) {
qDebug() << rx.capturedTexts();
qDebug() << rx.cap(1);
pos += rx.matchedLength();
}
}
int main(int argc, char *argv[])
{
test( "test \"a string\" \"string \\\" escaped\" 1 2", "([\"])(?:.)*([\"])");
test( "test \"a string\" \"string \\\" escaped\" 1 2", "((\")(?:\\\\?.)*?\\2)");
return a.exec();
}
#include <QtCore/QCoreApplication>
#include <QRegExp>
#include <QStringList>
#include <QDebug>
void test(const QString& text, const QString& pattern)
{
qDebug() << "testing " << text << " against " << pattern;
QRegExp rx(pattern);
int pos = 0;
while ((pos = rx.indexIn(text, pos)) != -1) {
qDebug() << rx.capturedTexts();
qDebug() << rx.cap(1);
pos += rx.matchedLength();
}
}
int main(int argc, char *argv[])
{
test( "test \"a string\" \"string \\\" escaped\" 1 2", "([\"])(?:.)*([\"])");
test( "test \"a string\" \"string \\\" escaped\" 1 2", "((\")(?:\\\\?.)*?\\2)");
QCoreApplication a(argc, argv);
return a.exec();
}
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?
Bookmarks