develooper Front page | perl.beginners | Postings from July 2011

Re: s/// and \n question

Thread Previous | Thread Next
From:
C.DeRykus
Date:
July 22, 2011 03:36
Subject:
Re: s/// and \n question
Message ID:
29d7386f-95d1-42c5-91cc-361dfb979f09@z7g2000prh.googlegroups.com
On Jul 21, 6:06 pm, shawnhco...@gmail.com (Shawn H Corey) wrote:
> On 11-07-21 08:54 PM, Rob Dixon wrote:
> ...
> I think part of the confusion is also in what does $ match?  According
> to perlre, under "Regular Expressions",
>
>     $        Match the end of the line (or before newline at the end)
>
> That means it can match the end of the string or a newline at the end of
> the string.
>
> Try this and see what it prints:
>
> #!/usr/bin/env perl
>
> use strict;
> use warnings;
>
> while( <> ){
>    print if /(\w+)$/}
>
> __END__
>

Just entering a 'return' which will put a newline in
$_  won't print if the target regex is  /(\w+)+$/.
Only [a-zA-Z_0-9]  --or potentially more  with
certain locale settings-- will.  But not a newline
in any case.

Even /(.+)$/ won't match a single newline at the
end  unless the  regex '/s' modifier is used:

    perl -E "$_ = qq{\n}; say qq{matched\n} if /(.)$/"  <-- no

    perl -E "$_ = qq{\n}; say qq{matched\n} if /(.)$/s"  <---yes

--
Charles DeRykus


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About