我有很多包含日期扩展名的文件。例如,今天的文件名为
filename.20101118
格式为yyyymmdd。我想列出所有日期文件的/ grep / etc。
答案 0 :(得分:1)
你的意思是这样的吗?
/filename\.[12][90][0-9][0-9][01][0-9][0-3][0-9]/
请注意,这也符合无效日期。
或更广义:
/[a-zA-Z0-9_]+\.[12][90][0-9][0-9][01][0-9][0-3][0-9]/
而不是[a-zA-Z0-9_]
,您可以使用文件名可以包含的任何字符。
答案 1 :(得分:1)
如果您不打算进行任何验证,请尝试此操作。
\.(\d{8})
答案 2 :(得分:1)
注意:我假设您的“文件名”只是字母数字(ASCII)。
我最好的朋友RegexBuddy说:
[a-zA-Z0-9]+?\.(19|20)[0-9]{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])
<强>匹配强>
filename123.20101118
FOOBAR123.19961212
不匹配
FOOBAR.88881201
foobar.20103512
filename.20101235
免责声明:我只是一个快乐的用户,与RB没有任何关系
<强>解释强>
[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
答案 3 :(得分:0)
最简单的:
find -regex ".*/.*\.[0-9]+$"
更好:
find -regextype posix-basic -regex ".*/.*\.[0-9]\{8\}$"
最佳:
find -regextype posix-extended -regex ".*/.*\.[0-9]{4}([0][1-9]|1[012])(0[1-9]|[12][0-9]|3[01])$"
最后一个会找到任何年(我们不想重复 Y2K ,现在,我们呢?)。并且只有01-12范围内的月份和01-31范围内的天数。当然,它不会验证一个月或闰日中的正确天数。