develooper Front page | perl.perl5.porters | Postings from August 2001

Bug or Feature: /\S$/ matches "\n"

Thread Next
From:
Dorner Thomas
Date:
August 28, 2001 03:34
Subject:
Bug or Feature: /\S$/ matches "\n"
Message ID:
6727B1DACFCDD311A757009027CA8D690257AA7A@Ex02.inhouse.start.de
Sorry if this is an FAQ but I haven't found it in the perlre manpage.

I found this behavior on BS2000 first, but my current Linux version
(5.005_03 in the company as well as a 5.6 at home) do the same thing:

~> perl -e '$line = "xx\n"; $line .= " " if $line =~ m/\S$/; print
">$line<\n";'
>xx
 <
~> perl -e '$line="xx\n";$line.=" "unless $line!~m/\S$/;print ">$line<\n";'
>xx
 <

\n is treated as non-whitespace here (see below for the thing really
happening).

A few more tests show:

~> perl -e '$line="xx\n";$line=~s/\s/y/g;print ">$line<\n";'
>xxy<
~> perl -e '$line="xx\n";$line=~s/\s$/y/g;print ">$line<\n";'
>xxy<
~> perl -e '$line="xx\n";$line=~s/\S$/y/g;print ">$line<\n";'
>xy
<
~> perl -e '$line="xx\n";$line=~s/\S/y/g;print ">$line<\n";'
>yy
<

It seems, that $ matches the end of the string *except* when the last
character is a newline, this seems to be ignored in that case.

Is this the intented behavior (= is this a feature) or should this be
considered a bug?

Thomas
PS: At the moment I'm using
$line and substr($line,-1) =~ m/\S/o
as an awkward substitution which is working as intented (by me ;-).

Viele Grüße, Thomas
________________________________________
Thomas Dorner
START AMADEUS, Technologiestrategie (I-T)
Tel.: (0 6172) 91 - 26 41     Fax: - 55 26 41
E-Mail: Thomas.Dorner@start.de

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