谁能帮我解决这个问题?
我有一个包含 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);
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/