Es gibt bereits mehrere good discussions von regular expressions and empty lines auf SO. Ich werde diese Frage entfernen, wenn es sich um ein Duplikat handelt.Unterschiede zwischen leeren und leeren Zeilen in Regexps
Kann jemand erklären, warum dieses Skript 5 3 4 5 4 3
anstelle von 4 3 4 4 4 3
ausgibt? Wenn ich es im Debugger $blank
und $classyblank
starte bleibe bei "4" (was ich vermute, ist der richtige Wert) bis kurz vor der print-Anweisung.
my ($blank, $nonblank, $non_nonblank,
$classyblank, $classyspace, $blanketyblank) = 0 ;
while (<DATA>) {
$blank++ if /\p{IsBlank}/ ; # POSIXly blank - 4?
$nonblank++ if /^\P{IsBlank}$/ ; # POSIXly non-blank - 3
$non_nonblank++ if not /\S/ ; # perlishly not non-blank - 4
$classyblank++ if /[[:blank:]]/ ; # older(?) charclass blankness - 4?
$classyspace++ if /^[[:space:]]$/ ; # older(?) charclass whitespace - 4
$blanketyblank++ if /^$/ ; # perlishly *really empty* - 3
}
print join " ", $blank, $nonblank, $non_nonblank,
$classyblank, $classyspace, $blanketyblank , "\n" ;
__DATA__
line above only has a linefeed this one is not blank because: words
this line is followed by a line with white space (you may need to add it)
then another blank line following this one
THE END :-\
Ist es etwas mit dem __DATA__
Abschnitt zu tun oder bin ich Missverständnis reguläre Ausdrücke POSIX?
ps:
Wie auf einer fristgerechten Beitrag in Kommentar bemerkt elsewhere "wirklich leer" (/^$/
) können Nicht-Leere verpassen:
perl -E 'my $string = "\n" . "foo\n\n" ; say "empty" if $string =~ /^$/ ;'
perl -E 'my $string = "\n" . "bar\n\n" ; say "empty" if $string =~ /\A\z/ ;'
perl -E 'my $string = "\n" . "baz\n\n" ; say "empty" if $string =~ /\S/ ;'
Dann gibt es 'if/\ A \ Z /' und 'if/\ A \ z /' ... die in verschiedenen Sprachen ziemlich konsistent sind [außer Python aber das ist ok] (http://stackoverflow.com/ Fragen/7063420/Perl-kompatibler-regulärer-Ausdruck-pcre-in-Python). –
'Dies ist Perl 5, Version 22, Subversion 0 (v5.22.0) für amd64-freebsd' gebaut –
Nicht im Zusammenhang mit Ihrer Kernfrage, aber' my $ string = "\ n", "foo \ n \ n" ' weist $ $ string eine einzelne neue Zeile zu. Der Rest ist wegen des Komma-Betreibers weggeworfen. – ThisSuitIsBlackNot