in reply to conditional regex

Update: All '*' corrected to be '?' per follow ups.

but how to match s only if e is there, and match e only if s is there ?

I don't think anyone actually answered your question.

For proper abbreviations of 'yes', you can use m[^Y(?:E(?:S)?)?$]i.

That's a bit unweildy, but the repetitious nature means it is quite easy to generate a regex for any given word:

sub genAbbrevRegex{ my( $first, @rest ) = split '', shift; my $re = ''; $re = "(?:$_$re)?" for reverse @rest; return qr[^$first$re$]i; } print genAbbrevRegex( 'fred' );; (?i-xsm:^f(?:r(?:e(?:d)?)?)?$) $r = genAbbrevRegex( 'Yes' );; [0] Perl> print "$_: ", m[$r]? 'ok' : 'not ok' for qw[ YES YE Y yes ye + y YS ES S ys es s yees yess ];; YES: ok YE: ok Y: ok yes: ok ye: ok y: ok YS: not ok ES: not ok S: not ok ys: not ok es: not ok s: not ok yees: not ok yess: not ok

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.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: conditional regex
by JavaFan (Canon) on Aug 22, 2010 at 15:02 UTC
    Your pattern also matches 'YEESS', 'yeeeeeeeeeeeeeeeeeees', and 'YeEeEeEeSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS'.

    Probably not what you want.

    There's a much simpler solution. Reverse the subject and the pattern:

    for my $str (qw[YES YE Y yes ye y YS ES S ys es s YEESS YYYEEs]) { say "$str: ", "yes" =~ /^\Q$str/i ? "ok" : "not ok"; } __END__ YES: ok YE: ok Y: ok yes: ok ye: ok y: ok YS: not ok ES: not ok S: not ok ys: not ok es: not ok s: not ok YEESS: not ok YYYEEs: not ok
    Or, instead of the pattern match:
    say "ok" unless index "yes", lc $str;

      Indeed that is much simpler. I wish I'd thought of it :)

Re^2: conditional regex
by AnomalousMonk (Archbishop) on Aug 22, 2010 at 15:06 UTC

    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

      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.