簡體   English   中英

Python 正則表達式匹配多行字符串

[英]Python regex matching multiline string

我的字符串:

PCT Filing Date: 2 December 2015
\nApplicants: Silixa Ltd.
\nChevron U.S.A. Inc. (Incorporated
in USA - California)
\nInventors: Farhadiroushan,
Mahmoud
\nGillies, Arran
Parker, Tom'

我的代碼

regex = re.compile(r'(Applicants:)( )?(.*)', re.MULTILINE)
print(regex.findall(text))

我的 output:

[('Applicants:', ' ', 'Silixa Ltd.')]

我需要的是獲取 'Applicants:' 和 '\nInventors:' 之間的字符串

'Silixa Ltd.' & 'Chevron U.S.A. Inc. (Incorporated
in USA - California)'

在此先感謝您的幫助

嘗試使用 re.DOTALL 代替:

import re

text='''PCT Filing Date: 2 December 2015
\nApplicants: Silixa Ltd.
\nChevron U.S.A. Inc. (Incorporated
in USA - California)
\nInventors: Farhadiroushan,
Mahmoud
\nGillies, Arran
Parker, Tom'''

regex = re.compile(r'Applicants:(.*?)Inventors:', re.DOTALL)
print(regex.findall(text))

給我

$ python test.py
[' Silixa Ltd.\n\nChevron U.S.A. Inc. (Incorporated\nin USA - California)\n\n']

這樣做的原因是 MULTILINE 不會讓點 (.) 匹配換行符,而 DOTALL 會。

如果您想要的是Applicants:\nInventors:之間的內容,則您的正則表達式應反映:

>>> regex = re.compile(r'Applicants: (.*)Inventors:', re.S)
>>> print(regex.findall(s))
['Silixa Ltd.\n\nChevron U.S.A. Inc. (Incorporated\nin USA - California)\n']

re.S是“點匹配所有”選項,所以我們的(.*)也將匹配新行。 請注意,這與re.MULTILINE不同,因為re.MULTILINE只表示我們的表達式應該應用於多行,但不會改變事實. 不會匹配換行符。 如果. 不匹配換行符,像(.*)這樣的匹配仍然會在換行符處停止,無法達到您想要的多行效果。

另請注意,如果您對Applicants:Inventors:不感興趣,您可能不希望將其放在()之間,如(Inventors:)中的正則表達式,因為匹配將嘗試為其創建匹配組。 這就是您在 output 中獲得 3 個元素而不是只有 1 個的原因。

如果你想匹配 \nApplicants \nApplicants:\nInventors:之間的所有文本,你也可以在不使用re.DOTALL的情況下獲得匹配,以防止不必要的回溯。

匹配Applicants:並在第 1 組中捕獲同一行的 rest 以及后面所有不以Inventors:

然后匹配發明家。

^Applicants: (.*(?:\r?\n(?!Inventors:).*)*)\r?\nInventors:
  • ^字符串的開頭(如果不必在開頭,則使用\b
  • Applicants:字面匹配
  • (捕獲組 1
    • .*匹配線的rest
    • (?:\r?\n(?:Inventors.).*)*匹配所有不以 Invertors 開頭的行:
  • )關閉組
  • \r?\nInventors:匹配換行符和 Inventors:

正則表達式演示| Python 演示

示例代碼

import re
text = ("PCT Filing Date: 2 December 2015\n"
    "Applicants: Silixa Ltd.\n"
    "Chevron U.S.A. Inc. (Incorporated\n"
    "in USA - California)\n"
    "Inventors: Farhadiroushan,\n"
    "Mahmoud\n"
    "Gillies, Arran\n"
    "Parker, Tom'")
regex = re.compile(r'^Applicants: (.*(?:\r?\n(?!Inventors:).*)*)\r?\nInventors:', re.MULTILINE)
print(regex.findall(text))

Output

['Silixa Ltd.\nChevron U.S.A. Inc. (Incorporated\nin USA - California)']

這是一種更通用的方法,可以將這樣的字符串解析為其中所有鍵和值的字典(即,行開頭的任何字符串后跟:是鍵,該鍵后面的字符串是數據) :

import re 

txt="""\
PCT Filing Date: 2 December 2015
Applicants: Silixa Ltd.
Chevron U.S.A. Inc. (Incorporated
in USA - California)
Inventors: Farhadiroushan,
Mahmoud
Gillies, Arran
Parker, Tom'"""

pat=re.compile(r'(^[^\n:]+):[ \t]*([\s\S]*?(?=(?:^[^\n:]*:)|\Z))', flags=re.M)
data={m.group(1):m.group(2) for m in pat.finditer(txt)}

結果:

>>> data
{'PCT Filing Date': '2 December 2015\n', 'Applicants': 'Silixa Ltd.\nChevron U.S.A. Inc. (Incorporated\nin USA - California)\n', 'Inventors': "Farhadiroushan,\nMahmoud\nGillies, Arran\nParker, Tom'"}

>>> data['Applicants']
'Silixa Ltd.\nChevron U.S.A. Inc. (Incorporated\nin USA - California)\n'

正則表達式的演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM