in reply to Did regex match fail because of "end of string"?

I'm not sure if this is going to be of any use but you might be able to detect that the digits are at the end of the string rather than the 'b'. You could do this by making the match for 'b' conditional on whether you've got to the end so that the match doesn't actually fail if the 'b' isn't there. The code may do a better job of explaining what I mean.

#!/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
    Thank you for your input, but I'll try to avoid modifying the regexes because they are user input, and I don't want to deparse them.

    And I don't only want to detect end-of-string between \d+ and 'b', but also between 'a' and \d+ - which means that I'd had to add a closure between any two atoms in the regex - that's not a feasible option :(

Re^2: Did regex match fail because of "end of string"?
by ikegami (Patriarch) on Oct 16, 2007 at 21:06 UTC
    An input of "a" isn't flagged as an incomplete match.