Qt 使用 QRegularExpression 多行选项
Qt Using QRegularExpression multiline option
我正在编写一个使用 QRegularExpression 和 MultilineOption 的程序,我编写了这段代码,但匹配停止在第一行。为什么?我哪里做错了?
QString recv = "AUTH-<username>-<password>\nINFO-ID:45\nREG-<username>-<password>-<name>-<status>\nSEND-ID:195-DATE:12:30 2/02/2015 <esempio>\nUPDATEN-<newname>\nUPDATES-<newstatus>\n";
QRegularExpression exp = QRegularExpression("(SEND)-ID:(\d{1,4})-DATE:(\d{1,2}):(\d) (\d{1,2})\/(\d)\/(\d{2,4}) <(.+)>\n|(AUTH)-<(.+)>-<(.+)>\n|(INFO)-ID:(\d{1,4})\n|(REG)-<(.+)>-<(.+)>-<(.+)>-<(.+)>\n|(UPDATEN)-<(.+)>\n|(UPDATES)-<(.+)>\n", QRegularExpression::MultilineOption);
qDebug() << exp.pattern();
QRegularExpressionMatch match = exp.match(recv);
qDebug() << match.lastCapturedIndex();
for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
qDebug() << match.captured(i);
}
有人可以帮助我吗?
你得到了一个 or
表达式,第一个为真,任务完成。
您需要拆分字符串并循环数组以与此表达式进行比较我认为可以工作。
如果数据每次都具有相同的结构,你可以使用这样的东西:
"(AUTH)-<([^>]+?)>-<([^>]+?)>\nINFO-ID:(\d+)\n(REG)-<([^>]+?)>-<([^>]+?)>-<([^>]+?)>-<([^>]+?)>\n(SEND)-ID:(\d+)-DATE:(\d+):(\d+) (\d+)/(\d+)/(\d+) <([^>]+?)>\n(UPDATEN)-<([^>]+?)>\n(UPDATES)-<([^>]+?)>"
21 场比赛
答案是您应该使用 .globalMatch
方法而不是 .match
。
参见QRegularExpression documentation:
Attempts to perform a global match of the regular expression against
the given subject string, starting at the position offset inside the
subject, using a match of type matchType and honoring the given
matchOptions. The returned QRegularExpressionMatchIterator is
positioned before the first match result (if any).
此外,您可以删除 QRegularExpression::MultilineOption
选项,因为它未被使用。
示例代码:
QRegularExpressionMatchIterator i = exp.globalMatch(recv);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
// ...
}
实际上我 google 这个问题有类似的问题,但我不能完全同意答案,因为我认为大多数关于与新 QRegularExpression 进行多行匹配的问题都可以回答为以下:
使用 QRegularExpression::DotMatchesEverythingOption 选项允许 (.) 匹配换行符。这对于从 QRegExp
移植非常有用
我正在编写一个使用 QRegularExpression 和 MultilineOption 的程序,我编写了这段代码,但匹配停止在第一行。为什么?我哪里做错了?
QString recv = "AUTH-<username>-<password>\nINFO-ID:45\nREG-<username>-<password>-<name>-<status>\nSEND-ID:195-DATE:12:30 2/02/2015 <esempio>\nUPDATEN-<newname>\nUPDATES-<newstatus>\n";
QRegularExpression exp = QRegularExpression("(SEND)-ID:(\d{1,4})-DATE:(\d{1,2}):(\d) (\d{1,2})\/(\d)\/(\d{2,4}) <(.+)>\n|(AUTH)-<(.+)>-<(.+)>\n|(INFO)-ID:(\d{1,4})\n|(REG)-<(.+)>-<(.+)>-<(.+)>-<(.+)>\n|(UPDATEN)-<(.+)>\n|(UPDATES)-<(.+)>\n", QRegularExpression::MultilineOption);
qDebug() << exp.pattern();
QRegularExpressionMatch match = exp.match(recv);
qDebug() << match.lastCapturedIndex();
for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
qDebug() << match.captured(i);
}
有人可以帮助我吗?
你得到了一个 or
表达式,第一个为真,任务完成。
您需要拆分字符串并循环数组以与此表达式进行比较我认为可以工作。
如果数据每次都具有相同的结构,你可以使用这样的东西:
"(AUTH)-<([^>]+?)>-<([^>]+?)>\nINFO-ID:(\d+)\n(REG)-<([^>]+?)>-<([^>]+?)>-<([^>]+?)>-<([^>]+?)>\n(SEND)-ID:(\d+)-DATE:(\d+):(\d+) (\d+)/(\d+)/(\d+) <([^>]+?)>\n(UPDATEN)-<([^>]+?)>\n(UPDATES)-<([^>]+?)>"
21 场比赛
答案是您应该使用 .globalMatch
方法而不是 .match
。
参见QRegularExpression documentation:
Attempts to perform a global match of the regular expression against the given subject string, starting at the position offset inside the subject, using a match of type matchType and honoring the given matchOptions. The returned QRegularExpressionMatchIterator is positioned before the first match result (if any).
此外,您可以删除 QRegularExpression::MultilineOption
选项,因为它未被使用。
示例代码:
QRegularExpressionMatchIterator i = exp.globalMatch(recv);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
// ...
}
实际上我 google 这个问题有类似的问题,但我不能完全同意答案,因为我认为大多数关于与新 QRegularExpression 进行多行匹配的问题都可以回答为以下:
使用 QRegularExpression::DotMatchesEverythingOption 选项允许 (.) 匹配换行符。这对于从 QRegExp
移植非常有用