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

Revient en arrière si 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 quantifieurs gourmands import re
re.match(r"\d+?", "12345bcada")
<re.Match object; span=(0, 1), match='1'>

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

Expressions rationnelles en Python