in reply to Re^4: Variable matching on a regex
in thread Variable matching on a regex

I think you're confused on a number of points. First off, you can't have a variable number of regex matches if you don't use /g. So if you want to go beyond hard-coding your regexes, you need to get over it.

Second, if you want to name your variables $d1, $d2, etc, you're just contradicting yourself again. You're asking how to know how many variables to create before you know how many matches you'll have. I suppose you could write a bunch of code to eval a string, but using an array is so simple.

Third, /g can be used in loop constructs, which allow you to examine your data as you're parsing it. Very simple parsers are very easy to write. For example:

$s = 'abc 1 23 do 456 re 789 me 0123 456 2 23 456 789 0123 456'; push @results, ("This has " . length($1) . " digits: $1") while $s =~ /(\d+)/g; print "$_\n" for @results; # Prints: # This has 1 digits: 1 # This has 2 digits: 23 # This has 3 digits: 456 # This has 3 digits: 789, etc.
Or you can look for more complicated patterns:
$s = '1 23 456 789 0123 456 2 23 456 789 0123 456'; push @results, ("This looks like a word: $1") while $s =~ /((?:\b\d{1, +2}\s+)+\b\d{3,})/g; print "$_\n" for @results; # Prints: # This looks like a word: 1 23 456 # This looks like a word: 2 23 456

It's not really clear from what you've written what you're trying to do. But capturing a varying number of results is not hard if you get over the idea of using named scalars.

--marmot