Is the lazy version of the "optional" quantifier ("??") ever useful in regular expression?

I can not imagine a situation where I would like to use ?? in regular expression, but maybe I don’t think it is complicated enough.

+6
regex
source share
2 answers

Maybe the list is delimited by a separator, and you don't want to match any bounding delimiter.

 ^((?:[^,]+,??)+),?$ 

This would capture "a,b,c" from "a,b,c," where, since the non-useful option would include a comma in the capture group.

+7
source share

I would use it as an optimization if the optional part is usually missing.

 Foo(PartUsuallyPresent)?Bar Foo(PartUsuallyAbsent)??Bar 

But I also lack a real example.

+2
source share

All Articles