python正则表达式
2023-09-14 09:01:12 时间
match()和search()都是python中的正则匹配函数,那这两个函数有何区别呢? match()函数只检测RE是不是在string的开始位置匹配, search()会扫描整个string查找匹配, 也就是说match()只有在0位置匹配成功的话才有返回,如果不是开始位置匹配成功的话,match()就返回none 例如: #! /usr/bin/env python # -*- coding=utf-8 -*- import re text = 'pythontab' m = re.match(r"\w+", text) if m: print m.group(0) else: print 'not match' 结果是:pythontab 而: #! /usr/bin/env python # -*- coding=utf-8 -*- # import re text = '@pythontab' m = re.match(r"\w+", text) if m: print m.group(0) else: print 'not match' 结果是:not match search()会扫描整个字符串并返回第一个成功的匹配 例如: #! /usr/bin/env python # -*- coding=utf-8 -*- # import re text = 'pythontab' m = re.search(r"\w+", text) if m: print m.group(0) else: print 'not match' 结果是:pythontab 那这样呢: #! /usr/bin/env python # -*- coding=utf-8 -*- # import re text = '@pythontab' m = re.search(r"\w+", text) if m: print m.group(0) else: print 'not match' 结果是:pythontab
相关文章
- python——正则表达式(re模块)详解
- python整除和取余写法_Python的整除和取余[通俗易懂]
- Python运算符与表达式
- Python代码调试
- Python 对目录的操作
- python字符串的使用方法_python输入字符串str
- 【说站】python快速排序的运作过程
- 【说站】python创建实例中类属性的变化
- 【说站】python类属性和实例属性的区别
- 【说站】python静态web服务器如何实现
- 【说站】Python如何列出目录中的所有文件
- Python字典提取_python字典键对应的值
- python程序化交易实例-用 Python 实现你的量化交易策略「建议收藏」
- 用python进行精细中文分句(基于正则表达式),HarvestText:文本挖掘和预处理工具
- Python使用platform库获取系统信息:操作系统信息、硬件信息、python环境信息
- python实现将excel数据添加到mongodb详解编程语言
- Python轻松连接SQL Server数据库(python连接sqlserver)
- 如何在Linux中安装Python?(linux安装python)
- python从MSSQL到Python:一段跨语言的旅程(mssql除以)
- Python内置的字符串处理函数整理