Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

looking for a one liner that will be used to answer a yes/no question.

Given a string (which is a filename minus the extension) Answer yes/no to the question..

does the name end with one of two strings?
example: code needs to determine if the string ends with:
_FOO or _BAR (_FOO and _BAR are preset and never change)

Since its a filename anything can prefix:

dog_FOO = true my_dog_is_blue_BAR = true my_dog_FOO_FOO = true my_dog_FOO_is_blue = false _BAR= false --> important special case! 12345-(*65326523_BAR=true

Thanks in advance! (oh mighty Munks!)

Replies are listed 'Best First'.
Re: One Liner Regex Question
by broquaint (Abbot) on May 11, 2003 at 12:20 UTC
    my $re = qr< . _(?: FOO | BAR ) \z >x; print "$_: ", (/$re/ ? 'true' : 'false'), $/ for map quotemeta, qw/ dog_FOO my_dog_is_blue_BAR my_dog_FOO_FOO my_dog_FOO_is_blue _BAR 12345-(*65326523_BAR /; __output__ dog_FOO: true my_dog_is_blue_BAR: true my_dog_FOO_FOO: true my_dog_FOO_is_blue: false _BAR: false 12345\-\(\*65326523_BAR: true
    Check out perlre and YAPE::Regex::Explain for more info on the above regex.
    HTH

    _________
    broquaint

Re: One Liner Regex Question
by Juerd (Abbot) on May 11, 2003 at 13:24 UTC

    I'm at a disadvantage as I'm not using Perl

    This is Perl Monks, not Regex Monks. Regexes are *NOT* the same everywhere.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: One Liner Regex Question
by fruiture (Curate) on May 11, 2003 at 12:11 UTC
    m/._(FOO|BAR)$/ ? 'true' : 'false'

    See perlre, this shouldn't be too hard

    --
    http://fruiture.de
    A reply falls below the community's threshold of quality. You may see it by logging in.