Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload