Python正则表达式跨多行匹配 [英] Python regex match across multiple lines

查看:124
本文介绍了Python正则表达式跨多行匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试跨多行匹配正则表达式模式.模式以子字符串开头和结尾,两个子字符串都必须在一行的开头.我可以跨行匹配,但是似乎无法指定结束模式也必须位于一行的开头.

I am trying to match a regex pattern across multiple lines. The pattern begins and ends with a substring, both of which must be at the beginning of a line. I can match across lines, but I can't seem to specify that the end pattern must also be at the beginning of a line.

示例字符串:

Example=N      ; Comment Line One error=

; Comment Line Two.

Desired=

我正在尝试从Example=匹配到Desired=.如果error=不在字符串中,则将起作用.但是,如果存在,我会匹配Example=N ; Comment Line One error=

I am trying to match from Example= up to Desired=. This will work if error= is not in the string. However, when it is present I match Example=N ; Comment Line One error=

config_value = 'Example'
pattern = '^{}=(.*?)([A-Za-z]=)'.format(config_value)
match = re.search(pattern, string, re.M | re.DOTALL)

我也尝试过:

config_value = 'Example'
pattern = '^{}=(.*?)(^[A-Za-z]=)'.format(config_value)
match = re.search(pattern, string, re.M | re.DOTALL)

推荐答案

您可以使用

config_value = 'Example'
pattern=r'(?sm)^{}=(.*?)(?=[\r\n]+\w+=|\Z)'.format(config_value)
match = re.search(pattern, s)
if match:
    print(match.group(1))

请参见 Python演示.

模式详细信息

  • (?sm)-re.DOTALLre.M已打开
  • ^-一行的开头
  • Example=-子字符串
  • (.*?)-第1组:任意0个以上的字符,尽可能少
  • (?=[\r\n]+\w+=|\Z)-正向超前,要求存在1+ CR或LF符号,后跟1个或多个单词字符,后跟=符号或字符串的结尾(\Z).
  • (?sm) - re.DOTALL and re.M are on
  • ^ - start of a line
  • Example= - a substring
  • (.*?) - Group 1: any 0+ chars, as few as possible
  • (?=[\r\n]+\w+=|\Z) - a positive lookahead that requires the presence of 1+ CR or LF symbols followed with 1 or more word chars followed with a = sign, or end of the string (\Z).

请参见 regex演示.

这篇关于Python正则表达式跨多行匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆