Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

I cannot think of a situation where I'd want to use ?? in a regular expression, but maybe I'm not thinking hard enough.

like image 671
wkf Avatar asked Jun 11 '09 01:06

wkf


2 Answers

Maybe a delimiter-separated list, and you don't want to match any terminating delimiter.

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

That would capture "a,b,c" from "a,b,c,", where as the non-lazy variant would include the comma in the capture-group.

like image 191
Markus Jarderot Avatar answered Oct 11 '22 19:10

Markus Jarderot


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

Foo(PartUsuallyPresent)?Bar

Foo(PartUsuallyAbsent)??Bar

But I definitly lack a real-world example, too.

like image 31
Daniel Brückner Avatar answered Oct 11 '22 19:10

Daniel Brückner