<font face="courier new,monospace">I'm having yet another mental block w.r.t. capturing regexes...<br><br>Given a string to match, and a list of regexes<br>I need to process the captured data (if any), if the pattern matches.<br>
<br>So generically I do:<br><br>if (@capture = ($line =~ m/$regex/)) {<br> foreach @capture { ... }<br>} else {<br> no match<br>}<br><br>When the regex has some capturing defined, all is fine.<br>The foreach does its thing.<br>
<br>But when the regex DOESN'T have any captures<br>it @capture still contains a single value ... '1'<br>(The success/truth indication.)<br><br>How can I tell the difference between:<br>- a successful match with no captured data<br>
- a successful match with captured data<br>- an unsuccessful match<br><br>TIA<br>Fulko<br><br><br><br><br>my $line = 'this is stuff in here';<br>my @patterns = qw / stuff (stuff) /;<br>foreach $regex (@patterns) {<br>
my @capture = ();<br> print "regex is '$regex' ... ";<br> if (@capture = ($line =~ m/$regex/)) {<br> print "captured " . scalar @capture . " items \n";<br> my $i = 0;<br>
foreach my $item (@capture) {<br> print " capture[$i]: '$item'\n";<br> $i++;<br> }<br> } else {<br> print "nothing captured\n";<br> }<br>}<br>
<br>
</font>