(?{ ... }) puzzlement
From: J Krugman (jkrugman345_at_yahbitoo.com)
Date: 05/31/04
- Next message: dave h: "Re: Cookies"
- Previous message: Alan J. Flavell: "Re: Printing lines out of a text file"
- Next in thread: Ben Morrow: "Re: (?{ ... }) puzzlement"
- Reply: Ben Morrow: "Re: (?{ ... }) puzzlement"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: dave h: "Re: Cookies"
- Previous message: Alan J. Flavell: "Re: Printing lines out of a text file"
- Next in thread: Ben Morrow: "Re: (?{ ... }) puzzlement"
- Reply: Ben Morrow: "Re: (?{ ... }) puzzlement"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|