* is greedy - it matches as many characters as it can, but it can match none at all. In (\d+).*(\d*\d+) the \d* is redundant (the following \d+ matches at least 1 digit and as many as it may) and the .* before it matches as many charactes as it can including all except one digit (the \d+ grabs one digit). One way to fix the problem is:
use strict; use warnings; my $data = "Exlief 4 page : 1 /10"; my $match = qr/pag\w+\s*:\s*(\d+)[^\d]*(\d+)/; print "Pages : $1 / $2\n" if $data =~ $match; $data = "Exlief 4 page : 1 / 5"; print "Pages : $1 / $2\n" if $data =~ $match;
Prints:
Pages : 1 / 10 Pages : 1 / 5
Note that a precompiled regex is used to save retyping (perhaps differently) the regex and that the 'match any character' has been replaced by 'match any character except a digit' and that the redundant digit match has been removed.
In reply to Re: Matching numbers by regex.
by GrandFather
in thread Matching numbers by regex.
by Wasted
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |