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

What is wrong with the code below? I'm trying to match the following:
ep_svr_<any number/s>_<3 letters>_<3 letters>_<any characters> #!/usr/bin/perl if ($ARGV[0] =~ m/^ep_svr_\d+_[A-Za-z]{3}_[A-Za-z]{3}_\.+\z/x) { print "valid label: $ARGV[0]\n"; } else { print "invalid label: $ARGV[0]\n"; };

Replies are listed 'Best First'.
Re: Problem with regex
by robartes (Priest) on Feb 05, 2003 at 07:42 UTC
    You escaped the 'any character' period, so it becomes a literal period. Try this instead:
    #!/usr/bin/perl if ($ARGV[0] =~ m/^ep_svr_\d+_[A-Za-z]{3}_[A-Za-z]{3}_.+\z/x) { print "valid label: $ARGV[0]\n"; } else { print "invalid label: $ARGV[0]\n"; }; __END__ $ epsrv.pl ep_svr_12_abc_abc_theend valid label: ep_svr_12_abc_abc_theend
    So, at least for my limited test case, it works.

    CU
    Robartes-

Re: Patterns
by insensate (Hermit) on Feb 05, 2003 at 07:00 UTC
    From the start I can see that you've included _ in your character classes when I think you want it to precede the class...
    /^ep_svr_\d+_\w{3}_\w{3}_.+$/;
    Update: The code tags weren't in when i diagnosed the problem...but the code should still fit the bill ;^)