coson
coson

Reputation: 8659

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

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 from 01-12 but when I change the regex to:

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

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

Upvotes: 27

Views: 73962

Answers (10)

James Kane
James Kane

Reputation: 113

Isnt this like more readable and elegant solution for Year validation? [0-2][0-9]{3,3} it matches everything from year 0000 to 2999

Upvotes: 0

Eolia
Eolia

Reputation: 237

To test month / year :

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

Upvotes: 0

Justin Sato
Justin Sato

Reputation: 574

"^(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.

Upvotes: 1

Geobio Boo
Geobio Boo

Reputation: 1354

(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

Upvotes: 3

PSP
PSP

Reputation: 1

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

Upvotes: -2

HerbN
HerbN

Reputation: 1509

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);

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342363

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:

Upvotes: -1

Greg Hewgill
Greg Hewgill

Reputation: 993085

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.

Upvotes: 22

MBO
MBO

Reputation: 30995

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

Upvotes: 5

YOU
YOU

Reputation: 123831

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

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

it will not match 13, 14 ....

Upvotes: 61

Related Questions