Expressions régulières en Python
Maria Eugenia Inzaugarat
Data Scientist
Deux types d'appariement :
Les quantificateurs standards sont gourmands par défaut : *, +, ?, {num, num}
Gourmand : fait correspondre le plus de caractères possible
Retourne la correspondance la plus longue
import re
re.match(r"\d+", "12345bcada")
<re.Match object; span=(0, 5), match='12345'>

Revient en arrière quand trop de caractères correspondent
Abandonne les caractères un par un
import re
re.match(r".*hello", "xhelloxxxxxx")
<re.Match object; span=(0, 6), match='xhello'>

? aux quantificateurs gourmands import re
re.match(r"\d+?", "12345bcada")
<re.Match object; span=(0, 1), match='1'>

Revient en arrière quand trop peu de caractères correspondent
Étend les caractères un à la fois
import re
re.match(r".*?hello", "xhelloxxxxxx")
<re.Match object; span=(0, 6), match='xhello'>

Expressions régulières en Python