2

私は、リモート マシンでアプリケーションを実行するターミナル プログラムに取り組んでいます。Windows cmd.exe のようなコマンドを次のように渡すことができます。

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

それを可能にするために、引用されたテキストを処理し、その文字列からコマンドとその引数を解析する必要があります。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"、それ以上は何も出力しません。入力されたすべての引数を単一のオブジェクトとして出力する必要があります...

それを機能させるために何ができますか?

解決策: (リンドリアンに感謝)

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;
}
4

1 に答える 1

3

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

(あなたは逆参照を使いすぎていました)

すべての結果を取得していることを確認してください。

デモ: http://regex101.com/r/pR8oF5

于 2013-10-14T15:00:14.430 に答える