Peter Daum am Samstag, 5. August 2006 18:45:
> Hi,

Hallo Peter

> when trying to process continuation lines in a file, I ran
> into a weird phenomenon that I can't make any sense of:
>
> $s contains a line read from a file, that ends with a backslash
> (+ the newline character), so
>
> $s='abc \
> ';
>
> $s =~ /^(.*)$/; print $1; # prints "abc \" as expected
>
>
> If the line ends with a backslash, I don't want to include it
> in the grouping, thus I use:
>
> $s =~ /^(.*[^\\])(\\)?$/; print "1: '$1', 2: '$2'";

You can see what matches what by adding additional catching () :

use warnings;

my $s =~ /^ (?:  (.*) ([^\\])  )   (\\)?  $ /x;
print "1: <$1>, 2: <$2>, 3: <$3>";

This shows:

1: <abc \>, 2: <
>, 3: <>

^
¦
this is not a citation '>'

The greedy .* matches as much as possible on the line: including '\'
The [^\\] matches the last "char" that is not a literal '\': that's the 
newline.
The (\\)? is optional and matches nothing, since nothing is left on the line.


> I would expect $1 to hold "abc " and $2=="\\", but instead,
> the first grouping  holds everything including the backslash
> and the following newline, while $2 is left undefined.
>
> The perlre manpage says:
> > .   Match any character (except newline)
> > $   Match the end of the line (or before newline at the end)
>
> but in my case the "." obviously matched the newline at the end.
>
> When I do a chomp($s) first, everything behaves as expected,
> while a "/m" at the end of the regular expression doesn't
> make any difference.
>
> Does anybody have an explanation what is going on here?
>
> Regards,
>                      Peter Daum


Hilft Dir das weiter?

Dani

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to