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

ทำไมต้องใช้ re.compile() ใน Python Regular Expression

re.compile() Method

re.compile(pattern, repl, string) รวม regular expression pattern เป็น pattern objects ใช้สำหรับ pattern matching ช่วยค้นหา pattern ซ้ำโดยไม่ต้องเขียนใหม่

ตัวอย่าง

import re
pattern = re.compile('TP')
result = pattern.findall('TP Tutorialspoint TP')
print(result)
result2 = pattern.findall('TP is most popular tutorials site of India')
print(result2)

ผลลัพธ์

['TP', 'TP']
['TP']