in reply to Regex for matching dotted numbers

It helps if you show what you've already tried, and how it behaves. Also tell us what your criteria are, so we can judge which solutions work for you.

Given that I'm lazy today, I'll give a short answer anyway (untested):

my @nums = $dotted_num_string =~ /^(\d+)(?:\.(\d+))*$/;

...which captures the numeric parts in @nums.

If you have some known restriction on how many or how few numeric fields there are, you can use a quantifier:

my @nums = $dotted_num_string =~ /^(\d+)(\.\d+){3,5}$/;

gives 4 to 6 numeric fields.

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re^2: Regex for matching dotted numbers
by AnomalousMonk (Archbishop) on Jul 21, 2014 at 13:12 UTC
    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'
      Yes, agreed. This seems to be a simpler version of your last one:
      @nums = $dotted_num_string =~ /(?:(\d+)\.?)/g;

      but allows a final trailing dot.

      I think your idea of using split, with suitable tests, is probably the best generic solution, barring any further constraints.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of