in reply to What's wrong with this regex

Since you have written only two pairs of (capturing) parentheses, you only get two match variables. A quantified capture stores only the string of the last match.

So either make the outer parenthesis capture, and use split to extract the numbers from $1, or write out four pairs of capturing parenthesis.

Note that this is more convenient in Perl 6: a quantified capture records a list of all matches.

Replies are listed 'Best First'.
Re^2: What's wrong with this regex
by bgu (Novice) on Mar 01, 2012 at 15:32 UTC
    Thanks for the quick answer guys.

    Unfortunately four groups is not going to help, it should match anthing from 1.2 to 1.2.3.4.5.6 etc, or anything follows this structure.

    To look for a substring matching  [\d+\.]+ is also not an option, as there may be other matches in the string, like a simple number.

      Sorry, I don't understand your answer. What exactly is wrong with the approach that uses split?

      $_ = '1.2.3.4.5.6 foo bar'; if (/((?:\d+\.)+(\d+)/) { my @matches = (split(/\./, $1), $2) }
      I'm thinking you don't need a beefy regex if that's all you want to do. How about something like this instead:
      my $string = '12.34.56.78 asdfasdfasdfasdfasdf'; my $numbers = shift @{[split ' ', $string]}; my @numbers = split '\.', $numbers; use Data::Dumper; print Dumper(@numbers) if $numbers =~ /^[\d.]+$/;

      EDIT: Whoops, sorry moritz - I confess to only skimming your answer and didn't notice that you had already suggested using split. Apologies!