Why does * Perl's regex seems not to be greedy?

I expected this to print "[b]", but it will print "[]":

$x = "abc";
$x =~ /(b*)/;
print "[$1]";

If a star is replaced by a plus, it acts as I expect. Shouldn't both the pluses and the stars be greedy?

ADDED: Thanks to everyone for pointing out (in seconds, it seemed!) That "b *" matches an empty line, the first occurrence of which precedes the beginning of the line. So greed is not a problem. It matches the empty string, even before the first "b".

+3
source share
6 answers

It is greedy, but b*will match an empty string. everything * will always match an empty string, so

  "abc"
  /\
     --- matches the empty string here.

$', abc, . , "bbb" "bbb", "b" "bb".

+10

, b* , .. a. , , :

$x = "zabc";
$x =~ /(.b*)/;
print "[$1]";
+10

a(backtrack) ( , regex backtracked) . + a c, $1 b.

+3

, . 'abc' = ~/(b *)/ , b. "bbc", :

[]

+3

, (AFAIR Perl, NFA). , .

"DFA vs NFA" .

+1

A * at the end of the template is almost always not what you want. We even have this as a trick in Learning Perl to illustrate this particular problem.

0
source

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


All Articles