c++ - 使用 QRegularExpression 提取双引号内的字符串
问题描述
我有一个如下字符串:
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();
}
模式是
goToNetPage\s*"([^"]+)
它匹配goToNetPage
0 个或多个空白字符,"
然后将任何 1+ 个字符捕获到第 1 组中,而不是"
- 使用 访问所需的值regExp.cap(1)
。
推荐阅读
- javascript - 为什么我无法在 Javascript Date 对象上使用 getDay() 方法和 getUTCDay() 方法获得相同的结果
- reactjs - 使用钩子将我的基于类的组件转换为功能组件
- python - 我在我的 pyqt5 程序中使用 QDoubleValidator 但它似乎不起作用
- c++ - 字符串到 Int C++ (stoi)
- bots - cortana 自定义技能在 Windows 手机上不起作用
- function - 如果为真函数,Powershell 测试 2 个语句
- angular - 角基本后卫
- angular - Angular 7在具有过渡效果的同一页面中创建卡片视图及其详细视图
- python - 在高斯混合模型/期望最大化模型中包括时间依赖性?
- javascript - 根据vue js中的父元素动态添加表单元素