简体   繁体   English

匹配正则表达式模式,其中开始和结束模式之间有\\ n \\ r \\ n

[英]Matching regex pattern where there is \n\r between starting and ending pattern

  • The red underscore is the desired string I want to match 红色下划线是我想要匹配的字符串
  • I would like to match all strings (including \\n) between the the two string provided in the example 我想匹配示例中提供的两个字符串之间的所有字符串(包括\\ n)
  • However, in the first example, where there is a newline, I can't get anything to match 但是,在第一个示例中,如果有换行符,我无法获得任何匹配项
  • In the second example, the regex expression works. 在第二个示例中,正则表达式有效。 It matches the string highlighted in Green because it resides on a single line 它匹配以绿色突出显示的字符串,因为它位于一行上
  • Not sure if there is a notation I need to include for \\n\\r to be part of the pattern to match 不确定是否有一个符号需要包含\\ n \\ r \\ n才能成为要匹配的模式的一部分

在此输入图像描述

Use this 用这个

output = re.search('This(.*?)\n\n(.*?)match', text)
>>> output.group(1)
'is a multiline expression'
>>> output.group(2)
'I would like to '

Try this one aswell: 试试这个:

output = re.search(r"This ([\S.]+) match", text).group(1).replace(r'\n','')

That will find the entire thing as one group then remove the new lines. 这将找到整个事物作为一个组然后删除新行。

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

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