How can I use a regular expression to validate month input?

前端 未结 9 837
孤街浪徒
孤街浪徒 2020-12-11 00:31

I am setting up this example Perl snippet to validate for months in a date:

Some scenarios I want to accept are:

MM M

#!/usr/bin/perl
use str         


        
9条回答
  •  悲&欢浪女
    2020-12-11 01:15

    Don't use regular expressions.

    Perl has the ability to automatically evaluate as a number or a string based on context. 01-09 will evaluate to 1-9 in the numeric context. So, you can simply check for a value:

    print "Enter in a month: ";
    chomp($pattern = );
    # We only want to print if the pattern matches
    print "Pattern matches\n" if ($pattern < 13 && $pattern > 0);
    

提交回复
热议问题