By specifying a number(4) inside curly braces, the regex will match exactly 4 digits. In your case, it matched exactly 4 digits and displayed it. You had no other restrictions on that pattern, so the string could further have digits,spaces, alpha-characters following those 4 digits. But the regex will match the first occurence of continuous 4-digits and returns true.
If you want to get 4 digit number, you should use either word boundary(\b) or space(\s) around the 4-digit number.
-
use strict;
-
my $string = "I have 256987, 2004 and I a 587458";
-
-
if ($string =~ /\b(\d{4})\b/)
-
{
-
print $1;
-
}
-