From: Ranga Nathan <[EMAIL PROTECTED]>
Date: Sat, 30 Jul 2005 19:55:38 -0700
I need to parse a string containing a repeating group. There is a count
that holds the repeat count. Only when I extract the count will I know how
many repeats there are . . .
I think the (??{code}) could be used. But I am not sure how. I tried a
couple of permutations with no result. Like this for example:
my $x = "04abcdefghijklmnopqur";
$x =~ /^(.{2})(.{??{3*\1}})(.*)/; # get the count and extract that many
groups of 3 chars. I am using the \1 as a multiplier.
print "1 = $1\n 2 = $2 \n 3 = $3\n";
It looks like you are missing parens around "??{3*\1}", but this still
doesn't work. The problem is that the ".{" before it is not recognized
as an "{m,n}" construct because it is incomplete to the RE parser.
Perhaps you were thinking of something like:
(??{".{".(3*\1)."}"})
in order to produce ".{12}" as part of the regexp. But this doesn't
seem to work either. I suspect this is because "\1" isn't meaningful to
the code that runs to generate this. So you could use a series of
anchored matches instead:
[EMAIL PROTECTED]> perl -e '$_ = "04abcdefghijklmnopqur";
/^.{2}/g && ($re = "\\G(.{".(3*$&)."})(.*)")
&& /$re/g && print "1 = $1\n 2 = $2 \n";
print "$re\n";'
1 = abcdefghijkl
2 = mnopqur
\G(.{12})(.*)
[EMAIL PROTECTED]>
But this is still extremely ugly. I think if I were you, I would give
up on dynamic regexps and just use unpack. If you do find something
less ugly than this, I would be curious to see it.
-- Bob Rogers
http://rgrjr.dyndns.org/
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm