#include <QDebug>
#include <QRegularExpression>
#include <QString>
#include <QStringList>
QStringList parseCommandLine(const QString &commandLine) {
QStringList result;
// 正则表达式匹配不带引号的部分和带引号的部分
QRegularExpression regex(R"(([^\"\s]+)|\"([^\"]*)\")");
QRegularExpressionMatchIterator i = regex.globalMatch(commandLine);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
if (match.captured(1).isEmpty()) {
result.append(match.captured(2)); // 捕获带引号的内容
} else {
result.append(match.captured(1)); // 捕获不带引号的内容
}
}
return result;
}
int main() {
QString commandLine =
" upload \"c:\\pa th 1\\1 .txt\" "
"\"c:\\pa th 2\\2 .txt\"";
QStringList args = parseCommandLine(commandLine);
// 输出解析后的参数
for (const QString &arg : args) {
qDebug() << arg;
}
return 0;
}
/*
输出:
"upload"
"c:\\pa th 1\\1 .txt"
"c:\\pa th 2\\2 .txt"
*/
12-03
2498
05-10
1918