Capturing all the regex matches into an array in perl
Often we need to track the list of matches whenever we use a regex.for example lets say i have a file which looks like below:
Jan 1 1982
Dec 20 1983
jan 6 1984
Now if i want to store all the decimal number in the file in a array..How?
first thing is we need to use a regex match and store all the numbers into an array.
Below is the code for it :
perl -lne 'push @a,/\d+/g;END{print "@a"}' your_file
The above will output:
> perl -lne 'push @a,/\d+/g;END{print "@a"}' your_file
1 1982 20 1983 6 1984
In the same way you can use any other pattern in the match regex and capture those patterns in an array.
0 comments: