I cannot think of a situation where I'd want to use '??' in a regular expression, but maybe I'm not thinking hard enough.
views:
103answers:
2
+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
2009-06-11 01:54:05
+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
2009-06-11 01:55:44
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
2009-06-11 02:06:07
Wait you changed it, so what I said probably no longer applies. I still see it matching the last comma though.
wkf
2009-06-11 02:17:09
It matches the last comma, but does not capture it in the group.
Daniel Brückner
2009-06-11 02:22:18
Got it. Excellent, thanks a lot!
wkf
2009-06-11 02:43:18
You don't need ?? to achieve this:^((?:[^,]+,)*[^,]+),?$
Jan Goyvaerts
2009-06-12 04:59:17
There are several ways to do most things.
MizardX
2009-06-12 14:30:25