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')
4

1 回答 1

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')
于 2016-04-06T19:56:58.760 回答