开发者

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

开发者 https://www.devze.com 2022-12-17 08:33 出处:网络
I am setting up this example Perl snippet to validate for months in a date: Some scenarios I want to accept are:

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 strict;
use warnings;

my $pattern;
my $month = "(0[1-9]|1[012])";
my $day = "(0[1-9]|[12]\d|3[01])";

system("cls");

do {

    print "Enter in a month: ";
    chomp($pattern = <STDIN>);

    # We only want to print if the pattern matches
    print "Pattern matches\n" if ($pattern =~ /$month/);


} while ($pattern ne "Q");

When I run this, it correctly filters fro开发者_开发知识库m 01-12 but when I change the regex to:

$month = "(0?[1-9]|1[012])";

then the regex allows 13, 14, etc... what gives?


If you really like to use regex, you need to put ^ and $, like

"^(0?[1-9]|1[012])$"

it will not match 13, 14 ....


You shouldn't use a regular expression to do numeric range validation. The regular expression you want is:

/^(\d+)$/

Then,

if ($1 >= 1 && $1 <= 12) {
    # valid month
}

This is much easier to read than any regular expression to validate a numeric range.

As an aside, Perl evaluates regular expressions by searching within the target for a matching expression. So:

/(0[1-9]|1[012])/

searches for a 0 followed by 1 to 9, or a 1 followed by 0, 1, or 2. This would match "202" for example, and many other numbers. On the other hand:

/(0?[1-9]|1[012])/

searches for an optional 0 1 to 9, or a 1 followed by 0, 1, or 2. So "13" matches here because it contains a 1, matching the first half of the regex. To make your regular expressions work as you expect,

/^(0?[1-9]|1[012])$/

The ^ and $ anchor the search to the start and end of the string, respectively.


To give you hint - month number "120" also matches in your version :-)

Change:

my $month = "(0[1-9]|1[012])";

to

my $month = /^(0[1-9]|1[012])$/;

and then play more with it


(0[1-9]|1[012])

the parens are so you can use this inside another block, for example if matching for an entire yyyy-MM-dd date format

source: http://www.regular-expressions.info/dates.html


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 = <STDIN>);
# We only want to print if the pattern matches
print "Pattern matches\n" if ($pattern < 13 && $pattern > 0);


"^(1[012]|0?[1-9])$" would be better because regular expression is assessed first one first. Let's say you want to match '12' and you write "^(0?[1-9]|1[012])$", then '1' will be picked because 0?[1-9] is taken first.


here's one way

while(1){
    print "Enter in a month: ";
    $pattern = <STDIN>;
    chomp($pattern);
    if ($pattern =~ /^(Q|q)$/ ){last;}
    if ($pattern =~ /^[0-9]$/ || $pattern =~ /^[0-9][12]$/ ) {
        print "Pattern matches\n";
    }else{
        print "try again\n";
    }
}

output

$ perl perl.pl
Enter in a month: 01
Pattern matches
Enter in a month: 000
try again
Enter in a month: 12
Pattern matches
Enter in a month: 00
try again
Enter in a month: 02
Pattern matches
Enter in a month: 13
try again
Enter in a month:


To test month / year :

^(0?[1-9]|1[012])\/([2-9][0-9)]{3})$


[^d>12|d<0] OR ^[d>12|d<0]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号