Save all matches of a group in Regex

1

Consider the following group:

([0-9 ]+)+

I capture matches that contain numbers and spaces, in the following string for example:

123  , 2133132, 31331, 22222222, 23......

would capture "23", the last match, but what I want is to be able to save all the matches, in this case it would have been five.

I'm trying to use perl, so I would have something like this:

echo "123  , 2133132, 31331, 22222222, 23....." | perl -pe "s/([0-9 ]+)+//g"

And then the same, only the last one.

Note: I am working on shell script, specifically ksh on Hp ux.

    
asked by manduinca 27.05.2017 в 19:19
source

2 answers

3

When a group is repeated, only the last text that coincided is captured. To show each result, you need to make multiple matches.

To find all matches, the /g (GLOBAL) switch must be used. It is not necessary to use groups, just the result of the global match.

In Perl:

my $texto = "123  , 2133132, 31331, 22222222, 23.....";

my @matches = $texto =~ /[\d ]+/g;

print "@matches\n";

Demo: link


Or, from command line:

$ echo -ne '123  , 2133132, 31331, 22222222, 23.....' | perl -wnE'say for /[\d ]+/g'
    
answered by 27.05.2017 в 22:42
0

You have to pay attention to the single quotes that surround the Perl miniprogram:

echo "123  , 2133132, 31331, 22222222, 23....." | perl -ne 'print "$1\n" while /([0-9]+)/g'
    
answered by 24.09.2017 в 13:42