Does this violate the longest principle?

I am trying to write a regular expression to recognize a single line of text, with the underscore (_) being recognized as a line continuation character. For example, "foo_ \ nbar" should be considered as one line, because "foo" ends with an underscore. I'm trying to:

$txt = "foo_\nbar";
print "$&\n" if $txt =~ /.*(_\n.*)*/;

However, this only prints:

foo_

This seems to violate the longest rule for Perl regular expressions!

Interestingly, if I remove the last star (*) in the regular expression, that is:

$txt = "foo_\nbar";
print "$&\n" if $txt =~ /.*(_\n.*)/;

he performs printing:

foo_
bar

But I need a star to find out "0 or more" sequels!

What am I doing wrong?

+3
source share
3

, @ysth. , :

/([^_\n]|_.)*/s
+6

Perl " "; . * , , . _, - :

/(.*(?!(?<=_)\n)_\n)*.*/
+5

:

POSIX . : "a | b" "b | a" .

PERL defines left biased taste. Each "a | b" checks the left branch of "a", and if it can match, "b" is never checked. Thus, "a | b" rarely matches "b | a". Here a * is like () | a | aa | aaa | aaaa | ...

+1
source

Source: https://habr.com/ru/post/1751977/


All Articles