in reply to Re: conditional regex
in thread conditional regex
Even though it might be considered a really, really emphatic variation of 'Yes', the solution of Re: conditional regex will accept something like 'Yeeessseeseeesssssess'. Wouldn't an approach like the following be better:
>perl -wMstrict -le "sub genAbbrevRegex{ my ($first, @rest) = split '', shift; my $re = ''; $re = qq{(?: $_ $re)?} for reverse @rest; return qr{ $first $re }xmsi; } print genAbbrevRegex( 'fred' );; my $r = genAbbrevRegex( 'Yes' );; print $r; print qq{'$_': }, m{ \A $r \z }xms ? 'ok' : 'not ok' for qw[ YES YE Y yes ye y ys es s e Yeeessseeseeesssssess];; " (?msix: f (?: r (?: e (?: d )?)?)? ) (?msix: Y (?: e (?: s )?)? ) 'YES': ok 'YE': ok 'Y': ok 'yes': ok 'ye': ok 'y': ok 'ys': not ok 'es': not ok 's': not ok 'e': not ok 'Yeeessseeseeesssssess': not ok
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: conditional regex
by BrowserUk (Patriarch) on Aug 22, 2010 at 15:23 UTC | |
by AnomalousMonk (Archbishop) on Aug 23, 2010 at 00:48 UTC | |
by BrowserUk (Patriarch) on Aug 23, 2010 at 05:13 UTC | |
by AnomalousMonk (Archbishop) on Aug 23, 2010 at 11:51 UTC |