In Perl, how can I use one regex grouping to capture more than one occurrence that matches it, into several array elements?
For example, for a string:
var1=100 var2=90 var5=hello var3="a, b, c" var7=test var3=hello
to process this with code:
$string = "var1=100 var2=90 var5=hello var3=\"a, b, c\" var7=test var3=hello";
my @array = $string =~ <regular expression here>
for ( my $i = 0; $i < scalar( @array ); $i++ )
{
print $i.": ".$array[$i]."\n";
}
I would like to see as output:
0: var1=100
1: var2=90
2: var5=hello
3: var3="a, b, c"
4: var7=test
5: var3=hello
What would I use as a regex?
The commonality between things I want to match here is an assignment string pattern, so something like:
my @array = $string =~ m/(\w+=[\w\"\,\s]+)*/;
Where the * indicates one or more occurrences matching the group.
(I discounted using a split() as some matches contain spaces within themselves (i.e. var3...) and would therefore not give desired results.)
With the above regex, I only get:
0: var1=100 var2
Is it possible in a regex? Or addition code required?
Looked at existing answers already, when searching for "perl regex multiple group" but not enough clues:
- Dealing with multiple capture groups in multiple records
- Multiple matches within a regex group?
- Regex: Repeated capturing groups
- Regex match and grouping
- How do I regex match with grouping with unknown number of groups
- awk extract multiple groups from each line
- Matching multiple regex groups and removing them
- Perl: Deleting multiple reccuring lines where a certain criterion is met
- Regex matching into multiple groups per line?
- PHP RegEx Grouping Multiple Matches
- How to find multiple occurrences with regex groups?