Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

จะจับคู่ที่ส่วนท้ายของสตริงใน python โดยใช้ Regular Expression ได้อย่างไร


รหัสต่อไปนี้ตรงกับคำว่า 'stadium' ที่ท้ายสตริง ' ผู้นำเชียร์ที่สนามฟุตบอล'

$-จับคู่ส่วนท้ายของสตริง

ตัวอย่าง

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

ผลลัพธ์

สิ่งนี้ให้ผลลัพธ์

stadium