regular expression to represent time in a special format
I have many files containing date extensions. For example, today's file would be named
filename.20101118
and format is yyyymmdd. I开发者_如何学编程 would like to list/grep/etc for all dated files.
Do you mean just something like this ?
/filename\.[12][90][0-9][0-9][01][0-9][0-3][0-9]/
Note that this matches non-valid dates, too.
or, more generalized:
/[a-zA-Z0-9_]+\.[12][90][0-9][0-9][01][0-9][0-3][0-9]/
instead of [a-zA-Z0-9_]
you can use whatever characters your filename can contain.
Try this if you're not looking to do any validation.
\.(\d{8})
Note: I have assumed that your "filename" will be only alphanumeric (ASCII).
My best friend RegexBuddy says:
[a-zA-Z0-9]+?\.(19|20)[0-9]{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])
Matches
filename123.20101118
FOOBAR123.19961212
Does not match
FOOBAR.88881201
foobar.20103512
filename.20101235
Disclaimer: I am just a happy user and have no affiliation to RB
Explanation
[a-zA-Z0-9]+?\.(19|20)[0-9]{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])
Options: case insensitive
Match a single character present in the list below «[a-zA-Z0-9]+?»
Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
A character in the range between “a” and “z” «a-z»
A character in the range between “A” and “Z” «A-Z»
A character in the range between “0” and “9” «0-9»
Match the character “.” literally «\.»
Match the regular expression below and capture its match into backreference number 1 «(19|20)»
Match either the regular expression below (attempting the next alternative only if this one fails) «19»
Match the characters “19” literally «19»
Or match regular expression number 2 below (the entire group fails if this one fails to match) «20»
Match the characters “20” literally «20»
Match a single character in the range between “0” and “9” «[0-9]{2}»
Exactly 2 times «{2}»
Match the regular expression below and capture its match into backreference number 2 «(0[1-9]|1[012])»
Match either the regular expression below (attempting the next alternative only if this one fails) «0[1-9]»
Match the character “0” literally «0»
Match a single character in the range between “1” and “9” «[1-9]»
Or match regular expression number 2 below (the entire group fails if this one fails to match) «1[012]»
Match the character “1” literally «1»
Match a single character present in the list “012” «[012]»
Match the regular expression below and capture its match into backreference number 3 «(0[1-9]|[12][0-9]|3[01])»
Match either the regular expression below (attempting the next alternative only if this one fails) «0[1-9]»
Match the character “0” literally «0»
Match a single character in the range between “1” and “9” «[1-9]»
Or match regular expression number 2 below (attempting the next alternative only if this one fails) «[12][0-9]»
Match a single character present in the list “12” «[12]»
Match a single character in the range between “0” and “9” «[0-9]»
Or match regular expression number 3 below (the entire group fails if this one fails to match) «3[01]»
Match the character “3” literally «3»
Match a single character present in the list “01” «[01]»
Created with RegexBuddy
Simplest:
find -regex ".*/.*\.[0-9]+$"
Better:
find -regextype posix-basic -regex ".*/.*\.[0-9]\{8\}$"
Best?:
find -regextype posix-extended -regex ".*/.*\.[0-9]{4}([0][1-9]|1[012])(0[1-9]|[12][0-9]|3[01])$"
The last one will find any year (we don't want a repeat of Y2K, now, do we?). and only months in the range 01-12 and days in the range 01-31. Of course, it doesn't validate for the correct number of days in a month or for leap days.
精彩评论