簡體   English   中英

python:用於匹配多行模式的正則表達式(應用於字符串的開頭)

[英]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

我需要精確匹配TextOfInterest1AnotherText-of_Interest2 ,注意TextOfInterest1AnotherText-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.

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