问题描述
我想通过 reg 表达式的这种条件获取结果列表:
QRegExp rx(
"(https://)"
"(.*)"
"(\\.jpg)"
);
QStringList list;
int pos = 0;
while ((pos = rx.indexIn("jpegData_https://sfasdfsf_sadlfkjnsdlfjhn.jpgasodjfhasdfoho;usdoauhfsvc.asdfkpjhttps://adfklja32908jf0jmn01.jpg",pos)) != -1)
{
list << rx.cap(0);
pos += rx.matchedLength();
}
for(auto it : list)
{
qDebug() << it;
}
我知道了:
"pegData_https://sfasdfsf_sadlfkjnsdlfjhn.jpgasodjfhasdfoho;usdoauhfsvc.asdfkpjhttps://adfklja32908jf0jmn01.jpg"
我需要:
https://sfasdfsf_sadlfkjnsdlfjhn.jpg
https://adfklja32908jf0jmn01.jpg
请帮帮我,QRegExp 条件有什么问题?
解决方法
默认情况下,匹配是贪婪的。您想要的行为应该只是在您的 QRegExp 上调用 setMinimal(true)。
例如
QRegExp rx("(https://)(.*)(\\.jpg)");
rx.setMinimal(true);