Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Khớp tại cuối chuỗi trong regex Python ($)

Anchor $ khớp vị trí cuối chuỗi (trước newline cuối nếu có, hoặc cuối tuyệt đối).

Ví dụ

import re
s = 'cheer leaders at the football stadium'
result = re.search(r'\w+$', s)
print(result.group())  # stadium

Kết quả

stadium

Giải thích: \w+$ khớp một từ (\w+) ngay trước cuối chuỗi.

Lưu ý: Với flag re.MULTILINE (re.M), $ khớp cuối mỗi dòng.