(?{ ... }) puzzlement

From: J Krugman (jkrugman345_at_yahbitoo.com)
Date: 05/31/04


Date: Mon, 31 May 2004 12:34:32 +0000 (UTC)


In an attempt to find a single regexp that would succeed if three
different sub-regexps matched in any order (see why in the thread
called '"Commutative" regexps'), I started playing with (?{...})-type
regexps. As warm-up, I tried this:

     1 use strict;
     2 use re 'eval';
     3
     4 my @re0 = qw(abc pqr xyz);
     5 my @seen = (undef) x @re0;
     6 my @re = map sprintf('%s(?{ $seen[%d] ||= "@-" })',
     7 $re0[$_], $_),
     8 0..$#re0;
     9 my $re = eval "qr/@{[join('|', @re)]}/";
    10
    11 #0 1 2
    12 #01234567890123456789012345
    13 '__pqr____xyz__pqr___abc___' =~ /(($re).*?)*/;
    14
    15 print "\$seen[$_] = $seen[$_]\n" for (0..$#seen);
    16
    17 __END__

    $seen[0] =
    $seen[1] =
    $seen[2] =

If I change line 13 to

    13 '__pqr____xyz__pqr___abc___' =~ /(($re).*?)*(?!)/;

The output I get changes to

    $seen[0] = 2 14 14
    $seen[1] = 2
    $seen[2] = 2 2 2

I find both results completely puzzling. I realize that ?{ ... }
is a highly experimental feature, but if anyone can explain to me
what's going on I'd very much appreciate it.

TIA,

jill

P.S. Unrelated regexp question: if I have a string or regexp in
a variable $x, and I want to use this variable to write a regexp
corresponding to 5 repeats of the contents of $x, how do I write
it? If I wrote /$x{5}/, it would be interpreted by perl as attempting
to access the value corresponding to key '5' in the hash %x.

-- 
To  s&e^n]d  me  m~a}i]l  r%e*m?o\v[e  bit from my a|d)d:r{e:s]s.


Relevant Pages

  • Re: beginners question
    ... I was playing around with regexp and got stuck with this: ... basically i want to strip off the last "String" and whatever that comes after that. ...
    (comp.lang.perl.misc)
  • Re: beginners question
    ... Ala Qumsieh wrote: ... I was playing around with regexp and got stuck with this: ...
    (comp.lang.perl.misc)
  • Re: Regexp
    ... I saw the response on google over 30 minutes ago. ... have been playing with it and have found a small difficulty. ... place a variable into the reg exp as you may have seen in my formula. ... How do I get the evaluated variable into the regexp? ...
    (comp.lang.javascript)
  • Re: Great SWT Program
    ... In a regexp, * means zero or more repeats of the preceding pattern. ...
    (comp.lang.java.programmer)
  • Re: Great SWT Program
    ... In a regexp, * means zero or more repeats of the preceding pattern. ...
    (comp.lang.java.programmer)