Espressioni regolari in Python
Maria Eugenia Inzaugarat
Data Scientist
Due tipi di matching:
I quantificatori standard sono greedy di default: *, +, ?, {num, num}
Greedy: cattura più caratteri possibile
Restituisce la corrispondenza più lunga
import re
re.match(r"\d+", "12345bcada")
<re.Match object; span=(0, 5), match='12345'>

Fa backtracking se ha catturato troppo
Rinuncia a un carattere alla volta
import re
re.match(r".*hello", "xhelloxxxxxx")
<re.Match object; span=(0, 6), match='xhello'>

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

Fa backtracking se ha catturato troppo poco
Espande di un carattere alla volta
import re
re.match(r".*?hello", "xhelloxxxxxx")
<re.Match object; span=(0, 6), match='xhello'>

Espressioni regolari in Python