【问题标题】:Python/Regex - How to extract date from filename using regular expression?Python/Regex - 如何使用正则表达式从文件名中提取日期?
【发布时间】:2011-10-11 15:38:17
【问题描述】:

我需要使用 python 从文件名中提取日期。日期格式如下:

month-day-year.somefileextension

例子:

10-12-2011.zip
somedatabase-10-04-2011.sql.tar.gz

最好的提取方法是使用正则表达式?

我有一些代码:

import re
m = re.search('(?<=-)\w+', 'derer-10-12-2001.zip')
print m.group(0)

代码将打印“10”。关于如何打印日期的一些线索?

最好的问候,

【问题讨论】:

    标签: python regex


    【解决方案1】:

    假设日期始终采用以下格式:[MM]-[DD]-[YYYY]。

    re.search("([0-9]{2}\-[0-9]{2}\-[0-9]{4})", fileName)
    

    【讨论】:

    • 请注意,为了使用它的输出,您将收到一个返回的列表:result = re.search("([0-9]{2}\-[0-9]{2}\-[0-9]{4})", fileName) result[0] # 会给您想要的东西
    • 添加到上面的 [0-9]{2,4}/[0-9]{2}/[0-9]{2,4} 将匹配 '2017/02/23 ' 和 '2017 年 2 月 23 日' 。同样,[0-9]{2,4}\-[0-9]{2}\-[0-9]{2,4} 将匹配 2017-02-23 和 23-02-2017。
    【解决方案2】:

    您想使用capture group

    m = re.search('\b(\d{2}-\d{2}-\d{4})\.', 'derer-10-12-2001.zip')
    print m.group(1)
    

    应该打印10-12-2001

    您可以使用更简洁的正则表达式,但确保它前面是 - 和后面是 . 可以提供一些最低限度的保护,以防止与时髦的文件名或不应该出现的格式错误的文件名的双重匹配完全匹配。

    编辑:我将最初的 - 替换为 \b,它匹配字母数字和非字母数字之间的任何边界。这样它将匹配日期之前是否有连字符或字符串的开头。

    【讨论】:

    • 如果文件名以日期开头(如第一个示例),则您的正则表达式不匹配。解决此问题的一种方法是将模式中的初始连字符替换为 (?:^|-)
    • 嗯,你是对的。我什至没有注意到那个例子。立即编辑。
    【解决方案3】:

    我认为您可以使用 re.split 提取日期,如下所示

    $ ipython
    
    In [1]: import re
    
    In [2]: input_file = '10-12-2011.zip'
    
    In [3]: file_split = re.split('(\d{2}-\d{2}-\d{4})', input_file, 1)
    
    In [4]: file_split
    Out[4]: ['', '10-12-2011', '.zip']
    
    In [5]: file_split[1]
    Out[5]: '10-12-2011'
    
    In [6]: input_file = 'somedatabase-10-04-2011.sql.tar.gz'
    
    In [7]: file_split = re.split('(\d{2}-\d{2}-\d{4})', input_file, 1)
    
    In [8]: file_split
    Out[8]: ['somedatabase-', '10-04-2011', '.sql.tar.gz']
    
    In [9]: file_split[1]
    Out[9]: '10-04-2011'
    

    我使用 Python 3.6.6、IPython 5.3.0 运行测试

    【讨论】:

      【解决方案4】:

      您输入的\w+ 匹配连字符后的一个或多个单词字符,因此这是预期的结果。您想要做的是在任一侧使用环视,匹配出现在第一个连字符和句点之间的数字和连字符:

      re.search(r'(?&lt;=-)[\d-]+(?=\.)', name).group(0)

      【讨论】:

        【解决方案5】:
        **This is simple method to find date from text file in python**
        import os
        import re
        file='rain.txt' #name of the file
        if(os.path.isfile(file)): #cheak if file exists or not
            with open(file,'r') as i:
                for j in i: #we will travarse line by line in file 
                    try:
                        match=re.search(r'\d{2}-\d{2}-\d{4}',j) #regular expression for date
                        print(match.group()) #print date if match is found
                    except AttributeError: 
                        pass
        else:
            print("file does not exist")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-31
          • 2019-07-17
          • 1970-01-01
          • 1970-01-01
          • 2017-05-13
          相关资源
          最近更新 更多