1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import re
ch = "23/06/2021!06:31:00!872701!4125:063100,4143,101,207,225,229,303:111111,325,329,441:252222"
motif = r"(:[0-9]{6})"
regex = re.compile(motif)
horaires = []
i = ch.find("!")
if i>=0:
# la chaine comporte un "!" => on cherche les horaires à la suite
while True:
x = regex.search(ch, i)
if x is None:
break # pas d'autres horaires
horaires.append([x.group(1), x.start(1), x.end(1)])
print(x.group(1), x.start(1), x.end(1))
i = x.end(1)
print()
# suppression des sous-chaines trouvées (parcours à l'envers)
for horaire, debut, fin in horaires[::-1]:
ch = ch[:debut] + ch[fin:]
print()
print(ch) |
Partager