Re: /(foo|)/ vs /(foo)?/



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.
.



Relevant Pages

  • Re: lc() with undefined arg
    ... > undef: 'reverse' in scalar context. ... the value is no longer undef but the empty string. ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)
  • .bottom of TextWidthHeight
    ... Within the With block of "sRect" ... Variable in question: ".Bottom" ... properly calculate the .Bottom variable where the rowsource of a listbox is ... Have the code arbutarily put in a string value that is not an empty string ...
    (microsoft.public.access.formscoding)
  • Re: strange return value
    ... the fact that it's returning an actual empty string is ... but normal behavior for Perl's idea of a boolean. ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)
  • Re: Use of uninitialized value in substitution (s///)
    ... >> Perl converts the undefined value to an empty string, ... My point is that warning or not, the substitution seems to ... so the purpose of the substitution is attained. ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)
  • Re: regular expression
    ... Your string does not cover empty string. ... expression so that when the user types text into the textbox, ... What is the regular expression syntax for that? ...
    (microsoft.public.dotnet.framework.aspnet)