> But it doesn't work for this case:

$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'

> it expects 1 returned.

Well, assuming you mean it shouldn't match as $x starts with a slash and
the RE doesn't - you're on the right path.  The reason is, the match goes
anywhere, it is "unanchored" so Perl happily says, "walking" down $x saying:
"slash? nope. "p"? match! "a" match!! ... slash? yay! one or more ("+")
word chars ("\w")? Aw, fail"

so, actually that RE fails:
$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'
[crickets]
as there's not even 1 "\w"
$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w}'


zero or more ("*") works
$ perl -le '$x="/path/"; print 1 if $x=~m{path/\w*}'
1

as does none
$ perl -le '$x="/path/"; print 1 if $x=~m{path/}'
1

and adding the initial "/" to the RE still works:
$ perl -le '$x="/path/"; print 1 if $x=~m{/path/}'
1

but if you'd anchored ("^" - zero-width "at the beginning of the string"
must be at the begining of the RE) your RE would fail too:
$ perl -le '$x="/path/"; print 1 if $x=~m{^path/}'

because the RE starts w/ "p" and the $x starts with slash.


On Tue, Jul 17, 2018 at 6:56 AM, Lauren C. <lau...@miscnote.net> wrote:

> Hello,
>
> I want to match:
>
> /path/
> /path/123
> /path/abc
>
> but /path/?xxx  should not be matched.
>
> This works:
>
> $ perl -le '$x="/path/abc"; print 1 if $x=~m{path/\w+}'
> 1
>
>
> this works too:
>
> $ perl -le '$x="/path/?abc"; print 1 if $x=~m{path/\w+}'
>
>
> But it doesn't work for this case:
>
> $ perl -le '$x="/path/"; print 1 if $x=~m{path/\w+}'
>
> it expects 1 returned.
>
> Can you help? thanks.
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk

Reply via email to