in reply to Did regex match fail because of "end of string"?
#!/usr/bin/perl -l # use strict; use warnings; my @strings = qw{ a123ga123b a123bdfda123 a123effa123 a123 d663h }; my $len; my $rsLen = \$len; my $rxCondMatch = qr {(?x) a (\d+) (?{ print q{digits at end of string} if pos() == $$rsLen }) (??{ if ( pos() != $$rsLen ) { q{b} } }) }; foreach my $string ( @strings ) { $len = length $string; print $string; print q{Match} if $string =~ $rxCondMatch; print q{-} x 20; }
Here's the output.
a123ga123b Match -------------------- a123bdfda123 Match -------------------- a123effa123 digits at end of string Match -------------------- a123 digits at end of string Match -------------------- d663h --------------------
I hope this can be of use to you.
Cheers,
JohnGG
Update: Corrected error in code, testing against $len instead of $$rsLen in (?{ ... }) block
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Did regex match fail because of "end of string"?
by moritz (Cardinal) on Oct 16, 2007 at 21:11 UTC | |
|
Re^2: Did regex match fail because of "end of string"?
by ikegami (Patriarch) on Oct 16, 2007 at 21:06 UTC |