timmy revised this gist 9 months ago. Go to revision
1 file changed, 20 insertions
re_example.py(file created)
| @@ -0,0 +1,20 @@ | |||
| 1 | + | import re | |
| 2 | + | ||
| 3 | + | # 定義正規表示式模式 | |
| 4 | + | pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b" | |
| 5 | + | ||
| 6 | + | # 測試字串 | |
| 7 | + | text = "聯絡我們: support@example.com 或 visit@example.org" | |
| 8 | + | ||
| 9 | + | # 搜尋第一個匹配的電子郵件 | |
| 10 | + | match = re.search(pattern, text) | |
| 11 | + | if match: | |
| 12 | + | print(f"找到電子郵件: {match.group()}") | |
| 13 | + | ||
| 14 | + | # 找出所有匹配的電子郵件 | |
| 15 | + | matches = re.findall(pattern, text) | |
| 16 | + | print(f"所有找到的電子郵件: {matches}") | |
| 17 | + | ||
| 18 | + | # 取代電子郵件為 [隱藏] | |
| 19 | + | masked_text = re.sub(pattern, "[隱藏]", text) | |
| 20 | + | print(f"隱藏處理後的文字: {masked_text}") | |
Newer
Older