I have tried:
/^([0-3][0-9])-(A-Za-z)-([0-1][0-9])?$/
and am not having success. What am I doing wrong?
Thank you folks!
I have tried:
/^([0-3][0-9])-(A-Za-z)-([0-1][0-9])?$/
and am not having success. What am I doing wrong?
Thank you folks!
Character classes are to be represented with square brackets.
/^([0-3][0-9])-([A-Za-z]+)-([0-1][0-9])?$/
/^([0-3][0-9])-([A-Z]{3})-([0-1][0-9])$/
EDIT :
/^(0[1-9]|[1-2][0-9]|3[0-1])-([A-Z]{3})-(0[1-9]|1[0-9])$/
Drop the ?. And you can drop the parens unless you're pulling sub-string matches:
/^([0-3][0-9])-([A-Z][A-Z][A-Z])-([0-1][0-9])$/
or
/^[0-3][0-9]-[A-Z][A-Z][A-Z]-[0-1][0-9]$/
To be overly pedantic, this regular expression matches it: 22-NOV-09
.
If what you want instead is to verify whether a given string is a validate date in a specific format, then I'd recommend using something like strptime
. For example:
#define _XOPEN_SOURCE
#include <time.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
struct tm tm;
char *p;
p = strptime(argv[1], "%d-%b-%y", &tm);
if (p && *p == '\0') {
printf("Matches\n");
return 0;
}
else {
printf("Didn't match\n");
return 1;
}
}
Python has datetime.datetime.strptime
, and Perl has POSIX::strptime
. I'm sure most other languages have access to this function, too.