您的位置 首页 知识

python正则r的作用 Python中re模块结合正则表达式的实际应用案例 p

python正则r的作用 Python中re模块结合正则表达式的实际应用案例 p

目录
  • 前言
  • re模块常用函数
  • 一、查看文本中是否包含 A 或 B 字符串
  • 二、替换多个关键词为统一格式
  • 三、提取所有数字
  • 四、去除标点符号
  • 五、提取中文字符
  • 六、删除空白字符(空格、换行、制表符等)
  • 七、提取邮箱地址
  • 八、提取 URL 地址
  • 九、保留字母、数字、中文,删除其他字符
  • 十、分词前预处理(小写 + 去除独特字符)
  • 十一、提取手机号码技巧(11位数字)
    • 1.提前手机号码
    • 2.从包含干扰字符的字符串中提取手机号并清理
    • 4.添加民族区号并统一格式
    • 5.封装成函数
    • 6.正则说明
  • 拓展资料

    前言

    在 Python 中,re 模块是用于处理正则表达式的标准库。它非常适合用于文本清洗、提取和整理任务。下面是一些常见的使用 re 包结合正则表达式进行文本清洗的技巧示例。

    re模块常用函数

    函数 功能
    re.match() 从字符串开头开始匹配
    re.search() 在整个字符串中查找第一个匹配项
    re.findall() 找出所有匹配的内容,返回列表
    re.sub() 替换匹配内容
    re.split() 根据正则表达式分割字符串

    一、查看文本中是否包含 A 或 B 字符串

    import retext = “这一个测试字符串,包含apple和banana。” 查看是否包含 ‘apple’ 或 ‘banana’if re.search(r’apple|banana’, text): print(“包含 apple 或 banana”)else: print(“不包含”)说明:r’apple|banana’ 一个正则表达式,表示匹配 “apple” 或者 “banana”re.search():只要在字符串中有匹配项就返回 True(或匹配对象)

    二、替换多个关键词为统一格式

    text = “访问网址 www.google.com 或 http://www.baidu.com 获取信息”cleaned_text = re.sub(r’www.|http://’, ”, text)print(cleaned_text) 输出: 访问网址 google.com 或 baidu.com 获取信息

    三、提取所有数字

    text = “电话号码是1234567890,邮编是100000″numbers = re.findall(r’d+’, text)print(numbers) 输出: [‘1234567890’, ‘100000’]

    四、去除标点符号

    import stringtext = “无论兄弟们好!这是一段,含有很多标点?”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 = “联系我 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’]

    九、保留字母、数字、中文,删除其他字符

    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'[^ws]|_’, ”, text).lower()print(cleaned_text) 输出: hello world 无论兄弟们好world

    十一、提取手机号码技巧(11位数字)

    1.提前手机号码

    示例目标: 将如 13812345678 格式手机号提取出来,并格式化为 138-1234-5678。

    实现代码:

    import retext = “我的电话是13812345678,请在职业时刻拨打。另一个号码是13987654321” 提取所有11位手机号phone_numbers = re.findall(r’1d10}’, text) 格式化为 138-1234-5678 样式formatted_numbers = [re.sub(r'(d3})(d4})(d4})’, 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 = “联系方式:138 1234 5678 或 139-1234-5678″ 去除非数字字符后提取11位手机号cleaned_numbers = re.findall(r’1d10}’, re.sub(r’D’, ”, text)) 再格式化输出formatted_numbers = [re.sub(r'(d3})(d4})(d4})’, r’1-2-3′, num) for num in cleaned_numbers]print(formatted_numbers) 输出: [‘138-1234-5678’, ‘139-1234-5678’]

    4.添加民族区号并统一格式

    如果你需要加上民族区号 +86:

    formatted_with_code = [‘+86 ‘ + num for num in formatted_numbers]print(formatted_with_code) 输出: [‘+86 138-1234-5678’, ‘+86 139-1234-5678’]

    5.封装成函数

    def format_chinese_phone(text): 清理非数字内容 cleaned = re.sub(r’D’, ”, text) 提取手机号 phones = re.findall(r’1d10}’, cleaned) 格式化 return [re.sub(r'(d3})(d4})(d4})’, r’1-2-3′, p) for p in phones] 使用示例text = “我的联系电话是:138 1234 5678 和 139-8765-4321″print(format_chinese_phone(text)) 输出: [‘138-1234-5678’, ‘139-8765-4321’]

    6.正则说明

    正则表达式 含义
    d 匹配任意数字
    D 匹配非数字
    n} 精确匹配 n 次
    r&039;(d3})(d4})(d4})&039; 分组提取前3位、中间4位、后4位
    1-2-3 替换为带连字符的格式

    拓展资料

    到此这篇关于Python中re模块结合正则表达式实际应用案例的文章就介绍到这了,更多相关Pythonre模块正则表达式内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!

    无论兄弟们可能感兴趣的文章:

    • python正则表达式re模块详细介绍
    • python的正则表达式re模块的常用技巧
    • PYTHON正则表达式 re模块使用说明
    • Python模块进修 re 正则表达式
    • python re正则表达式模块(Regular Expression)
    • python正则表达式re模块详解
    • Python基础教程之正则表达式基本语法以及re模块
    • Python的re模块正则表达式操作
    • 正则表达式+Python re模块详解
    • Python正则表达式re模块详解(建议收藏!)