【问题标题】:regular expression to represent time in a special format以特殊格式表示时间的正则表达式
【发布时间】:2010-11-19 13:15:03
【问题描述】:

我有许多包含日期扩展名的文件。例如,今天的文件将被命名为

文件名.20101118

格式为 yyyymmdd。我想为所有过时的文件列出/grep/etc。

【问题讨论】:

    标签: regex bash search grep


    【解决方案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_]

    【讨论】:

      【解决方案2】:

      如果您不想进行任何验证,请尝试此操作。

      \.(\d{8})
      

      【讨论】:

        【解决方案3】:

        注意:我假设您的“文件名”只是字母数字 (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
        

        【讨论】:

          【解决方案4】:

          最简单的:

          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])$"
          

          最后一个会找到 any 年(我们不想重复 Y2K,现在,是吗?)。并且只有 01-12 范围内的月份和 01-31 范围内的天。当然,它不会验证一个月中的正确天数或闰日。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多