[英]python: regex to match pattern on multiple lines (apply to start of string)
我正在嘗試創建一個模式來匹配以下多行文本中的兩行:
Text of no interest.
TextOfInterest1, foobar of no interest
another foobar of no interest
AnotherText-of_Interest2 some foobar don't care
我需要精確匹配TextOfInterest1
和AnotherText-of_Interest2
,注意TextOfInterest1
和AnotherText-of_Interest2
之間可能有多行。 我不知道如何在單個模式字符串中多次應用^
符號?
嘗試這個:
pat = r"^.*\n([-_\w]+).*\n.*\n([-_\w]+).*$"
string="""Text of no interest.
TextOfInterest1, foobar of no interest
another foobar of no interest
AnotherText-of_Interest2 some foobar don't care"""
re.search(pat,string).groups()
('TextOfInterest1', 'AnotherText-of_Interest2')
檢查regex101處的模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.