Search code examples
regexqtqtcore

QRegExp minimal matching by size


I want the smallest string that starts with '$' and ends with '#', from the beginning of the string(the string: $efg#)

I tried QRegExp::setMinimal but it's helping since it lookes from the first chatrecter:

QRegExp rx("\\$\.*#");
rx.setMinimal(true);
int i = rx.indexIn("$abcd$efg#");
QString s = rx.cap(0);
bool isMinimal= rx.isMinimal();

The result is: i=0 s=$abcd$efg# isMinimal = true

while what i wanted was: i=5 s=$efg#


Solution

  • You may want to redifine your regex as:

    QRegExp rx("\\$[^\\$#]*#");
    

    This will match the minimum $*# since it does not allow $ or # inbetween