Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "Year 2010" =~ /([0-4]*)/ results in empty string in $1?

Tags:

regex

perl

If I run

"Year 2010" =~ /([0-4]*)/;
print $1;

I get empty string. But

"Year 2010" =~ /([0-4]+)/;
print $1;

outputs "2010". Why?

like image 219
alexanderkuk Avatar asked Oct 21 '10 09:10

alexanderkuk


3 Answers

You get an empty match right at the start of the string "Year 2010" for the first form because the * will immediately match 0 digits. The + form will have to wait until it sees at least one digit before it matches.

Presumably if you can go through all the matches of the first form, you'll eventually find 2010... but probably only after it finds another empty match before the 'e', then before the 'a' etc.

like image 108
Jon Skeet Avatar answered Nov 12 '22 21:11

Jon Skeet


The first regular expression successfully matches zero digits at the start of the string, which results in capturing the empty string.

The second regular expression fails to match at the start of the string, but it does match when it reaches 2010.

like image 6
Mark Byers Avatar answered Nov 12 '22 23:11

Mark Byers


The first matches the zero-length string at the beginning (before Y) and returns it. The second searches for one-or-more digits and waits until it finds 2010.

like image 5
eumiro Avatar answered Nov 12 '22 21:11

eumiro