perlquestion
why_bird
<p>Me again---having some trouble with regexes (again..!)</p>
<p> This is about grouping, and (back?)referencing. I'm reading in a configuration file, and for each line I end up with a string like this:
<code>
$table_ref="!Frequency[A][B][C] ..some other text.."
</code>
<p>
I want to get the name of the table (in this case 'Frequency'), and then each of the table variables (here represented by A, B, C) the variables will always be inside square brackets, and there are between 1 and 4 inclusive of them (but I won't know how many). </p>
<p> So, I tried a regex like this:</p>
<code>
$temp_=~ /\!(\S+?) #tablename
(?:\[(.*?)\]){1,4} #table variables
\s+.*/x; #white space then rest of text.
print Dumper ($1, $2, $3, $4, $5);
</code>
<p> For the string above, the regex returned:<br><br>
$VAR1 = 'Frequency';<br>
$VAR2 = 'C';<br>
$VAR3 = undef;<br>
$VAR4 = undef;<br>
$VAR5 = undef;<br>
</p>
<p> When I replaced the {1,4} with a {3} (I thought there might have been a problem with greediness) I got exactly the same output as above..</p>
<p>I sort of expected (or rather hoped!) that the {1,4} would create multiple matches which would go into $2, $3, $4. Should it and have I done something wrong, or is that not how it works? I read the perdoc info (and some other stuff), but couldn't gather from that how it was supposed to work in this case. Any suggestions, in either case?</p>
<p> Thankyou again monks, you are giving me an enormous amount of help. It makes learning Perl (even more) fun when there are knowledgable people to point out your stupid mistakes/misunderstandings :) </p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-670682">
<center> ........<br>
Those are my principles. If you don't like them I have others.<br>
-- Groucho Marx<br>
.......<br></center>
</div></div>