Reguläre Ausdrücke scheitern an '\n'?

Hallo,

ich habe aus einem String was rauszuziehen und arbeite dabei mit regulären Ausdrücken. Kein Problem, dachte ich, bis jetzt einer der Strings zufällig einen Zeilenumbruch enthielt, und zwar vor der Stelle, die mein Suchmuster enthält.

Kleines Beispiel:

#!/usr/bin/perl
 
my $S1 = 'Das ist ein beliebiger Text.';
my $S2 = 'Das ist' . "\n" . ' ein beliebiger Text.';
 
$S1 =~ /(.+ein)/;
print 'Variante ohne "\n": ' . $1 . "\n";
 
$S2 =~ /(.+ein)/;
print 'Variante mit "\n": ' . $1 . "\n";

Bei der ersten Variante kommt natürlich „Das ist ein“ raus, bei der zweiten aber nur " ein".
Die Ursache ist mir klar: Der Punkt steht für alles außer „\n“. Gibt es das auch ohne diese Einschränkung? Oder wie komme ich sonst zu einem Ergebnis, welches das „\n“ noch enthält?

Danke,
Kristian

stichwort egimosx

pod -> perlre

m
Treat string as multiple lines. That is, change ^'' and $’’ from matching at only the very start or end of the string to the start or end of any line anywhere within the string,

$foo =~ /muster/m

Aha, muß ich mal probieren :wink:

$foo =~ /muster/m

Die Option war war nicht dokumentiert in SELFHTML, oder ich habe sie übersehen :wink:

Danke!
Kristian

stichwort egimosx

pod -> perlre

m
Treat string as multiple lines. That is, change ^'' and $’’ from matching at only the very start or end of the
string to the start or end of any line anywhere within the
string,

fast:
s Treat string as single line. That is, change „.“ to
match any character whatsoever, even a newline, which
normally it would not match.

$foo =~ /muster/s

fast:
s Treat string as single line. That is, change „.“
to
match any character whatsoever, even a newline,
which
normally it would not match.

$foo =~ /muster/s

recht hast du…