【问题标题】:match date from list of strings returns empty list字符串列表中的匹配日期返回空列表
【发布时间】:2021-10-17 19:31:19
【问题描述】:

我相信我的问题很简单,但我无法弄清楚为什么它不起作用。 我有物品清单,类似于:

['location_1/2021-05-12.tiff','location_1/2021-05-17.tiff','location_1/2021-05-19.tiff',
'location_1/2021-05-24.tiff','location_1/2021-05-28.tiff','location_1/2021-05-30.tiff'...]

我想获取仅包含日期的新列表,如下所示:

['2021-05-12','2021-05-17','2021-05-19','021-05-24','2021-05-28','021-05-30']

我已经尝试使用正则表达式来做到这一点:

dates_Values = list(filter(lambda x: match('([0-9]{4}-[0-9]{2}-[0-9]{2})', x), my_list))

但是,它返回空列表。

我怎样才能得到只有日期的正确列表?

【问题讨论】:

  • [s.split("/")[1].split(".")[0] for s in your_list] ?
  • @PatrickArtner 成功了。谢谢!
  • 你需要search() 而不是match()。

标签: python regex list date


【解决方案1】:

您可以简单地遍历每个元素并使用以下方法搜索日期格式 r'\d+-\d+-\d+' 模式并将这些元素附加到新列表中。

import re

my_list = ['location_1/2021-05-12.tiff','location_1/2021-05-17.tiff','location_1/2021-05-19.tiff', 'location_1/2021-05-24.tiff','location_1/2021-05-28.tiff']

new_list = []

for element in my_list:
    result = re.findall(r'\d+\-\d+\-\d+', element)
    if result:
        new_list.append(result[0])

print(new_list)

【讨论】:

  • 这里的变量结果将采用列表格式,因此在追加时您必须使用结果的索引 0 或 -1。
【解决方案2】:
import re
t = ['location_1/2021-05-12.tiff','location_1/2021-05-17.tiff','location_1/2021-05-19.tiff','location_1/2021-05-24.tiff','location_1/2021-05-28.tiff','location_1/2021-05-30.tiff']
dates_values = []
search_pattern = re.compile('([0-9]{4}-[0-9]{2}-[0-9]{2})')
for s in t:
    matchResult = search_pattern.search(s)
    if matchResult:
        print(matchResult.group(1))
        dates_values.append(matchResult.group(1))
print(dates_values)

【讨论】:

    【解决方案3】:
    import re 
    strs = ['location_1/2021-05-12.tiff','location_1/2021-05-17.tiff','location_1/2021-05-19.tiff','location_1/2021-05-24.tiff','location_1/2021-05-28.tiff','location_1/2021-05-30.tiff']
    
    dates_Values = [ re.search(r'([0-9]{4}-[0-9]{2}-[0-9]{2})', x).group(1) for x in strs]
    

    输出

    ['2021-05-12', '2021-05-17', '2021-05-19', '2021-05-24', '2021-05-28', '2021-05-30']
    

    【讨论】:

    • @Reut 如果你过滤,那么你需要一个额外的步骤
    【解决方案4】:

    // Hii, Check this out its working fine 
    
    let arr = [
        "location_1/2021-05-12.tiff",
        "location_1/2021-05-17.tiff",
        "location_1/2021-05-19.tiff",
        "location_1/2021-05-24.tiff",
        "location_1/2021-05-28.tiff",
        "location_1/2021-05-30.tiff",
      ];
      
    const newArr = arr.map((item,i) => {
      return  item.match("[0-9]{4}-[0-9]{2}-[0-9]{2}");
      });
      
    
      let dates_Values = []
      dates_Values = newArr.map((item,i,arr)=> {
         return item[0]
      })
    
      console.log(dates_Values);

    【讨论】:

      【解决方案5】:

      我会使用 non-regex 解决方案,将str.rsplit 与maxsplit 参数结合起来用于列表理解中的上述数据:

      >>> [i.rsplit('.', maxsplit=1)[0].rsplit('/', maxsplit=1)[-1] for i in my_list]
      ['2021-05-12', '2021-05-17', '2021-05-19', '2021-05-24', '2021-05-28', '2021-05-30']
      

      【讨论】:

        【解决方案6】:

        您的正则表达式永远不会匹配,因为没有字符串以日期开头:

        re.match(模式、字符串、标志=0)
        如果字符串开头的零个或多个字符与正则表达式模式匹配,则返回相应的匹配对象。如果字符串与模式不匹配,则返回 None;请注意,这与零长度匹配不同。

        (https://docs.python.org/3/library/re.html#re.match)


        你可以这样拆分:

        l = ['location_1/2021-05-12.tiff','location_1/2021-05-17.tiff',
             'location_1/2021-05-19.tiff','location_1/2021-05-24.tiff',
             'location_1/2021-05-28.tiff','location_1/2021-05-30.tiff']
        
        print([s.split("/")[1].split(".")[0] for s in l])
        

        输出:

        ['2021-05-12', '2021-05-17', '2021-05-19', '2021-05-24', '2021-05-28', '2021-05-30']
        

        或者保留正则表达式的方法:

        import re
        
        l = ['location_1/2021-05-12.tiff','location_1/2021-05-17.tiff',
             'location_1/2021-05-19.tiff','location_1/2021-05-24.tiff',
             'location_1/2021-05-28.tiff','location_1/2021-05-30.tiff']
        
        
        found = re.findall(r"/(\d{4}-\d{2}-\d{2}).", str.join(" ", l))
        print(found)
        

        输出:

        ['2021-05-12', '2021-05-17', '2021-05-19', '2021-05-24', '2021-05-28', '2021-05-30']
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-07
          • 2021-07-15
          • 1970-01-01
          • 2023-01-20
          • 2017-01-09
          • 2023-03-28
          • 2019-02-18
          • 2018-10-15
          相关资源
          最近更新 更多