Pages : 1
#1 Le 10/07/2015, à 11:14
- PengouinPdt
[RÉSOLU] Python: re.sub
Bonjour,
j'ai du mal à comprendre l'usage des regex en python.
Pour le code suivant enregistré dans un fichier :
[u'http://pix.toile-libre.org/?img=1436486232.png', u'http://pix.toile-libre.org/upload/original/1436486232.png', u'[url=http://pix.toile-libre.org/?img=1436486232.png][img]http://pix.toile-libre.org/upload/thumb/1436486232.png[/img][/url]', u'[url=http://pix.toile-libre.org/?img=1436486232.png][img]http://pix.toile-libre.org/upload/img/1436486232.png[/img][/url]', u"<a href='http://pix.toile-libre.org/?img=1436486232.png'><img src='http://pix.toile-libre.org/upload/thumb/1436486232.png' /></a>", u"<a href='http://pix.toile-libre.org/?img=1436486232.png'><img src='http://pix.toile-libre.org/upload/img/1436486232.png' /></a>"]
Je cherche à remplacer toutes les chaînes ayant cette forme "<img src='(.*)' />", par celle-là '<img alt="" src="\1"/>'.
Nous remarquons qu'il y a deux fois la chaîne en question.
Voici le code que j'écris dans mon fichier de test :
import re
def read_file(rfile):
'''Read file'''
try:
fil = open(rfile, 'r')
if fil:
strings = fil.read()
fil.close()
return strings
except IOError as ioe:
print 'Error to read document: %s' % ioe
return False
code = read_file('toilelibre_response')
print code
pattern = "<img src='(.*)' />"
replace = '<img alt="" src="\1"/>'
if re.search(pattern, code):
print 'chaine trouvée !'
chain = re.sub(r"<img src='(.*)' />", r'<img alt="" src="\1"/>', code, re.S)
print chain
Le retour nous restitue cela - où l'on remarque que je n'arrive pas à traiter la chaîne pour avoir le résultat attendu !
$ ./test.py
[u'http://pix.toile-libre.org/?img=1436486232.png', u'http://pix.toile-libre.org/upload/original/1436486232.png', u'[url=http://pix.toile-libre.org/?img=1436486232.png][img]http://pix.toile-libre.org/upload/thumb/1436486232.png[/img][/url]', u'[url=http://pix.toile-libre.org/?img=1436486232.png][img]http://pix.toile-libre.org/upload/img/1436486232.png[/img][/url]', u"<a href='http://pix.toile-libre.org/?img=1436486232.png'><img src='http://pix.toile-libre.org/upload/thumb/1436486232.png' /></a>", u"<a href='http://pix.toile-libre.org/?img=1436486232.png'><img src='http://pix.toile-libre.org/upload/img/1436486232.png' /></a>"]
chaine trouvée !
[u'http://pix.toile-libre.org/?img=1436486232.png', u'http://pix.toile-libre.org/upload/original/1436486232.png', u'[url=http://pix.toile-libre.org/?img=1436486232.png][img]http://pix.toile-libre.org/upload/thumb/1436486232.png[/img][/url]', u'[url=http://pix.toile-libre.org/?img=1436486232.png][img]http://pix.toile-libre.org/upload/img/1436486232.png[/img][/url]', u"<a href='http://pix.toile-libre.org/?img=1436486232.png'><img alt="" src="http://pix.toile-libre.org/upload/thumb/1436486232.png' /></a>", u"<a href='http://pix.toile-libre.org/?img=1436486232.png'><img src='http://pix.toile-libre.org/upload/img/1436486232.png"/></a>"]
Dernière modification par PengouinPdt (Le 10/07/2015, à 15:11)
Hors ligne
#2 Le 10/07/2015, à 12:26
- pingouinux
Re : [RÉSOLU] Python: re.sub
Bonjour,
C'est à cause de ceci
r"<img src='(.*)' />"
qu'il faut remplacer par
r"<img src='(.*?)' />"
(.*) va chercher la chaîne la plus longue possible, et va correspondre à ce qui se trouve entre le premier <img src=' et le dernier ' />.
(.*?) va limiter la recherche à la chaîne la plus petite possible, et tu auras les deux.
D'autre part, re.S n'est pas correct ici, et correspond à l'argument count de re.sub. Il se trouve que cette constante numérique vaut 16, et que tu n'as que 2 occurrences de la chaîne. Le mieux est ici d'omettre cet argument.
Hors ligne
#3 Le 10/07/2015, à 15:10
- PengouinPdt
Re : [RÉSOLU] Python: re.sub
En effet, je te remercie !
Mais j'ai résolu le problème légèrement différemment, quand j'ai réalisé que la variable en question recevait une liste ...
Résultat, le code correspondant devient :
self.urls = re.findall(r'<textarea>(.*?)</textarea>',
self.response)
pattern = "<img src='(.*)' />"
for key, val in enumerate(self.urls):
if re.search(pattern, val):
self.urls[key] = re.sub(r"'", r'"', self.urls[key])
self.urls[key] = re.sub(r'<img src="(.*)" />',
r'<img alt="" src="\1"/>', self.urls[key])
Bref, merci à toi
Hors ligne
Pages : 1