Regex Group in Perl: how to capture elements into array from regex group that matches unknown number of/multiple/variable occurrences from a string?

前端 未结 9 2144
遇见更好的自我
遇见更好的自我 2020-12-04 10:10

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:



        
9条回答
  •  攒了一身酷
    2020-12-04 10:43

    It is possible to do this with regexes, however it's fragile.

    my $string = "var1=100 var2=90 var5=hello var3=\"a, b, c\" var7=test var3=hello";
    
    my $regexp = qr/( (?:\w+=[\w\,]+) | (?:\w+=\"[^\"]*\") )/x;
    my @matches = $string =~ /$regexp/g;
    

提交回复
热议问题