This may sound crazy but I have to do it. Here is a regex I am trying to
get to work.
$x =~ /(.*?);
(?{$lib_count = $^N;})
(.*?);(?{$auth_count = $^N;})
(.*?);(?{$book_count = $^N;})
((??{'(?:(?:.*?);(?:.*?);){'.$book_count.'}'}))
((??{'(?:(?:.*?);(?:.*?);(.*?);(?{$xcount =
$^N;})(?:(??{\'(?:.*?;){$xcount}\'}))){'.$auth_count.'}'})) # Problem
here... at the second (??{}) as it is nested inside another (??{})
((?:(?:(??{'.{'.4*$lib_count.'}'}))))(.*?);(.*)(?:;)?/x;
There are $xcount occurrences of a pattern withing $auth_count occurrences
of the enveloping pattern.
I need nested 'postponed' expressions in a regex but Perl does not like
it. This regex is generated by a program. And I need to do it in one
regex. Subroutine execution using (?{ }) is ok. But I am not sure how it
can construct and drop in regex at runtime.
I wonder if custom regex engine can come to help. But so far I have seen
only examples of overloaded operators.
__________________________________________
Ranga Nathan / CSG
Systems Programmer - Specialist; Technical Services;
BAX Global Inc. Irvine-California
Tel: 714-442-7591 Fax: 714-442-2840
Ranga Nathan <[EMAIL PROTECTED]>
Sent by: [EMAIL PROTECTED]
07/30/2005 09:36 PM
To
[email protected]
cc
Subject
Re: [Boston.pm] A regex question
I got it.
The (??{code}) replaces the result of the code as a regex and then
evaluates it on the fly. So,
my $x = "04abcdefghijklmnopqur";
$x =~ /^(.{2})((??{'.{' . 3*$1 . '}'}))(.*)/;
print "1 = $1\n 2 = $2 \n 3 = $3\n ";
worked.
BTW, my perl 5.8 regex documentation still says that (??{code}) is
experimental. I hope it is now a permanent feature :-)
Thanks all for the prompt response. That is the reason I posted to this
list!
__________________________________________
Ranga Nathan / CSG
Systems Programmer - Specialist; Technical Services;
BAX Global Inc. Irvine-California
Tel: 714-442-7591 Fax: 714-442-2840
Bob Rogers <[EMAIL PROTECTED]>
07/30/2005 09:09 PM
To
Ranga Nathan <[EMAIL PROTECTED]>
cc
[email protected]
Subject
[Boston.pm] A regex question
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
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm