如何使用正则表达式将一个列表中的字符串元素与另一个列表相匹配?

时间:2018-12-21 05:31:20

标签: python regex python-3.x

我创建了一个日期为天的列表。以及员工分别登录的时间。我想找出员工在哪几天没有记录任何表明他/她休假的时间。

pattern = ["Timings: "]
timingData = ['Day: 1.0 Timings: 09:52 17:46 ', 'Day: 2.0 Timings: 09:29 09:29 17:54 ', 'Day: 3.0 Timings: 09:28 09:28 17:42 ', 'Day: 4.0 Timings: 11:18 17:47 ', 'Day: 5.0 Timings: ', 'Day 6.0 Timings: ']

使用正则表达式,我想查找哪些时间后面没有数字,并更新计数器或相应地记录位置以计算假期数。

因此,应该将Day:5.0和Day:6.0记录为假期。

我尝试了有效的在线正则表达式。但是我无法弄清楚如何在本地编辑器上实现它。 Link

4 个答案:

答案 0 :(得分:2)

您在这里不一定需要正则表达式。如果您只是分割“ Timings:” ,然后检查分割结果,该怎么办?对我来说看起来很干净:

In [1]: timingData = ['Day: 1.0 Timings: 09:52 17:46 ', 'Day: 2.0 Timings: 09:29 09:29 17:54 ', 'Day: 3.0 Timings: 09:28 09:28 17:42 ', 'Day: 4.0 Timings: 11:18 17:47 ', 'Day: 5.0 Timings: ', 'Day 6.0 Timings: ']

In [2]: for item in timingData:
            day, timing = item.split("Timings: ")
            if not timing:
                print(day)        
Day: 5.0 
Day 6.0 

当然,这里有一个假设,就是列表中的项目遵循此特定模式。

答案 1 :(得分:1)

在您发布的字符串中,您有单词Timings:,但正则表达式仅包含Timing:,该单词显然不匹配,并且看起来像是您输入的错字。

您可以使用简单的正则表达式,如果Timings:后接可选空格和至少两位数字,则可以认为计时数据不存在。

这是相同的python代码,

import re

timingData = ['Day: 1.0 Timings: 09:52 17:46 ', 'Day: 2.0 Timings: 09:29 09:29 17:54 ', 'Day: 3.0 Timings: 09:28 09:28 17:42 ', 'Day: 4.0 Timings: 11:18 17:47 ', 'Day: 5.0 Timings: ', 'Day 6.0 Timings: ']

for s in timingData:
 if (re.match(r'.*Timings:\s*\d{2}:.*', s)):
  print (s + ' --> ' + "Matched")
 else:
  print (s + ' --> ' + "Didn't match")

哪个提供了您期望的以下输出

Day: 1.0 Timings: 09:52 17:46  --> Matched
Day: 2.0 Timings: 09:29 09:29 17:54  --> Matched
Day: 3.0 Timings: 09:28 09:28 17:42  --> Matched
Day: 4.0 Timings: 11:18 17:47  --> Matched
Day: 5.0 Timings:  --> Didn't match
Day 6.0 Timings:  --> Didn't match

答案 2 :(得分:1)

正如@alecxe所指出的那样,正则表达式在此问题上的作用是过大的,但是如果您确实需要,我想您可以简单地执行以下操作:

import re
holidays = []

timingData = ['Day: 1.0 Timings: 09:52 17:46 ', 'Day: 2.0 Timings: 09:29 09:29 17:54 ', 'Day: 3.0 Timings: 09:28 09:28 17:42 ', 'Day: 4.0 Timings: 11:18 17:47 ', 'Day: 5.0 Timings: ', 'Day 6.0 Timings: ']

for t in timingData:
    a = re.search('(Timings:\s)[\w:\s]+',t)
    if a == None: #No matches found
            holidays.append(t[:8])
print(holidays)

答案 3 :(得分:0)

import re
timingData = ['Day: 1.0 Timings: 09:52 17:46 ', 'Day: 2.0 Timings: 09:29 09:29 17:54 ', 'Day: 3.0 Timings: 09:28 09:28 17:42 ', 'Day: 4.0 Timings: 11:18 17:47 ', 'Day: 5.0 Timings: ', 'Day 6.0 Timings: ']

regexedData = []
for i in timingData:
    regexedData.append(re.findall(r'(.+?)(Timings:\s)$',i))
for i in regexedData:
    if i:
        print(i[0][0])

输出:

C:\Users\Desktop>py x.py
Day: 5.0
Day 6.0