Reputation: 362
here is my string : "i love regex but i don't really understand regex. freaking regex!"
The way I understand regex (the wrong way for sure), if I capture text with "i.*regex". For me, there is 5 matches :
1 - "i love regex"
2 - "i love regex but i don't really understand regex"
3 - "i love regex but i don't really understand regex. freaking regex"
4 - "i don't really understand regex"
5 - "i don't really understand regex. freaking regex"
I don't understand the algotrithm behind regex text scanning.
Here my C++/Qt code :
QString str = "i love regex but i don't really understand regex. freaking regex!";
QRegularExpression re1("i.*regex");
qDebug()<<re1.match(str).captured(0); // OUT : "i love regex but i don't really understand regex. freaking regex"
QRegularExpression re2("i.{0,10}regex");
qDebug()<<re2.match(str).captured(0); // OUT : "i love regex"
So in Qt or in general, what is the expression to extract the first match("i love regex") from "i love regex but i don't really understand regex. freaking regex!"??
Thanks in advance
Upvotes: 1
Views: 1436
Reputation: 4029
Default matches are greedy. To get the shortest match use:
QRegExp::setMinimal(true);
This will revoke the greedy behaviour and return the shortest match. See: http://doc.qt.io/qt-5/qregexp.html
Upvotes: 0
Reputation: 5962
As already mentioned in a comment, it's the quantifier you use that determines whether you'll get the longest possible match or the shortest possible match.
*
basically means "any number of occurrences, but as many as possible still resulting in a match"
*?
basically means "any number of occurrences, but as few as possible still resulting in a match"
Quantifiers like *
and +
are by default "greedy", meaning they match as much as possible. If you modify them with a ?
they become "lazy" and exhibit the other behavior.
This being said, some regex implementations may not support lazy quantifiers, and it's possible you'll have to use some other method in your particular situation.
Upvotes: 1