首页 > 解决方案 > Python:仅在使用正则表达式的字符串中的特定单词之后查找完整文本

问题描述

有一段文字如下:

text = list of documents check 01 original invoice in favour of company z 02 cjpc abstract sheet weighment 
slip goods receipt note iz checklist creator id name 30009460 xyz@abc.com
checklist creation date 31 03 2018 checklist print date time 31 03 2018 10 45 57 note anything 
written manually on the checklist will not be considered invoice parth enterprise â invoice no dated 
kashish aarcade baroi road 18 25 mar 2018 village baroi delivery note mode terms of payment taluka 
mundra kutch supplierâ s ref other reference s gst no 24acypt3861 c1 z 7 dated buyer i buyer s order 
no 21 jun 2017 abc corporation 5700214006 â dated 40 mwp solar power plant i despatch document no 
vill bitta ta naliya abadasa despatched through destination march 18 terms of

目标: 我想提取单词'invoice'之后的文本,特别是'invoice'的第二次出现

我的方法:

txt = re.findall('invoice (.*)',text)

在上述方法中,我期望字符串列表如下:

txt = ['in favour of company z 02 cjpc abstract sheet weighment 
    slip goods receipt note iz checklist creator id name 30009460 xyz@abc.com
    checklist creation date 31 03 2018 checklist print date time 31 03 2018 10 45 57 note anything 
    written manually on the checklist will not be considered','parth enterprise â invoice no dated 
    kashish aarcade baroi road 18 25 mar 2018 village baroi delivery note mode terms of payment 
    taluka ..... #rest of the string]

但是我得到了text原始字符串中给出的整个字符串。如果我使用text.partition('invoice')我没有得到正确的字符串,如txt.

任何帮助,将不胜感激。

标签: pythonregex

解决方案


如果您想获得问题中的 2 个匹配项,则可以使用 2 个捕获组。

第一次匹配,直到第一次出现发票。然后在第二次出现发票之前在组 1 中捕获。

然后再次匹配发票,并捕获组 2 中的其余字符串。

^.*? invoice (.*?) invoice (.*)

正则表达式演示| Python 演示

例如

import re

text = "list of documents check 01 original invoice in favour of company z 02 cjpc abstract sheet weighment slip goods receipt note iz checklist creator id name 30009460 xyz@abc.comchecklist creation date 31 03 2018 checklist print date time 31 03 2018 10 45 57 note anything written manually on the checklist will not be considered invoice parth enterprise â invoice no dated kashish aarcade baroi road 18 25 mar 2018 village baroi delivery note mode terms of payment taluka mundra kutch supplierâ s ref other reference s gst no 24acypt3861 c1 z 7 dated buyer i buyer s order no 21 jun 2017 abc corporation 5700214006 â dated 40 mwp solar power plant i despatch document no vill bitta ta naliya abadasa despatched through destination march 18 terms of"
regex = r"^.*? invoice (.*?) invoice (.*)"

matches = re.search(regex, text)

if matches:
    print(matches.group(1))
    print('\n')
    print(matches.group(2))

输出

in favour of company z 02 cjpc abstract sheet weighment slip goods receipt note iz checklist creator id name 30009460 xyz@abc.comchecklist creation date 31 03 2018 checklist print date time 31 03 2018 10 45 57 note anything written manually on the checklist will not be considered


parth enterprise â invoice no dated kashish aarcade baroi road 18 25 mar 2018 village baroi delivery note mode terms of payment taluka mundra kutch supplierâ s ref other reference s gst no 24acypt3861 c1 z 7 dated buyer i buyer s order no 21 jun 2017 abc corporation 5700214006 â dated 40 mwp solar power plant i despatch document no vill bitta ta naliya abadasa despatched through destination march 18 terms of

推荐阅读