tags:

views:

85

answers:

5

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!

+1  A: 
/^([0-3][0-9])-([A-Za-z]+)-([0-1][0-9])$/
S.Mark
+1  A: 

Character classes are to be represented with square brackets.

/^([0-3][0-9])-([A-Za-z]+)-([0-1][0-9])?$/
Alan Haggai Alavi
+4  A: 
/^([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])$/

fastcodejava
You should say what the OP was doing wrong...
bobbymcr
A: 

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]$/
NVRAM
+2  A: 

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.

Zach Hirsch