Question

i am workin on a terminal program to execute applications on remote machines. you can pass a command like in the windows cmd.exe like:

"C:\random Directory\datApplication.py" "validate" -r /c "C:\anotherDirectory"

to make that possible i have to deal with quoted text and parse the command and its arguments from that string. in notepad++ i found a RegExp to patch them (([^" \t\n]+)|("[^"]*"))+ and it works. in Qt4.8.1 i tried:

static const QRegExp re("(([^\" \\t\\n]+)|(\"[^\"]*\"))+");
re.matchExact(str); // str is something like shown above
qDebug() << re.capturedTexts();

and this code only prints me 3 times the "C:\random Directory\datApplication.py" and nothing more. it should print out every argument entered as a single object ...

what can i do to make it working?

SOLUTION: (thanks to Lindrian)

const QString testText = "\"C:\\random Directory\\datApplication.py\" \"validate\" -r /c \"C:\\anotherDirectory\"";
static const QRegExp re("([^\" \\t\\n]+|\"[^\"]*\")+");
int pos = 0;
while ((pos = re.indexIn(testText)) != -1) //-i indicates that nothing is found
{
    const int len = re.matchedLength();
    qDebug() << testText.mid(pos,len);
    pos += len;
}
Was it helpful?

Solution

FTFY: ([^" \t\n]+|"[^"]*")

(You were just overusing backrefs)

Make sure you're capturing all results.

Demo: http://regex101.com/r/pR8oF5

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top