1

I have a string like below:

on prepareFrame
  go to frame 10
    goToNetPage "http://www.apple.com"
    goToNetPage "http://www.cnn.com"
    etc..
end 

I want to extract all the urls from this string by using QRegularExpression. I've already tried:

QRegularExpression regExp("goToNetPage \"\\w+\"");
QRegularExpressionMatchIterator i = regExp.globalMatch(handler);
while (i.hasNext()) {
    QRegularExpressionMatch match = i.next();
    QString handler = match.captured(0);
}

But this not working.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Mosi
  • 1,178
  • 2
  • 12
  • 30
  • 1
    You should use `regExp("goToNetPage\\s*\"[^\"]+\"")`. Or, `regExp("goToNetPage\\s*\"([^\"]+)")` and then access the value in Group 1 using `match.captured(1)` – Wiktor Stribiżew Dec 19 '18 at 17:44

1 Answers1

1

You may use

QRegExp regExp("goToNetPage\\s*\"([^\"]+)");
QStringList MyList;
int pos = 0;

while ((pos = regExp.indexIn(handler, pos)) != -1) {
    MyList << regExp.cap(1);
    pos += regExp.matchedLength();
}

The pattern is

goToNetPage\s*"([^"]+)

It matches goToNetPage, 0 or more whitespace chars, " and then captures into Group 1 any 1+ chars other than " - the required value is accessed using regExp.cap(1).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563