Qt 6.11 is out! See what's new in the release
blog
[SOLVED] How to tell QRegExp to take the first result?
General and Desktop
3
Posts
2
Posters
1.7k
Views
1
Watching
-
lets say we have a text
@start wekjrnwekjrnkjn end erweriuweirbiwur end
@This code
@QRegExp filter("start(.+)end");
int result = filter.indexIn(html_source);
if(result != -1)
qDebug() << filter.cap(1);@will output
@wekjrnwekjrnkjn end erweriuweirbiwur@and not
@wekjrnwekjrnkjn@that i want.. any ideas?
-
Regular Expressions are "greedy" be default. The string it picks perfectly matches your RegExp. The string you want matches as well, but it's shorter. So RegExp picks to longer one. That's the greediness.
The best way is to fix your RegExp, so it will only match the desired string.
For example:
@QRegExp filter("start\s+(\w+)\s+end");@Alternatively, you may try the QRegExp::setMinimal() function.