[sf-perl] don't understand regexp behavior

Mark Kvale kvale at phy.ucsf.edu
Mon Oct 1 11:53:40 PDT 2007


David Alban wrote:
> i've got a file with one line:
> 
>   $ cat ~/junk2
>   //foo/bar/bat.c#42 delete
> 
>   $ od -c ~/junk2
>   0000000   /   /   f   o   o   /   b   a   r   /   b   a   t   .   c   #
>   0000020   4   2       d   e   l   e   t   e  \n
>   0000032
> 
> and i get:
> 
>   $ perl -ne 'm{ \A ( [^\#]+ \# \d+ ) \s+ \S+ }xms and print' ~/junk2
>   //foo/bar/bat.c#42 delete
> 
>   $ perl -ne 'm{ \A ( [^\#]+ \# \d+ ) \s+ \S+ \z }xms and print' ~/junk2
> 
> the second invocation prints nothing.
> 
> what am i missing?  i would expect both patterns to match.  i actually
> want to use the second pattern because words like 'delete', 'add',
> 'edit' will be last on each line of input.
> 
>   This is perl, v5.8.5 built for i386-linux-thread-multi
> 
> thanks,
> david


Note that \z matches the very end of the sting, but you have not allowed for the 
newline whitespace:

1031% perl -ne 'm{ \A ( [^\#]+ \# \d+ ) \s+ \S+ \n\z}xms and print' test.txt
//foo/bar/bat.c#42 delete

If you don't want to deal with the newline, use \Z.

Mark


More information about the SanFrancisco-pm mailing list