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\})]
精彩评论