Findall trong Python

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: 24
0

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
1

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
2

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
3_______1_______4
Match at index 14, 21
Full match: June 24
Month: June
Day: 24
5
Match at index 14, 21
Full match: June 24
Month: June
Day: 24
6

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
0

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
8
Match at index 14, 21
Full match: June 24
Month: June
Day: 24
4
['123456789', '987654321']
0
['123456789', '987654321']
1_______11_______2

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
0

['123456789', '987654321']
4
['123456789', '987654321']
5
Match at index 14, 21
Full match: June 24
Month: June
Day: 24
4
['123456789', '987654321']
7
['123456789', '987654321']
8

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
0

MetaCharacters0_______30_______1

MetaCharacters0_______30_______3

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
0

MetaCharacters0_______30_______6

MetaCharacters0_______30_______8

MetaCharacters0_______31_______0re1re2 re3 re4

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
0

MetaCharacters0______31_______7

MetaCharacters0re9

MetaCharacters0re.search()1

MetaCharacters0____32_______3

MetaCharacters0____32_______5

MetaCharacters0____32_______7

MetaCharacters0____32_______9

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
0

MetaCharacters0____33_______2

MetaCharacters0_______31_______0re1re.MatchObject6 re3 re.MatchObject8re.MatchObject9# A Python program to demonstrate working of re.match(). 0

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
0

MetaCharacters0____34_______3

MetaCharacters0_______31_______0re1# A Python program to demonstrate working of re.match(). 7 re3 re.MatchObject8import0# A Python program to demonstrate working of re.match(). 0

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
0

MetaCharacters0_______35_______4

MetaCharacters0_______31_______0re1import8 re3 re.MatchObject8re 1# A Python program to demonstrate working of re.match(). 0

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
0

re 4_______11_______8

MetaCharacters0_______31_______0re1re 9

Match at index 14, 21
Full match: June 24
Month: June
Day: 24
00

đầ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