in reply to Re: regex matching specific strings.
in thread regex matching specific strings.

$var =~ /^(?:abc)|(?:def)|(?:ghi)$/; is wrong. It matches strings that

You want

$var =~ /^(?:abc|def|ghi)\z/;

Replies are listed 'Best First'.
Re^3: regex matching specific strings.
by markkawika (Monk) on Jul 22, 2009 at 23:50 UTC

    My only tiny quibble with that regex, correct as it is, is that using ^ in combination with \z might be confusing to a future reader. I'd suggest:

    $var =~ /\A(?:abc|def|ghi)\z/;

    Which is technically exactly the same as ikegami's suggestion, but points out to the reader "Hey I'm using less-common techniques in this regex" right at the beginning with that \A.

      That is funny, because every single character in a regex could be critical, so unless you know exact meaning of every single character, you should be very very careful :)