pythonregexnewlinelinefeedlf

Python regexp \n issue


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

Solution

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