Biểu thức chính quy [đôi khi được gọi là Biểu thức hợp lý] là một chuỗi các ký tự xác định mẫu tìm kiếm, chủ yếu để sử dụng trong khớp mẫu với chuỗi hoặc khớp chuỗi, i. e. các hoạt động giống như "tìm và thay thế". Biểu thức chính quy là một cách tổng quát để khớp các mẫu với các chuỗi ký tự
Biểu thức chính quy của mô-đun [RE] chỉ định một tập hợp các chuỗi [mẫu] phù hợp với nó. Để hiểu phép loại suy RE, MetaCharacters
hữu ích, quan trọng và sẽ được sử dụng trong các chức năng của mô-đun re
Có tổng cộng 14 siêu ký tự và sẽ được thảo luận khi chúng đi vào các chức năng
\ Used to drop the special meaning of character following it [discussed below] [] Represent a character class ^ Matches the beginning $ Matches the end . Matches any character except newline ? Matches zero or one occurrence. | Means OR [Matches with any of the characters separated by it. * Any number of occurrences [including 0 occurrences] + One or more occurrences {} Indicate number of occurrences of a preceding RE to match. [] Enclose a group of REs
lại. Tìm kiếm[]
Phương thức re.search[]
trả về Không có [nếu mẫu không khớp] hoặc một re.MatchObject
chứa thông tin về phần khớp của chuỗi. Phương thức này dừng sau lần khớp đầu tiên, vì vậy phương thức này phù hợp nhất để kiểm tra biểu thức chính quy hơn là trích xuất dữ liệu
Ví dụ
Python3
# A Python program to demonstrate working of re.match[].
import
re
Match at index 14, 21 Full match: June 24 Month: June Day: 240
Match at index 14, 21 Full match: June 24 Month: June Day: 241
Match at index 14, 21 Full match: June 24 Month: June Day: 242
Match at index 14, 21 Full match: June 24 Month: June Day: 243_______1_______4
Match at index 14, 21 Full match: June 24 Month: June Day: 245
Match at index 14, 21 Full match: June 24 Month: June Day: 246
Match at index 14, 21 Full match: June 24 Month: June Day: 240
Match at index 14, 21 Full match: June 24 Month: June Day: 248
Match at index 14, 21 Full match: June 24 Month: June Day: 244
['123456789', '987654321']0
['123456789', '987654321']1_______11_______2
Match at index 14, 21 Full match: June 24 Month: June Day: 240
['123456789', '987654321']4
['123456789', '987654321']5
Match at index 14, 21 Full match: June 24 Month: June Day: 244
['123456789', '987654321']7
['123456789', '987654321']8
Match at index 14, 21 Full match: June 24 Month: June Day: 240
MetaCharacters
0_______30_______1
MetaCharacters
0_______30_______3
Match at index 14, 21 Full match: June 24 Month: June Day: 240
MetaCharacters
0_______30_______6
MetaCharacters
0_______30_______8
MetaCharacters
0_______31_______0re
1re
2 re
3 re
4
Match at index 14, 21 Full match: June 24 Month: June Day: 240
MetaCharacters
0______31_______7
MetaCharacters
0re
9
MetaCharacters
0re.search[]
1
MetaCharacters
0____32_______3
MetaCharacters
0____32_______5
MetaCharacters
0____32_______7
MetaCharacters
0____32_______9
Match at index 14, 21 Full match: June 24 Month: June Day: 240
MetaCharacters
0____33_______2
MetaCharacters
0_______31_______0re
1re.MatchObject
6 re
3 re.MatchObject
8re.MatchObject
9# A Python program to demonstrate working of re.match[].
0
Match at index 14, 21 Full match: June 24 Month: June Day: 240
MetaCharacters
0____34_______3
MetaCharacters
0_______31_______0re
1# A Python program to demonstrate working of re.match[].
7 re
3 re.MatchObject
8import
0# A Python program to demonstrate working of re.match[].
0
Match at index 14, 21 Full match: June 24 Month: June Day: 240
MetaCharacters
0_______35_______4
MetaCharacters
0_______31_______0re
1import
8 re
3 re.MatchObject
8re
1# A Python program to demonstrate working of re.match[].
0
Match at index 14, 21 Full match: June 24 Month: June Day: 240
re
4_______11_______8
MetaCharacters
0_______31_______0re
1re
9
Match at index 14, 21 Full match: June 24 Month: June Day: 2400
đầu ra
Match at index 14, 21 Full match: June 24 Month: June Day: 24
lại. tìm tất cả []
Trả về tất cả các kết quả khớp mẫu không chồng chéo trong chuỗi, dưới dạng danh sách các chuỗi. Chuỗi được quét từ trái sang phải và kết quả khớp được trả về theo thứ tự tìm thấy