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

时间:2019-04-17 01:10:36

标签: python regex

  • 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
  • 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

enter image description here

2 个答案:

答案 0 :(得分:0)

Use this

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

答案 1 :(得分:0)

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.