c++qtqt5qregexp

Get a list of results from by conditions


I will want to get a list of resaults by this condition of reg expression:

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;
}

I have got it:

"pegData_https://sfasdfsf_sadlfkjnsdlfjhn.jpgasodjfhasdfoho;usdoauhfsvc.asdfkpjhttps://adfklja32908jf0jmn01.jpg"

I need to get:

  1. https://sfasdfsf_sadlfkjnsdlfjhn.jpg
  2. https://adfklja32908jf0jmn01.jpg

Please, help me, what is wrong in QRegExp condition?


Solution

  • By default, matching is greedy. Your desired behavior should simply be a case of calling setMinimal(true) on your QRegExp.

    e.g.

    QRegExp rx("(https://)(.*)(\\.jpg)");
    rx.setMinimal(true);