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
"^(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.
If you really like to use regex, you need to put ^ and $, like
"^(0?[1-9]|1[012])$"
it will not match 13, 14 ....
[^d>12|d<0] OR ^[d>12|d<0]
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
To test month
/ year
:
^(0?[1-9]|1[012])\/([2-9][0-9)]{3})$