2016-04-06 3 views
0

Cette recherche ok:Python regexp n problème

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

Mais quand je remplace l'un des trois b-\n-il pas cherché:

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

Mais je veux analyser en second cas:

('aaa\r', 'b\nc') 

Répondre

3

Vous avez besoin du drapeau DOTALL:

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

Résultat:

('aaa\r', 'b\nc') 
+0

Merci! Oui '.' ne correspond pas' \ n' par défaut – user3479125