假设我有两个字符串列表,如下所示。你知道吗
lst_1 = ['foo','bar','Invoice No: SME2324-AA']
lst_2 = ['trincas','hotel park','delivery date 12-sept-2019','invoice no: 11245']
目标: 我想从这两张清单中提取发票号码。你知道吗
我目前的做法是:
lst_3 = [lst_1,lst_2]
txt=[]
for inv_no in lst_3:
for i in inv_no:
z = i
inv = re.search(r'Invoice (\S+) (.+?)',' '.join(z))
txt.append(inv)
当我想看到输出,即txt
时,我得到
[None, None, None, None, None, None, None, None]
我要找的是
['SME2324-AA','11245']
我错过了什么?任何帮助都将不胜感激。你知道吗
不使用
regex
,您可以这样尝试:输出:
' '.join
使每个元素之间都有空格。你知道吗(.+?)
在第一次匹配时停止(即非贪婪),而r'Invoice...'
在小写invoice
时必然失败。你知道吗append(inv)
实际上不会附加匹配结果;您需要指定group:if inv: text.append(inv.group(2)
解决所有问题:
输出:
通过将
re.findall
与re.IGNORECASE
一起使用,可以使其更简单:输出:
我们可以尝试将您的列表合并成一个字符串,然后使用
re.findall
查找所有发票号码:这张照片:
相关问题 更多 >
编程相关推荐