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

    Of course, it should have been 0 or 1 not 0 or many.

    I don't see any advantage in using /msx, nor \A & \z for this?

    (Nor qq{} when simple quotes do, but that a personal beef. PBP has a lot to answer for :)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I don't see any advantage in using /msx ...

      A matter of personal taste. I find the rationale of PBP compelling with regard to 'standardizing' the behavior of the  . ^ $ metacharacters, and always using 'extended' formatting for readability.

      nor \A & \z ...

      Another personal preference. When generating a regex like the one used in the example, I prefer to postpone consideration of certain questions (Must the pattern be at the start of the string? May there be whitespace before it?) to the point at which the regex is actually used. Specifically WRT  \A \z I again accept the rationale of PBP for their use to anchor absolute start/end of a string (and they're almost imperative if  /m is used).

      [n]or qq{} ...

      I use  qq{} because I run my example code under Windoze shell and I'm trying to avoid irruptions of backwhacks. If no interpolation is involved, I'll typically use '' single-quotes.

        I can see some advantages to those in hand-written regex, but none at all for these generated regex.

        I can't imagine an application for a multi-line response to a single prompt, never mind accepting an abbreviated multi-line response.

        I like consistency in coding style, and rules-of-thumb have their place, but I think you have to apply context to decisions.

        Keeping two hands on the steering wheel is good general advice, but applied dogmatically, you'd never adjust a mirror or sun-visor; turn the radio on or off; shut the sat-nav up; scratch your ear...


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.