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