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

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

Thread Next
Dorner Thomas
August 28, 2001 03:34
Bug or Feature: /\S$/ matches "\n"
Message ID:
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
~> perl -e '$line="xx\n";$line.=" "unless $line!~m/\S$/;print ">$line<\n";'

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

A few more tests show:

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

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?

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

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About