Re: Explication de code - RE
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
import re
# Ta string
some_string='-</th></tr> <tr><th>12 -</th></tr> <tr horiz="no" hight=45><th>34 '
# Ton expression régulière compilée
re_buildnr=re.compile(r'-</th></tr> <tr[ 0-9a-zA-Z="]*><th>(?P<buildnr>\d+)')
# Cherche dans ta string, la première occurence de ton exp.
match_buildnr=re_buildnr.search(some_string)
print type(match_buildnr)
# Si ton exp n'est pas trouvée dans ta string (résultat du search = None)
# Message d'erreur. Par contre le msg ne ve rien dire ... il parle d'url alors
# que tu ne t'en sert pas.
if match_buildnr == None:
log.p('Error: Due to change in URL format, no build information was retrieved from URL: %s' % (url))
sys.ex(1)
# Tant que ton exp est trouvée, boucle et contine de la chercher dans ta string.
# a chaque fois qu'il la trouve, il converti le résultat en int et l'affiche multiplié par 100.
while match_buildnr != None:
startpos=match_buildnr.start()+1
newest_buildnr=match_buildnr.group('buildnr')
newest_buildnr=int(newest_buildnr)
print "100*nr is %i" % (newest_buildnr*100)
match_buildnr=re_buildnr.search(some_string, startpos) |