开发者

What regular expression matches this pattern: 22-NOV-09

I have tried:

/^([0-3][0-9])-(A-Za-z)-([0-1][0-9])?$/

and am not having success. W开发者_如何学运维hat am I doing wrong?

Thank you folks!


/^([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])$/


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.


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


Character classes are to be represented with square brackets.

/^([0-3][0-9])-([A-Za-z]+)-([0-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]$/
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜