2013-10-14 91 views
2

我正在研究終端程序以在遠程機器上執行應用程序。 你可以通過如下命令在Windows CMD.EXE的,如:RegExp查找命令行參數

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

來做到這一點我必須處理引用文本和解析命令,並從該字符串它的參數。 in notepad ++我找到了一個RegExp來修補它們(([^" \t\n]+)|("[^"]*"))+,它的工作原理。在Qt4.8.1我想:

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

這個代碼只打印了我3次"C:\random Directory\datApplication.py",僅此而已。 它應該輸出作爲單個對象輸入的每個參數...

我該怎麼做才能使它工作?

SOLUTION:(感謝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; 
} 

回答

3

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

(你只是過度使用backrefs)

確保您拍攝的所有結果。

演示:http://regex101.com/r/pR8oF5

+0

感謝您的快速回復。必須改變一些qt代碼以及'找到'正確的匹配。 真棒鏈接順便說一句,有助於瞭解很多(和作爲一個血腥的初學者正則表達式我可以使用它);) – Zaiborg