开发者

Regular expression for any occurrence of a date of the form `MMM. DD, YYYY`

I'm refreshing my grep skills in preparation for a job I'm starting.

I want to do a grep for any occurrence of a date of the form MMM. DD, YYYY.

Exampl开发者_JAVA百科e: Sep. 12, 2007

The regular expression I came up with was:

grep "[[:alpha:]]\{3\}.[[:space:]][([[:digit:]])([[:digit:]]\{2\})],[[:space:]][[:digit:]]\{4\}" file

My logic: three letters; a period; a space; one digit OR two digits; a comma; a space; four digits.

It may be more complex than it needs to be but really I just want to see where I went wrong.


Perl regex might be a little easier to read and understand:

perl -ne 'print if /^\w\w\w\. \d\d, \d\d\d\d$/' somefile


You didn't escape the .s, and (more fatally) you didn't escape the parentheses so they were taken literally. (With grep you need to use \(...\) for grouping. egrep uses unescaped parentheses.)


Do you just want to see where you went wrong? Here:

[([[:digit:]])([[:digit:]]\{2\})]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜