views:

103

answers:

2

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

+1  A: 

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.

Daniel Brückner
+2  A: 

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.

MizardX
Maybe it's just the flavor I'm using, but that seems to match just the a and the b and the c; no delimiters. Which makes sense, because '??' will always be able to match nothing, and since it's lazy, it will always choose nothing, no?
wkf
Wait you changed it, so what I said probably no longer applies. I still see it matching the last comma though.
wkf
It matches the last comma, but does not capture it in the group.
Daniel Brückner
Got it. Excellent, thanks a lot!
wkf
You don't need ?? to achieve this:^((?:[^,]+,)*[^,]+),?$
Jan Goyvaerts
There are several ways to do most things.
MizardX