objectif
Écrire des regex multi-lignes documentées avec noms de groupes.
code minimal
import re
pat = re.compile(r"""
^
(?P<year>\d{4})- # année
(?P<month>\d{2})- # mois
(?P<day>\d{2})$ # jour
""", re.VERBOSE)
m = pat.fullmatch("2025-08-17")
print(m.group("month") == "08") # attendu: True
utilisation
import re
email = re.compile(r"""
^[A-Za-z0-9._%+-]+
@
[A-Za-z0-9.-]+\.[A-Za-z]{2,}$
""", re.VERBOSE)
print(bool(email.fullmatch("u@data.pm")))
variante(s) utile(s)
import re
print(bool(re.compile(r"[a]+ # c", re.VERBOSE).search("aaa")))
notes
- En VERBOSE, les espaces sont ignorés sauf échappés ou dans classes [] ou commentaires.
- Combinez avec flags comme IGNORECASE, MULTILINE si besoin.