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

วิธีหา adverbs ทั้งหมดและตำแหน่งในข้อความด้วย Python Regex

ตามเอกสาร Python หากต้องการข้อมูลมากกว่าแค่ข้อความที่จับคู่ finditer() มีประโยชน์เพราะคืน match objects แทน strings หากเป็นนักเขียนต้องการหา adverbs ทั้งหมดและตำแหน่งในข้อความ ใช้ finditer() ดังนี้:

>> text = "He was carefully disguised but captured quickly by police."
>>> for m in re.finditer(r"\w+ly", text):
...     print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))
07-16: carefully
40-47: quickly