7stud has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
If I use the construct (?(condition)yes-pattern) in a regex, and the condition is the execute-perl-code construct (?{1}), i.e. the condition is always true, the output is as expected:
my $str = 'bxAybz'; while ( $str =~ /(?(?{1})(b[xyz]))/g ) { say 'yes'; say $1; } --output:-- yes bx yes bz
But when I use the condition (?{pos() % 2 == 0}), I expect the same output, but I don't get it:
my $str = 'bxAybz'; while ( $str =~ /(?(?{pos() % 2 == 0})(b[xyz]))/g ) { say 'yes'; say $1; } --output:-- yes bx yes Use of uninitialized value $1 in say at 2.pl line 9. yes bz
Three matches?
Also, I notice the x modifier doesn't work with a conditional regex:
That feels like a bug.my $str = 'bxAybz'; while ( $str =~ / (? (?{1}) (b[xyz]) ) /gx ) { say 'yes'; say $1; } --output:-- Sequence (? ...) not recognized in regex; marked by <-- HERE in m/ (? <-- HERE (?{1}) (b[xyz]) ) / at 2.pl line 12.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Conditional regex
by Anonymous Monk on Feb 17, 2013 at 18:35 UTC | |
by AnomalousMonk (Archbishop) on Feb 17, 2013 at 19:30 UTC | |
by Anonymous Monk on Feb 17, 2013 at 18:49 UTC |