Làm cách nào để trích xuất một chữ cái từ một từ trong python?

Biểu thức chính quy [RegEx] là một công cụ cực kỳ mạnh mẽ để xử lý và trích xuất các mẫu ký tự từ văn bản. Biểu thức chính quy nhanh và giúp bạn tránh sử dụng các vòng lặp không cần thiết trong chương trình của mình để khớp và trích xuất thông tin mong muốn

Trong bài đăng này, chúng tôi sẽ chỉ cho bạn cách bạn có thể sử dụng các biểu thức chính quy trong Python để giải quyết một số loại vấn đề nhất định

Để xem qua bài đăng này, không cần phải có kiến ​​​​thức trước về các biểu thức chính quy

Hãy hiểu cách bạn có thể sử dụng RegEx để giải quyết các vấn đề khác nhau trong xử lý văn bản. Trong bài đăng này, chúng tôi đang tập trung vào việc trích xuất các từ từ chuỗi

Sử dụng Biểu thức chính quy trong Python

Để bắt đầu sử dụng Biểu thức chính quy trong Python, bạn cần nhập mô-đun

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
7 của Python

import re 

Chúng tôi đã chia bài đăng này thành 3 phần không liên quan chặt chẽ với nhau và bạn có thể trực tiếp đến bất kỳ phần nào trong số đó để bắt đầu làm việc, nhưng nếu bạn không quen thuộc với RegEx, chúng tôi khuyên bạn nên theo dõi bài đăng này theo thứ tự

Chúng tôi sẽ sử dụng hàm

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
8 được cung cấp trong mô-đun
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
7 trong suốt bài đăng này để giải quyết các vấn đề của chúng tôi. Hãy bắt đầu nào

Sử dụng Toán tử "
cities_record = 'Chennai'
re.findall[cities_record, text]
0 để trích xuất tất cả các từ cụ thể

Giả sử rằng bạn có đoạn văn bản sau đây mô tả các thành phố khác nhau và bạn muốn có một danh sách tất cả các lần xuất hiện cho thành phố cụ thể

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."

Bây giờ, bạn muốn trích xuất tất cả các lần xuất hiện của Chennai, bạn có thể làm điều gì đó như thế này

cities_record = 'Chennai'
re.findall[cities_record, text]

Ở đây,

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
8 là một phương thức trong
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
7 nhận hai tham số — đầu tiên là mẫu được tìm kiếm, trong trường hợp này là
cities_record = 'Chennai'
re.findall[cities_record, text]
3 và tham số thứ hai là nội dung trong
cities_record = 'Chennai'
re.findall[cities_record, text]
4, từ đó nó sẽ tìm kiếm mẫu

Phương thức này trả về tất cả các kết quả khớp không chồng chéo của mẫu, nằm trong biến

cities_record = 'Chennai'
re.findall[cities_record, text]
5, từ chuỗi tham số thứ hai, nằm trong biến
cities_record = 'Chennai'
re.findall[cities_record, text]
6 trong trường hợp của chúng tôi, dưới dạng danh sách các chuỗi

Do đó, ô mã trên sẽ trả về danh sách tất cả các lần xuất hiện của từ

cities_record = 'Chennai'
re.findall[cities_record, text]
3 trong chuỗi của chúng tôi và do đó sẽ trả về danh sách sau

cities_record = 'Chennai'
re.findall[cities_record, text]
8

Nhưng đợi một chút. Tài liệu của chúng tôi đã có Chennai xảy ra 4 lần nhưng danh sách chỉ hiển thị 2. Tại sao?

Nếu bạn xem kỹ đoạn văn, bạn sẽ thấy rằng lần thứ ba, tên của thành phố được viết là

cities_record = 'Chennai'
re.findall[cities_record, text]
9 với chữ viết thường là
import re 
60

Theo mặc định, biểu thức chính quy phân biệt chữ hoa chữ thường

Vậy làm cách nào để bạn chụp được cả

import re 
61 trong một lượt? . Bạn có thể đặt giá trị của nó thành
import re 
64 như sau

import re 
6

Bằng cách đặt tham số flags thành

import re 
65, bạn đang yêu cầu trình thông dịch bỏ qua trường hợp này trong khi thực hiện tìm kiếm. Khi chạy mã này, bạn sẽ nhận được đầu ra sau

import re 
66

Tìm kiếm nhiều mẫu

Bây giờ, cùng với Chennai, bạn muốn trích xuất tất cả các lần xuất hiện của tên thành phố “Mumbai” từ đoạn văn bản này. Bạn chỉ có thể làm điều này bằng cách sử dụng toán tử

import re 
67 để tạo mẫu của mình

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
5

Điều này sẽ trả lại

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
6

Vì vậy, về cơ bản,

import re 
67 là một 'ký tự đặc biệt' yêu cầu regex tìm kiếm mẫu một
import re 
69 mẫu hai trong văn bản được cung cấp

Điều gì sẽ xảy ra nếu bạn muốn tìm kiếm sự xuất hiện của

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
50 trong tài liệu của mình? . Dấu gạch chéo ngược
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
53 về cơ bản yêu cầu regex đọc nó dưới dạng một ký tự mà không suy ra ý nghĩa đặc biệt của nó

Vì vậy, với tìm kiếm này, không thành vấn đề nếu tên của thành phố được viết là “mUMBAI”, “MUMBAI”, “CHENNAI” hoặc “cHENNAI” trong tài liệu của bạn. Tất cả những trường hợp này sẽ bị bắt, miễn là viết đúng chính tả của thành phố. Nếu bạn muốn bao gồm nhiều thành phố hơn trong tìm kiếm của mình, bạn có thể bao gồm lại chúng bằng cách sử dụng toán tử

import re 
67

Trích xuất các từ chỉ chứa bảng chữ cái

Có những lúc bạn muốn trích xuất các từ chỉ chứa bảng chữ cái. Một ví dụ điển hình cho điều này là nếu bạn nhận được một tài liệu văn bản chứa tên của tất cả các loại trái cây và rau quả cùng với số lượng tính bằng kilôgam mà một người đã mua ở định dạng sau

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
4

Để chỉ trích xuất tên của các loại trái cây/rau đã mua, bạn có thể tạo một mẫu bằng cách sử dụng lớp chỉ chứa các ký tự. Mô hình sẽ như sau

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
5

Trong mẫu này,

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
55 biểu thị một lớp ký tự từ
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
56 đến
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
57. Toán tử
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
58 biểu thị số lần xuất hiện của lớp ký tự này. Do đó, để trích xuất tên của các loại trái cây và rau quả, bạn có thể sử dụng mẫu như sau

cities_record = 'Chennai'
re.findall[cities_record, text]
0

Bạn sẽ nhận được đầu ra sau

cities_record = 'Chennai'
re.findall[cities_record, text]
1

Ký tự

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
58 là một ký tự đặc biệt trong regex. Nó được sử dụng để khớp với 1 hoặc nhiều lần lặp lại của biểu thức hoặc lớp chính quy trước đó, trong trường hợp của chúng tôi là
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
55. Vì vậy, nó khớp với 1 hoặc nhiều lần lặp lại của bảng chữ cái viết thường và do đó chúng tôi nhận được danh sách trên. Nếu chúng tôi muốn bao gồm 1 hoặc nhiều lần lặp lại của cả bảng chữ cái viết thường và in hoa, chúng tôi có thể tạo mẫu như sau

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
0

Vì vậy, theo cách này, bất kể trái cây và rau quả của chúng ta được viết bằng trường hợp nào, chúng sẽ được bắt bởi mẫu này ngay cả khi không sử dụng cờ

import re 
65

Hiểu các lớp ký tự trong Regex

Dấu ngoặc vuông là 'ký tự đặc biệt' trong biểu thức chính quy được sử dụng để khớp với một bộ ký tự. Ví dụ:

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
62 sẽ khớp với
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
63,
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
64 hoặc
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
65. Trong trường hợp của chúng tôi, chúng tôi đã sử dụng
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
55. Ký tự
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
67 khi được sử dụng bên trong
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
68, chỉ định phạm vi ký tự có thể khớp. Nó được sử dụng bằng cách đặt nó giữa hai ký tự là giới hạn dưới và trên của phạm vi

Lớp

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
55 sẽ khớp với bất kỳ chữ cái ASCII viết thường nào,
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
40sẽ khớp với tất cả các bảng chữ cái viết thường từ
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
56 đến
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
42, v.v. Nếu bạn muốn khớp với chữ
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
43 bên trong dấu ngoặc vuông, bạn cần chỉ định nó bằng dấu gạch chéo ngược
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
44. Ký tự dấu gạch chéo ngược
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
45 là ký tự thoát yêu cầu regex coi ký tự theo sau là một nghĩa đen và bỏ qua ý nghĩa đặc biệt của nó

Regex cũng sẽ coi

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
43 là một chữ nếu nó được sử dụng làm ký tự bắt đầu hoặc bắt đầu bên trong dấu ngoặc vuông như thế này.
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
47. Điều này sẽ chỉ phù hợp với
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
48 và
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
43

Trích xuất các từ theo sau mẫu cụ thể

Bạn sẽ thường gặp các vấn đề trong đó bạn phải trích xuất các từ/mẫu cụ thể theo sau là một ký tự cụ thể. Một ví dụ điển hình cho trường hợp này là khi bạn nhận được nhận xét về một bài viết cụ thể có thể trên một trang web và bạn muốn trích xuất tất cả tên/id người dùng được gắn thẻ trong đó

Để đơn giản, hãy giả sử rằng tên người dùng của chúng ta chỉ có thể chứa các bảng chữ cái và bất kỳ thứ gì theo sau là

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
50 mà không có khoảng trắng là tên người dùng

Hãy lấy nhận xét sau làm văn bản mẫu

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
1

Hãy tạo một mẫu biểu thức chính quy có thể được sử dụng để tìm kiếm tất cả tên người dùng được gắn thẻ trong nhận xét

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
2

Mẫu biểu thức chính quy này sẽ tìm và trích xuất tất cả tên người dùng được gắn thẻ trong nhận xét, không có phần

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
50

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
3

Đầu ra cho biểu thức chính quy ở trên là

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
4

Ở đây, nếu bạn kiểm tra mẫu của chúng tôi một cách cẩn thận, chúng tôi đã đặt mẫu của chúng tôi bên trong dấu ngoặc đơn sau

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
50. Mẫu có dấu ngoặc đơn trả về bất kỳ thứ gì được so khớp thông thường với biểu thức nằm trong dấu ngoặc đơn nhưng bắt đầu hoặc kết thúc bằng bất kỳ thứ gì được đề cập bên ngoài dấu ngoặc đơn

Điều đó có nghĩa là, những gì được tìm kiếm trong trường hợp này là @ ngay sau đó là 1 hoặc nhiều lần lặp lại của bất kỳ bảng chữ cái viết thường/viết hoa nào, nhưng chỉ mẫu bên trong

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
53 được trả về làm đối tượng quan tâm. Vì vậy, nếu bạn loại bỏ toán tử
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
53 khỏi cụm từ thông dụng của chúng ta

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
5

Bạn sẽ nhận được đầu ra sau

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
6

Đây là một trong những cách mà bạn có thể sử dụng toán tử

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
53 để trích xuất các mẫu cụ thể mà chúng tôi quan tâm, xuất hiện cùng với một số mẫu khác mà chúng tôi không quan tâm đến việc nắm bắt, chẳng hạn như chúng tôi muốn bỏ qua ký hiệu
text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
50 trong trường hợp của mình

Để hiểu tất cả các thành phần cơ bản của regex trong Python, cách tốt nhất để làm điều đó là truy cập tài liệu chính thức của Python 3. 8 RegEx tại đây

Chủ Đề