带有QRegulareXpression双引号中的字符串

Extract strings inside double quotes with QRegularExpression

本文关键字:字符串 QRegulareXpression 带有      更新时间:2023-10-16

我的字符串如下:

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

我想使用qregularexpression从该字符串中提取所有URL。我已经尝试了:

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

但这不起作用。

您可以使用

QRegExp regExp("goToNetPage\s*"([^"]+)");
QStringList MyList;
int pos = 0;
while ((pos = regExp.indexIn(handler, pos)) != -1) {
    MyList << regExp.cap(1);
    pos += regExp.matchedLength();
}

模式是

goToNetPages*"([^"]+)

它匹配goToNetPage、0或更多Whitespace Chars,",然后捕获到第1组中,除"以外的任何1 字符 - 使用regExp.cap(1)