re库主要功能函数
来源:互联网 发布:人工智能用什么语言写 编辑:程序博客网 时间:2024/06/05 03:42
#coding=utf-8import re#re.search(pattern, string, flags)匹配正则表达式的第一个位置,返回match对象match = re.search(r'[1-9]\d{5}', 'BIT 100081')if match: print match.group(0)#re.match(pattern, string, flags)从字符串的开始位置匹配正则表达式,返回match对象#不调整参数时会报错#match = re.match(r'[1-9]\d{5}', 'BIT 100081')#调整参数match = re.match(r'[1-9]\d{5}', '100081BIT')if match: print match.group(0)#报错# print match.group(0)# Traceback (most recent call last):# File "C:/Users/Administrator/PythonWorkSpace/python14.py", line 10, in <module># print match.group(0)# AttributeError100081# : 'NoneType' object has no attribute 'group'#re.findall(pattern, string, flags)以列表类型返回全部能匹配的子串ls = re.findall(r'[1-9]\d{5}', 'BIT100081 TSU100084')print ls#re.split(pattern, string, maxsplit, flags)将字符串按照正则表达式匹配结果进行分割,返回列表类型#maxsplit:最大分割数ls = re.split(r'[1-9]\d{5}', 'BIT100081 TSU100084')print lsls = re.split(r'[1-9]\d{5}', 'BIT100081 TSU100084', 1)print ls #只匹配第一个#re.finditer(pattern, string, flags)返回一个匹配结果的迭代类型,每个迭代元素都是match对象for m in re.finditer(r'[1-9]\d{5}', 'BIT100081 TSU100084'): if m: print m.group(0)#re.sub(pattern, repl, string,count=0, flags=0)在一个字符串中替换所有匹配正则表达式的子串,返回替换后的字符串#repl:替换匹配字符串的字符串#count:匹配的最大替换次数print re.sub(r'[1-9]\d{5}', ':zipcode', 'BIT100081 TSU100084')print re.sub(r'[1-9]\d{5}', ':zipcode', 'BIT100081 TSU100084', 1)
阅读全文
0 0
- re库主要功能函数
- SciPy库主要功能
- Python标准库的主要功能
- re模块 compile函数
- re模块 compile函数
- RE库
- python re的几个函数
- python re 模块 findall 函数
- 二. re模块函数详解
- Python Re模块 常用函数
- re模块下的函数
- python 中的 re.compile 函数
- PHP留言本模块主要功能函数说明(代码可实现)
- DeepLearningToolbox(1)代码框架分析之关键函数的主要功能
- python之re库
- Python Re库
- Python标准库--re
- Re库基本用法
- Linux下的文件描述符与文件指针及其区别
- 漫谈递归:递归的思想
- localtime()函数的使用问题
- vb.net指定范围内获取随机数
- git学习及基本入门
- re库主要功能函数
- 深度学习之标注工具【Qt版】
- 数组删除重复的数字1方法一:
- 集成学习(Ensemble Learning)
- Ubuntu安装使用google代码规范工具cpplint
- Android Studio安装新的字体
- 基于函数的索引+创建基于函数的索引
- 线段树入门(建树,查询,更新)hdu1754
- Linux重定向命令(stdout, stdin, stderr)