3

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?

PeterBlack
  • 33
  • 1
  • 3
  • 1
    Manual says that `MultilineOption` changes behavior only regarding `^` and `$` and your expression doesn't contain them. – Predelnik Mar 27 '15 at 22:01

3 Answers3

3

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();
    // ...
}
Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2

Actually I google'd this question having similar issue, but I couldn't agree completely with an answer, as I think most of the questions about multi-line matching with new QRegularExpression can be answered as following:

use QRegularExpression::DotMatchesEverythingOption option which allows (.) to match newline characters. Which is extremely useful then porting from QRegExp

evilruff
  • 3,947
  • 1
  • 15
  • 27
0

you got an or Expression and the first one is true, job is done. you need to split the string and loop the array to compare with this Expression will work i think.

If the data every times have the same struct you can use something like this:

"(AUTH)-<([^>]+?)>-<([^>]+?)>\\nINFO-ID:(\\d+)\\n(REG)-<([^>]+?)>-<([^>]+?)>-<([^>]+?)>-<([^>]+?)>\\n(SEND)-ID:(\\d+)-DATE:(\\d+):(\\d+) (\\d+)/(\\d+)/(\\d+) <([^>]+?)>\\n(UPDATEN)-<([^>]+?)>\\n(UPDATES)-<([^>]+?)>"

21 Matches

Scriptkiddy1337
  • 792
  • 4
  • 9