如何使用正則表示式 (Regex) 過濾 Pandas 系列中的有效電子郵件?
正則表示式是一系列字元,用於定義搜尋模式。在本程式中,我們將使用這些正則表示式來過濾有效和無效的電子郵件。
我們將定義一個包含不同電子郵件的 Pandas 系列,並檢查哪個電子郵件有效。我們還將使用一個名為 re 的 Python 庫,該庫用於正則表示式目的。
演算法
Step 1: Define a Pandas series of different email ids. Step 2: Define a regex for checking validity of emails. Step 3: Use the re.search() function in the re library for checking the validity of the email.
示例程式碼
import pandas as pd import re series = pd.Series(['jimmyadams123@gmail.com', 'hellowolrd.com']) regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$' for email in series: if re.search(regex, email): print("{}: Valid Email".format(email)) else: print("{} : Invalid Email".format(email))
輸出
jimmyadams123@gmail.com: Valid Email hellowolrd.com : Invalid Email
解釋
regex 變數包含以下符號
- ^: 字串開頭的錨點
- [ ]: 開方括號和閉方括號定義一個字元類,以匹配單個字元
- \ : 跳脫字元
- . : 點號匹配除換行符之外的任何字元
- {} : 開花括號和閉花括號用於範圍定義
- $ : 美元符號是字串結尾的錨點
廣告