Regular Expressions in Python
Maria Eugenia Inzaugarat
Data Scientist
Hai kiểu so khớp:
Các lượng từ mặc định là tham lam: *, +, ?, {num, num}
Tham lam: khớp nhiều ký tự nhất có thể
Trả về chuỗi khớp dài nhất
import re
re.match(r"\d+", "12345bcada")
<re.Match object; span=(0, 5), match='12345'>

Quay lui khi khớp quá nhiều ký tự
Bỏ bớt từng ký tự một
import re
re.match(r".*hello", "xhelloxxxxxx")
<re.Match object; span=(0, 6), match='xhello'>

? vào sau lượng từ tham lam import re
re.match(r"\d+?", "12345bcada")
<re.Match object; span=(0, 1), match='1'>

Quay lui khi khớp quá ít ký tự
Mở rộng thêm từng ký tự một
import re
re.match(r".*?hello", "xhelloxxxxxx")
<re.Match object; span=(0, 6), match='xhello'>

Regular Expressions in Python