It is certainly possible to write a function that accepts information about the values you want to set for the variables, that returns a regexp to set them up in that way.

As you supposed, the parens are counted at regexp compile time, so it is not possible to embed all the logic in a regexp without fixing the paren count in advance.

$^N will be the first match capture that reaches the maximum value of @+[1..], which can be emulated by constructing the regexp to nest the captures that end at this point.

You may need to allow for some captures being unset, as in "ac" =~ /(a)?(b)?(c)?/.

For the simple case where the nesting is natural, most efficient would be to forget the lookaheads and just construct a nesting of dots and parens, along with a simple negative lookahead for unset parens ((?!))?. I think something like the below would do it, but I have not tested it exhaustively:

my $s = "abcdefghi"; my @a = ([ 0, 3 ], [ 0, 1 ], [ 1, 2 ], [ 2, 1 ]); my $qr = matcher(\@a); match($s, $qr); sub matcher { my $array = shift; my(%pos, @undef); my($start, $length) = @{ $array->[0] }; for (1 .. $#$array) { my($pos, $width) = ($array->[$_][0], $array->[$_][1]); if (defined $pos) { $pos{$pos} .= '('; $pos{$pos + $width} .= ')'; } else { push @undef, $_; } } my $qr = '.' x ($start + $length); for (sort { $b <=> $a } keys %pos) { substr $qr, $_, 0, $pos{$_}; } for (reverse @undef) { $qr =~ s/((.*?\(){$_})/$1((?!))?/; } $qr =~ s{(.{$start})}{(?<=^$1)} if $start; qr/$qr/; }

Extending this to add lookaheads for captures that are not naturally nested is left as an exercise for the reader. :)

With respect to your title, note that it is possible to plug in an alternate regexp engine - this is how use re 'debug' is implemented - but I'm not aware that anyone has ever taken advantage of this, nor do I imagine there is any documentation of how you might do so.

Hugo


In reply to Re: Plug for an alternate regex engine by hv
in thread Plug for an alternate regex engine by bsb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.