Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Using QRegularExpression multiline option

I'm writing a program that use QRegularExpression and MultilineOption, I wrote this code but matching stop on first line. Why? Where am I doing wrong?

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

Can someone help me?

like image 729
PeterBlack Avatar asked Oct 27 '25 08:10

PeterBlack


1 Answers

The answer is you should use .globalMatch method rather than .match.

See QRegularExpression documentation on that:

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).

Also, you can remove the QRegularExpression::MultilineOption option as it is not being used.

Sample code:

QRegularExpressionMatchIterator i = exp.globalMatch(recv);
while (i.hasNext()) {
    QRegularExpressionMatch match = i.next();
    // ...
}
like image 197
Wiktor Stribiżew Avatar answered Oct 28 '25 23:10

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!