c++ - 在 Qt 中使用正则表达式查找两个标签之间的字符串

标签 c++ regex qt parsing

谁能帮我解决这个问题?

我有一个包含 N 个子字符串的字符串,由标签分隔,我必须获取所有子字符串。字符串就像

STARTfoo barENDSTARThi there!ENDSTARTstackoverflowrulezEND

我想获取 START/END 标签之间的所有字符串,我尝试了几个正则表达式但没有成功:

(START)(.*)(END) gives me ALL the contend between the first and last tag

(START)(\w+)(END) gives me no result

代码很简单:

QString l_str "STARTfoo barENDSTARThi there!ENDSTARTstackoverflowrulezEND"; 
QRegExp rx("(START)(\w+)(END)");
QStringList list;
int pos = 0;
while ((pos = rx.indexIn(l_str, pos)) != -1)
{
    list << rx.cap(1);
    pos += rx.matchedLength();
}
qWarning() << list;

我想要一个像这样的结果列表:

STARTfoo barEND

STARThi there!END

STARTstackoverflowrulezEND

有什么帮助吗?

谢谢!

最佳答案

rx.setMinimal(true).* 一起使用使其变得惰性:

QRegExp rx("START.*END");
rx.setMinimal(true);

参见 QRegExp::setMinimal docs :

Enables or disables minimal matching. If minimal is false, matching is greedy (maximal) which is the default.

关于c++ - 在 Qt 中使用正则表达式查找两个标签之间的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37297341/

相关文章:

c++ - 同一类型的多个 typedef

C++类实例数组初始化

python - PySide:如果隐藏了一个小部件,调整主窗口大小的最佳方法是什么?

python - 如何在qt4的文本编辑框中创建多个选择?

c++ - gcc 链接器获取未使用对象的列表

c++ - 使用 EIGEN 定义动态矩阵

正则表达式匹配和验证互联网媒体类型?

python - RegEx:如何匹配特定时间以上的时间码?

c# - Trim() 无法正常工作?

linux - 当我将我的QT版本从4.8.6升级到4.8.7时,widget列表会失去焦点