Tôi đang cố gắng khớp các biểu thức khác nhau cho các địa chỉ:
Ví dụ: '398 W. Broadway'
Tôi muốn phù hợp với W. hoặc E. [Đông] hoặc pl. cho vị trí ... vv
Nó rất đơn giản bằng cách sử dụng regex này
pattern = re.compile[r'book']7 Ví dụ.
Tuy nhiên, mô -đun Python Re không khớp với bất cứ điều gì khi tôi nhập nó
>>> a
'398 W. Broadway'
>>> x = re.match['[W.|West]', a]
>>> x
>>> x == None
True
>>>
Hỏi ngày 28 tháng 9 năm 2013 lúc 18:29Sep 28, 2013 at 18:29
Saher Ahwalsaher AhwalSaher Ahwal
8.73529 Huy hiệu vàng81 Huy hiệu bạc146 Huy hiệu đồng29 gold badges81 silver badges146 bronze badges
3
pattern = re.compile[r'book']8 khớp ở đầu chuỗi đầu vào.
Để phù hợp ở bất cứ đâu, hãy sử dụng
pattern = re.compile[r'book']9 thay thế.
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
Xem
for word in words: if re.match[pattern, word]: print[f'The {word} matches']0:
Python cung cấp hai hoạt động nguyên thủy khác nhau dựa trên các biểu thức thông thường: re.match [] chỉ kiểm tra một trận đấu ở đầu chuỗi, trong khi Re.Search [] kiểm tra đối sánh ở bất cứ đâu trong chuỗi [đây là những gì Perl làm theo mặc định ].
Đã trả lời ngày 28 tháng 9 năm 2013 lúc 18:30Sep 28, 2013 at 18:30
Falsetrufalsetrufalsetru
345K59 Huy hiệu vàng688 Huy hiệu bạc610 Huy hiệu Đồng59 gold badges688 silver badges610 bronze badges
3
for word in words: if re.match[pattern, word]: print[f'The {word} matches']1 hạn chế tìm kiếm để bắt đầu ở ký tự đầu tiên của chuỗi. Sử dụng
for word in words: if re.match[pattern, word]: print[f'The {word} matches']2 thay thế. Lưu ý rằng
for word in words: if re.match[pattern, word]: print[f'The {word} matches']3 phù hợp với bất kỳ ký tự nào [ngoại trừ một dòng mới]. Nếu bạn muốn kết hợp một thời gian theo nghĩa đen, hãy thoát nó [
for word in words: if re.match[pattern, word]: print[f'The {word} matches']4 thay vì đơn giản
for word in words: if re.match[pattern, word]: print[f'The {word} matches']3].
Đã trả lời ngày 28 tháng 9 năm 2013 lúc 18:31Sep 28, 2013 at 18:31
Tim Peterstim PetersTim Peters
64,8K12 Huy hiệu vàng122 Huy hiệu bạc129 Huy hiệu đồng12 gold badges122 silver badges129 bronze badges
sửa đổi lần cuối ngày 29 tháng 7 năm 2022
Hướng dẫn biểu thức thường xuyên của Python chỉ ra cách sử dụng các biểu thức thông thường trong Python. Đối với các biểu thức thông thường trong Python, chúng tôi sử dụng mô -đun RE.
Biểu thức chính quy được sử dụng để tìm kiếm văn bản và thao tác văn bản nâng cao hơn. Biểu thức chính quy là các công cụ tích hợp như GREP, SED, biên tập viên văn bản như VI, Emacs, ngôn ngữ lập trình như TCL, Perl và Python.
Python re mô -đun
Trong Python, mô -đun
for word in words: if re.match[pattern, word]: print[f'The {word} matches']6 cung cấp các hoạt động khớp biểu thức thường xuyên.
Một mẫu là một biểu thức chính quy xác định văn bản chúng tôi đang tìm kiếm hoặc thao tác. Nó bao gồm các văn bản văn bản và metacharacters. Mẫu được biên dịch với hàm
for word in words: if re.match[pattern, word]: print[f'The {word} matches']7. Bởi vì các biểu thức thông thường thường bao gồm các ký tự đặc biệt, nên sử dụng các chuỗi thô. .
Sau khi chúng tôi biên soạn một mẫu, chúng tôi có thể sử dụng một trong các chức năng để áp dụng mẫu trên chuỗi văn bản. Các tổ chức bao gồm
for word in words: if re.match[pattern, word]: print[f'The {word} matches']8,
for word in words: if re.match[pattern, word]: print[f'The {word} matches']9,
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches0 và
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches1.
Biểu cảm thường xuyên
Bảng sau đây cho thấy một số biểu thức thông thường cơ bản:
for word in words: if re.match[pattern, word]: print[f'The {word} matches']3 | Khớp với bất kỳ ký tự đơn. |
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches3 | Khớp với phần tử trước một lần hoặc không. |
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches4 | Khớp với phần tử trước một lần hoặc nhiều lần. |
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches5 | Khớp với phần tử trước bằng 0 hoặc nhiều lần. |
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches6 | Khớp với vị trí bắt đầu trong chuỗi. |
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches7 | Khớp với vị trí kết thúc trong chuỗi. |
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches8 | Nhà điều hành xen kẽ. |
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches9 | Khớp với a hoặc b, hoặc c. |
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']0 | Phạm vi; khớp với a hoặc b, hoặc c. |
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']1 | Phủ định, phù hợp với mọi thứ ngoại trừ a, hoặc b hoặc c. |
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']2 | Phù hợp với nhân vật không gian trắng. |
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']3 | Khớp với một ký tự từ; tương đương với #!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']4 |
Các chức năng regex
Chúng tôi tìm kiếm các trận đấu với các chức năng Regex.
for word in words: if re.match[pattern, word]: print[f'The {word} matches']8 | Xác định nếu RE khớp với đầu của chuỗi. |
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']6 | Xác định nếu RE khớp với toàn bộ chuỗi. |
for word in words: if re.match[pattern, word]: print[f'The {word} matches']9 | Quét qua một chuỗi, tìm kiếm bất kỳ vị trí nào mà điều này phù hợp. |
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']8 | Tìm tất cả các chuỗi con nơi RE khớp và trả về chúng làm danh sách. |
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches1 | Tìm tất cả các chuỗi con nơi RE khớp với nhau và trả về chúng như một trình lặp lại. |
$ ./fullmatch_fun.py The book matches0 | Tách chuỗi theo mẫu lại. |
Các hàm
for word in words: if re.match[pattern, word]: print[f'The {word} matches']8,
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']6 và
for word in words: if re.match[pattern, word]: print[f'The {word} matches']9 trả về một đối tượng đối sánh nếu chúng thành công. Nếu không, họ trở lại
$ ./fullmatch_fun.py The book matches4.
Hàm khớp
Hàm
for word in words: if re.match[pattern, word]: print[f'The {word} matches']8 trả về một đối tượng khớp nếu 0 hoặc nhiều ký tự ở đầu chuỗi khớp với mẫu biểu thức chính quy.
match_fun.py
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.match[pattern, word]: print[f'The {word} matches']
Trong ví dụ, chúng ta có một từ ngữ. Mẫu được biên dịch sẽ tìm kiếm một chuỗi 'cuốn sách' trong mỗi từ.
pattern = re.compile[r'book']
Với chức năng
for word in words: if re.match[pattern, word]: print[f'The {word} matches']7, chúng tôi tạo ra một mẫu. Biểu thức chính quy là một chuỗi thô và bao gồm bốn ký tự bình thường.
for word in words: if re.match[pattern, word]: print[f'The {word} matches']
Chúng tôi trải qua bộ tuple và gọi hàm
for word in words: if re.match[pattern, word]: print[f'The {word} matches']8. Nó áp dụng mẫu trên từ. Hàm
for word in words: if re.match[pattern, word]: print[f'The {word} matches']8 trả về một đối tượng khớp nếu có một trận đấu ở đầu chuỗi. Nó trả về
$ ./fullmatch_fun.py The book matches4 nếu không có trận đấu.
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches
Bốn trong số các từ trong tuple phù hợp với mẫu. Lưu ý rằng các từ không bắt đầu với thuật ngữ 'cuốn sách' không khớp. Để bao gồm cả những từ này, chúng tôi sử dụng hàm
for word in words: if re.match[pattern, word]: print[f'The {word} matches']9.
Hàm fullmatch
Hàm
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']6 trông có vẻ phù hợp chính xác.
fullmatch_fun.py
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']
Trong ví dụ, chúng tôi sử dụng hàm
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']6 để tìm kiếm thuật ngữ 'cuốn sách' chính xác.
$ ./fullmatch_fun.py The book matches
Chỉ có một trận đấu.
Chức năng tìm kiếm
Hàm
for word in words: if re.match[pattern, word]: print[f'The {word} matches']9 tìm kiếm vị trí đầu tiên nơi mẫu biểu thức chính quy tạo ra một trận đấu.
search_fun.py
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.search[pattern, word]: print[f'The {word} matches']
Trong ví dụ, chúng tôi sử dụng hàm
for word in words: if re.match[pattern, word]: print[f'The {word} matches']9 để tìm kiếm thuật ngữ 'sách'.
$ ./search_fun.py The book matches The bookworm matches The bookish matches The cookbook matches The bookstore matches The pocketbook matches
Lần này các từ nấu ăn và các từ bỏ túi cũng được bao gồm.
Dot Metacharacter
Các dấu chấm [.] Metacharacter là viết tắt của bất kỳ ký tự nào trong văn bản.
dot_meta.py
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
0Trong ví dụ, chúng ta có một tuple với tám từ. Chúng tôi áp dụng một mẫu chứa metacharacter chấm trên mỗi từ.
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
1Dấu chấm là viết tắt của bất kỳ ký tự duy nhất trong văn bản. Nhân vật phải có mặt.
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
2Hai từ phù hợp với mô hình: bảy và trả thù.
Câu hỏi đánh dấu nhân vật meta
Ký tự meta dấu câu hỏi [?] Là một bộ định lượng phù hợp với phần tử trước đó bằng 0 hoặc một lần.
question_mark_meta.py
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
3Trong ví dụ, chúng tôi thêm một dấu hỏi sau ký tự dấu chấm. Điều này có nghĩa là trong mẫu chúng ta có thể có một ký tự tùy ý hoặc chúng ta không thể có nhân vật ở đó.
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
4Lần này, ngoài bảy và trả thù, các từ chẵn và sự kiện cũng phù hợp.
Neo
Neo phù hợp với vị trí của các ký tự bên trong một văn bản nhất định. Khi sử dụng ^ neo, trận đấu phải xảy ra ở đầu chuỗi và khi sử dụng neo $, trận đấu phải xảy ra ở cuối chuỗi.
anchors.py
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
5Trong ví dụ, chúng tôi có ba câu. Mẫu tìm kiếm là
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.search[pattern, word]: print[f'The {word} matches']5. Mẫu kiểm tra xem chuỗi "Jane" được đặt ở đầu văn bản.
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.search[pattern, word]: print[f'The {word} matches']6 sẽ tìm kiếm "Jane" ở cuối câu.
Kết hợp chuẩn xác
Một trận đấu chính xác có thể được thực hiện với hàm
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']6 hoặc bằng cách đặt thuật ngữ giữa các mỏ neo: ^ và $.
exact_match.py
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
6Trong ví dụ, chúng tôi tìm kiếm một trận đấu chính xác cho thuật ngữ 'cuốn sách'.
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
7Các lớp nhân vật
Một lớp ký tự xác định một tập hợp các ký tự, bất kỳ một trong số đó có thể xảy ra trong một chuỗi đầu vào cho một trận đấu để thành công.
character_class.py
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
8Trong ví dụ, chúng tôi sử dụng một lớp ký tự để bao gồm cả các từ xám và xám.
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
9Lớp
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.search[pattern, word]: print[f'The {word} matches']8 cho phép sử dụng 'E' hoặc 'A' Charcter trong mẫu.
Được đặt tên là các lớp ký tự
Có một số lớp ký tự được xác định trước.
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']2 phù hợp với ký tự khoảng trắng
$ ./search_fun.py The book matches The bookworm matches The bookish matches The cookbook matches The bookstore matches The pocketbook matches0,
$ ./search_fun.py The book matches The bookworm matches The bookish matches The cookbook matches The bookstore matches The pocketbook matches1 A Digit
$ ./search_fun.py The book matches The bookworm matches The bookish matches The cookbook matches The bookstore matches The pocketbook matches2 và
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']3 một ký tự từ
$ ./search_fun.py The book matches The bookworm matches The bookish matches The cookbook matches The bookstore matches The pocketbook matches4.
named_character_class.py
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.match[pattern, word]: print[f'The {word} matches']0
Trong ví dụ, chúng tôi đếm số trong văn bản.
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.match[pattern, word]: print[f'The {word} matches']1
Mẫu
$ ./search_fun.py The book matches The bookworm matches The bookish matches The cookbook matches The bookstore matches The pocketbook matches5 tìm kiếm bất kỳ số lượng chữ số nào trong văn bản.
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.match[pattern, word]: print[f'The {word} matches']2
Với phương thức
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']8, chúng tôi tra cứu tất cả các số trong văn bản.
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.match[pattern, word]: print[f'The {word} matches']3
Trường hợp không nhạy cảm với nhau
Theo mặc định, sự phù hợp của các mẫu là trường hợp nhạy cảm. Bằng cách chuyển
$ ./search_fun.py The book matches The bookworm matches The bookish matches The cookbook matches The bookstore matches The pocketbook matches7 cho hàm
for word in words: if re.match[pattern, word]: print[f'The {word} matches']7, chúng ta có thể làm cho nó không nhạy cảm.
case_insensitive.py
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.match[pattern, word]: print[f'The {word} matches']4
Trong ví dụ, chúng tôi áp dụng mẫu trên các từ bất kể trường hợp nào.
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.match[pattern, word]: print[f'The {word} matches']5
Tất cả bốn từ phù hợp với mẫu.
Thay thế
Nhà điều hành xen kẽ | Tạo một biểu thức chính quy với một số lựa chọn.
alternations.py
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.match[pattern, word]: print[f'The {word} matches']6
Chúng tôi có tám tên trong danh sách.
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.match[pattern, word]: print[f'The {word} matches']7
Biểu thức chính quy này tìm kiếm các chuỗi "Jane", "beky" hoặc "Robert".
Hàm Finditer
Hàm
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches1 trả về một iterator mang lại các đối tượng khớp trên tất cả các trận đấu không chồng chéo cho mẫu trong một chuỗi.
finditer_fun.py
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.match[pattern, word]: print[f'The {word} matches']8
Trong ví dụ, chúng tôi tìm kiếm thuật ngữ 'cáo' trong văn bản. Chúng tôi đi qua trình lặp của các trận đấu tìm thấy và in chúng bằng các chỉ mục của chúng.
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.match[pattern, word]: print[f'The {word} matches']9
Các hàm
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
00 và >>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
01 trả về chỉ số bắt đầu và kết thúc, tương ứng.pattern = re.compile[r'book']0
Bắt các nhóm
Bắt các nhóm là một cách để coi nhiều ký tự như một đơn vị. Chúng được tạo ra bằng cách đặt các ký tự bên trong một bộ dấu ngoặc tròn. Chẳng hạn, [sách] là một nhóm duy nhất chứa 'B', 'o', 'o', 'k', các ký tự.
Kỹ thuật nhóm bắt giữ cho phép chúng tôi tìm ra những phần của một chuỗi phù hợp với mẫu thông thường.
capturing_groups.py
pattern = re.compile[r'book']1
Ví dụ mã in tất cả các thẻ HTML từ chuỗi được cung cấp bằng cách chụp một nhóm ký tự.
pattern = re.compile[r'book']2
Để tìm tất cả các thẻ, chúng tôi sử dụng phương thức
#!/usr/bin/python import re words = ['book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook'] pattern = re.compile[r'book'] for word in words: if re.fullmatch[pattern, word]: print[f'The {word} matches']8.
pattern = re.compile[r'book']3
Chúng tôi đã tìm thấy bốn thẻ HTML.
Ví dụ về email Python Regex
Trong ví dụ sau, chúng tôi tạo một mẫu Regex để kiểm tra địa chỉ email.
emails.py
pattern = re.compile[r'book']4
Ví dụ này cung cấp một giải pháp khả thi.
pattern = re.compile[r'book']5
Các ký tự
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches6 đầu tiên và
$ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches7 cuối cùng cung cấp một mô hình chính xác. Không có ký tự trước và sau mẫu được cho phép. Email được chia thành năm phần. Phần đầu tiên là phần địa phương. Đây thường là tên của một công ty, cá nhân hoặc biệt danh.
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
05 liệt kê tất cả các ký tự có thể, chúng ta có thể sử dụng trong phần địa phương. Chúng có thể được sử dụng một hoặc nhiều lần.Phần thứ hai bao gồm ký tự
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
06 theo nghĩa đen. Phần thứ ba là phần miền. Nó thường là tên miền của nhà cung cấp email như Yahoo hoặc Gmail. >>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
07 là một lớp ký tự cung cấp tất cả các ký tự có thể được sử dụng trong tên miền. Trình định lượng $ ./match_fun.py The book matches The bookworm matches The bookish matches The bookstore matches4 cho phép sử dụng một hoặc nhiều ký tự này.
Phần thứ tư là ký tự dấu chấm. Nó được đi trước bởi ký tự thoát [\] để có được một dấu chấm theo nghĩa đen.
Phần cuối cùng là miền cấp cao nhất:
>>> import re
>>> re.match['a', 'abc']
>>> re.match['a', 'bac']
>>> re.search['a', 'bac']
09. Các miền cấp cao nhất có thể có từ 2 đến 18 ký tự, chẳng hạn như SK, mạng, thông tin, du lịch, làm sạch, bảo hiểm du lịch. Độ dài tối đa có thể là 63 ký tự, nhưng hầu hết các tên miền đều ngắn hơn 18 ký tự hiện nay. Ngoài ra còn có một ký tự chấm. Điều này là do một số miền cấp cao nhất có hai phần; Ví dụ, Co.uk.pattern = re.compile[r'book']6
Trong chương này, chúng tôi đã đề cập đến các biểu hiện thường xuyên trong Python.
Đọc hướng dẫn Python hoặc liệt kê tất cả các hướng dẫn Python.