Python中re模塊結合正則表達式的實際應用案例
前言
在 Python 中,re 模塊是用于處理正則表達式的標準庫。它非常適合用于文本清洗、提取和整理任務。下面是一些常見的使用 re 包結合正則表達式進行文本清洗的方法示例。
re模塊常用函數(shù)
函數(shù) | 功能 |
---|---|
re.match() | 從字符串開頭開始匹配 |
re.search() | 在整個字符串中查找第一個匹配項 |
re.findall() | 找出所有匹配的內(nèi)容,返回列表 |
re.sub() | 替換匹配內(nèi)容 |
re.split() | 根據(jù)正則表達式分割字符串 |
一、查看文本中是否包含 A 或 B 字符串
import re text = "這是一個測試字符串,包含apple和banana。" # 查看是否包含 'apple' 或 'banana' if re.search(r'apple|banana', text): print("包含 apple 或 banana") else: print("不包含") 說明: r'apple|banana' 是一個正則表達式,表示匹配 “apple” 或者 “banana” re.search():只要在字符串中有匹配項就返回 True(或匹配對象)
二、替換多個關鍵詞為統(tǒng)一格式
text = "訪問網(wǎng)址 www.google.com 或 http://www.baidu.com 獲取信息" cleaned_text = re.sub(r'www\.|http://', '', text) print(cleaned_text) # 輸出: 訪問網(wǎng)址 google.com 或 baidu.com 獲取信息
三、提取所有數(shù)字
text = "電話號碼是1234567890,郵編是100000" numbers = re.findall(r'\d+', text) print(numbers) # 輸出: ['1234567890', '100000']
四、去除標點符號
import string text = "你好!這是一段,含有很多標點?" pattern = f"[{re.escape(string.punctuation)}]" cleaned_text = re.sub(pattern, "", text) print(cleaned_text) # 輸出: 你好這是一段含有很多標點
五、提取中文字符
text = "Hello 你好,世界123" chinese_chars = re.findall(r'[\u4e00-\u9fa5]+', text) print(''.join(chinese_chars)) # 輸出: 你好世界
六、刪除空白字符(空格、換行、制表符等)
text = " 這是一個\t帶有很多\n空白的文本 " cleaned_text = re.sub(r'\s+', ' ', text).strip() print(cleaned_text) # 輸出: 這是一個 帶有很多 空白的文本
七、提取郵箱地址
深色版本 text = "聯(lián)系我 at example@example.com 或 support@domain.co.cn" emails = re.findall(r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+', text) print(emails) # 輸出: ['example@example.com', 'support@domain.co.cn']
八、提取 URL 地址
text = "訪問 https://example.com 或 http://www.google.com" urls = re.findall(r'https?://(?:www\.)?\S+', text) print(urls) # 輸出: ['https://example.com', 'http://www.google.com']
九、保留字母、數(shù)字、中文,刪除其他字符
text = "這!is a 123_測試-text..." cleaned_text = re.sub(r'[^\u4e00-\u9fa5a-zA-Z0-9]', '', text) print(cleaned_text) # 輸出: 這is a 123測試text
十、分詞前預處理(小寫 + 去除特殊字符)
text = "Hello World! 你好,World!" cleaned_text = re.sub(r'[^\w\s]|_', '', text).lower() print(cleaned_text) # 輸出: hello world 你好world
十一、提取手機號碼方法(11位數(shù)字)
1.提前手機號碼
示例目標: 將如 13812345678 格式手機號提取出來,并格式化為 138-1234-5678。
實現(xiàn)代碼:
import re text = "我的電話是13812345678,請在工作時間撥打。另一個號碼是13987654321" # 提取所有11位手機號 phone_numbers = re.findall(r'1\d{10}', text) # 格式化為 138-1234-5678 樣式 formatted_numbers = [re.sub(r'(\d{3})(\d{4})(\d{4})', r'\1-\2-\3', num) for num in phone_numbers] print(formatted_numbers) # 輸出: ['138-1234-5678', '139-8765-4321']
2.從包含干擾字符的字符串中提取手機號并清理
有時候手機號中可能夾雜著空格、短橫線等字符,比如 "138 1234 5678" 或 "139-1234-5678"。 示例代碼:
text = "聯(lián)系方式:138 1234 5678 或 139-1234-5678" # 去除非數(shù)字字符后提取11位手機號 cleaned_numbers = re.findall(r'1\d{10}', re.sub(r'\D', '', text)) # 再格式化輸出 formatted_numbers = [re.sub(r'(\d{3})(\d{4})(\d{4})', r'\1-\2-\3', num) for num in cleaned_numbers] print(formatted_numbers) # 輸出: ['138-1234-5678', '139-1234-5678']
4.添加國家區(qū)號并統(tǒng)一格式
如果你需要加上國家區(qū)號 +86:
formatted_with_code = ['+86 ' + num for num in formatted_numbers] print(formatted_with_code) # 輸出: ['+86 138-1234-5678', '+86 139-1234-5678']
5.封裝成函數(shù)
def format_chinese_phone(text): # 清理非數(shù)字內(nèi)容 cleaned = re.sub(r'\D', '', text) # 提取手機號 phones = re.findall(r'1\d{10}', cleaned) # 格式化 return [re.sub(r'(\d{3})(\d{4})(\d{4})', r'\1-\2-\3', p) for p in phones] # 使用示例 text = "我的聯(lián)系電話是:138 1234 5678 和 139-8765-4321" print(format_chinese_phone(text)) # 輸出: ['138-1234-5678', '139-8765-4321']
6.正則說明
正則表達式 | 含義 |
---|---|
\d | 匹配任意數(shù)字 |
\D | 匹配非數(shù)字 |
{n} | 精確匹配 n 次 |
r'(\d{3})(\d{4})(\d{4})' | 分組提取前3位、中間4位、后4位 |
\1-\2-\3 | 替換為帶連字符的格式 |
總結
到此這篇關于Python中re模塊結合正則表達式實際應用案例的文章就介紹到這了,更多相關Python re模塊正則表達式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Tensorflow獲取張量Tensor的具體維數(shù)實例
今天小編就為大家分享一篇Tensorflow獲取張量Tensor的具體維數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01關于python函數(shù)的建立、調(diào)用、傳參、返回值詳解
這篇文章主要介紹了關于python函數(shù)的建立、調(diào)用、傳參、返回值詳解,Python?還支持自定義函數(shù),即將一段有規(guī)律的、可重復使用的代碼定義成函數(shù),從而達到一次編寫多次調(diào)用的目的,需要的朋友可以參考下2023-07-07Python re 模塊findall() 函數(shù)返回值展現(xiàn)方式解析
這篇文章主要介紹了Python re 模塊findall() 函數(shù)返回值展現(xiàn)方式解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08Python HTTP下載文件并顯示下載進度條功能的實現(xiàn)
這篇文章主要介紹了Python HTTP下載文件并顯示下載進度條功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04