in reply to Re: Regex for matching dotted numbers
in thread Regex for matching dotted numbers
my @nums = $dotted_num_string =~ /^(\d+)(?:\.(\d+))*$/;
...which captures the numeric parts in @nums.
The second capture group above will only return the last substring matched even though it may match many times. A regex solution is possible, but just using split might be preferable. (It's also important to know if the dotted number string is 'alone' or is embedded in a longer string, whether recognition or extraction is intended, etc., none of which is entirely clear to me.)
c:\@Work\Perl\monks>perl -wMstrict -le "my $dotted_num_string = '1.22.333.4444'; my @nums = $dotted_num_string =~ /^(\d+)(?:\.(\d+))*$/; printf qq{'$_' } for @nums; print ''; ;; @nums = $dotted_num_string =~ /^(\d+)(\.\d+){3,5}$/; printf qq{'$_' } for @nums; print ''; ;; @nums = $dotted_num_string =~ m{ \G [.]? (\d+) }xmsg; printf qq{'$_' } for @nums; " '1' '4444' '1' '.4444' '1' '22' '333' '4444'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Regex for matching dotted numbers
by QM (Parson) on Jul 21, 2014 at 19:54 UTC |