Regex trong Python

Đi sâu vào các tập dữ liệu là một phần của bài học dành cho bất kỳ ai làm việc trong lĩnh vực khoa học dữ liệu. Thông thường, điều này có nghĩa là xử lý số, nhưng chúng ta sẽ làm gì khi tập dữ liệu của chúng ta chủ yếu dựa trên văn bản? . Trong hướng dẫn này, chúng ta sẽ xem xét kỹ hơn cách sử dụng biểu thức chính quy [regex] trong Python

Biểu thức chính quy [regex] về cơ bản là các mẫu văn bản mà bạn có thể sử dụng để tự động tìm kiếm và thay thế các thành phần trong chuỗi văn bản. Điều này có thể làm cho việc dọn dẹp và làm việc với các tập dữ liệu dựa trên văn bản trở nên dễ dàng hơn nhiều, giúp bạn không phải tìm kiếm trong hàng núi văn bản bằng tay

Biểu thức chính quy có thể được sử dụng trên nhiều ngôn ngữ lập trình khác nhau và chúng đã xuất hiện từ rất lâu

Tuy nhiên, trong hướng dẫn này, chúng ta sẽ tìm hiểu về các biểu thức chính quy trong Python, vì vậy hãy làm quen cơ bản với các khái niệm chính của Python như câu lệnh if-else, vòng lặp while và for, v.v. , bắt buộc. [Nếu bạn cần xem lại bất kỳ nội dung nào trong số này, các khóa học Python cơ bản của chúng tôi bao gồm tất cả các chủ đề có liên quan một cách tương tác, ngay trong trình duyệt của bạn. ]

Khi kết thúc hướng dẫn, bạn sẽ quen với cách thức hoạt động của regex Python và có thể sử dụng các mẫu và hàm cơ bản trong mô-đun regex của Python,

From:
From:
4, để phân tích các chuỗi văn bản. Bạn cũng sẽ được giới thiệu về cách regex có thể được sử dụng phối hợp với pandas để làm việc với các kho văn bản lớn [corpus có nghĩa là một tập dữ liệu văn bản]

[Để làm việc qua phần pandas của hướng dẫn này, bạn sẽ cần cài đặt thư viện pandas. Cách dễ nhất để thực hiện việc này là tải xuống Anaconda và thực hiện hướng dẫn này trong sổ ghi chép Jupyter. Đối với các tùy chọn khác, hãy xem hướng dẫn cài đặt pandas. ]

From: "Mr. Be [email protected]> Message-Id: From: "PRINCE OBONG ELEME" . Mẫu của chúng tôi,


for line in re.findall["From:...........", fh]:
    print[line]
5, bao gồm dấu ngoặc đóng, >. Hãy khắc phục nó


for line in fh.split["n"]:
    if "From:" in line:
        print[line]
9_______5_______0

Địa chỉ email kết thúc bằng một ký tự chữ và số, vì vậy chúng tôi giới hạn mẫu bằng


for line in re.findall["From:...........", fh]:
    print[line]
8. Vì vậy, sau biểu tượng @, chúng ta có

for line in fh.split["n"]:
    if "From:" in line:
        print[line]
15, có nghĩa là mẫu mà chúng ta muốn là một nhóm bất kỳ loại ký tự nào kết thúc bằng một ký tự chữ và số. Điều này loại trừ >

Do đó, mẫu địa chỉ email đầy đủ của chúng tôi trông như thế này.


for line in fh.split["n"]:
    if "From:" in line:
        print[line]
16

Phù. Đó là một chút để làm việc thông qua. Tiếp theo, chúng ta sẽ chạy qua một số hàm

From:
From:
4 phổ biến sẽ hữu ích khi chúng ta bắt đầu tổ chức lại kho văn bản của mình

Các hàm Regex Python phổ biến

Không thể phủ nhận

From:
From:
9 hữu ích, nhưng nó không phải là chức năng tích hợp sẵn duy nhất có sẵn cho chúng tôi trong
From:
From:
4

  • 
    for line in fh.split["n"]:
        if "From:" in line:
            print[line]
    20
  • 
    for line in fh.split["n"]:
        if "From:" in line:
            print[line]
    21
  • 
    for line in fh.split["n"]:
        if "From:" in line:
            print[line]
    22

Hãy xem xét từng cái một trước khi sử dụng chúng để mang lại trật tự cho tập dữ liệu của chúng ta

lại. Tìm kiếm[]

Trong khi

From:
From:
9 khớp với tất cả các phiên bản của một mẫu trong chuỗi và trả về chúng trong một danh sách, thì

for line in fh.split["n"]:
    if "From:" in line:
        print[line]
20 khớp với phiên bản đầu tiên của một mẫu trong chuỗi và trả về nó dưới dạng đối tượng khớp ________0____4

der.com>
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 
Message-Id: 
From: "Mr. Be
[email protected]>
Message-Id: 
From: "PRINCE OBONG ELEME" 

Chủ Đề