Re: /(foo|)/ vs /(foo)?/
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel)
- Date: 17 Nov 2005 12:59:21 GMT
kj <socyl@xxxxxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
>
>
>
>
>
> While looking at someone else's code I came across a regular
> expression that included a construct like /(foo|)/. As far as I
> can tell, it should produce the same result as /(foo)?/. But the
> author of the code knows a heck of a lot more Perl than I do, so
> I'm wondering why she would have picked the former over the latter.
> Any ideas?
The difference is in what is captured when no "foo" is found in the
string. /(foo|)/ matches an empty string, so $1 is an empty string
after the match. /(foo)?/ skips the match entirely, so $1 is undefined.
A subtle but relevant difference.
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
.
- References:
- /(foo|)/ vs /(foo)?/
- From: kj
- /(foo|)/ vs /(foo)?/
- Prev by Date: Re: /(foo|)/ vs /(foo)?/
- Next by Date: Re: [OT] Hiding from Google
- Previous by thread: Re: /(foo|)/ vs /(foo)?/
- Next by thread: Re: /(foo|)/ vs /(foo)?/
- Index(es):
Relevant Pages
|