简体   繁体   English

Python正则表达式跨多行匹配

[英]Python regex match across multiple lines

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 string: 示例字符串:

Example=N      ; Comment Line One error=

; Comment Line Two.

Desired=

I am trying to match from Example= up to Desired= . 我正在尝试从Example=匹配到Desired= This will work if error= is not in the string. 如果error=不在字符串中,这将起作用。 However, when it is present I match Example=N ; Comment Line One error= 但是,当存在时,我匹配Example=N ; Comment Line One error= 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)

I also tried: 我也尝试过:

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

You may use 您可以使用

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

See the Python demo . 参见Python演示

Pattern details 图案细节

  • (?sm) - re.DOTALL and re.M are on (?sm) re.DOTALLre.M已开启
  • ^ - start of a line ^ -一行的开始
  • Example= - a substring Example= -子字符串
  • (.*?) - Group 1: any 0+ chars, as few as possible (.*?) -组1:任意0个以上的字符,并尽可能少
  • (?=[\\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 ). (?=[\\r\\n]+\\w+=|\\Z) -正向提前,要求存在1+ CR或LF符号,后跟1个或多个单词字符,后跟=符号,或字符串的结尾( \\Z )。

See the regex demo . 参见regex演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM