0

This searched ok:

>>> re.search(r'(.*?)\r\n(.+?)\r\n', 'aaa\r\r\nbbb\r\n').groups()
('aaa\r', 'bbb')

But when I replace one of three b to \n it not searched:

>>> re.search(r'(.*?)\r\n(.+?)\r\n', 'aaa\r\r\nb\nc\r\n').groups()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groups'

But I want to parse in second case:

('aaa\r', 'b\nc')
Ivan Borshchov
  • 2,215
  • 3
  • 26
  • 56

1 Answers1

3

You need the DOTALL flag:

import re
re.search(r'(.*?)\r\n(.+?)\r\n', 'aaa\r\r\nb\nc\r\n', flags=re.DOTALL).groups()

result:

('aaa\r', 'b\nc')
Travis
  • 1,889
  • 1
  • 21
  • 34