python标准模块学习2_______re模块
来源:互联网 发布:a star算法c 编辑:程序博客网 时间:2024/05/24 01:46
re模块是Python常用模块中用来进行正则匹配的模块,re模块有两个主要而函数match()和search(),以及compile()
compile()函数会对正则表达式进行编译,对正则表达式进行预编译是被建议的,但不是必须的
用一个变量接收compile返回的RegexObject对象,可以用该变量去调用方法,并能实现代码重用提升性能
match()尝试用complie()生成的匹配模式匹配字符创match(pattern,string,flags=0),检查字符串开头是否能匹配
search()尝试在字符串中搜索正则表达式中出现的第一个匹配的字符串,search(pattern,string,flags=0),检查字符串中能否匹配
match与search是有区别的,match会从string的第一个字符开始进行匹配,而search则会从任意一个开始进行匹配
pattern = re.compile('x')
pattern.match('zxc') 无结果
pattern.match('zxc',1) 匹配成功
findall(pattern,string,flags=0)在string搜索所有的匹配项,返回一个匹配项组成的列表
finditer(patter,string,flags=0)与findall相同,但是返回的是一个迭代器而不是列表
split(pattern,string,max=0)根据pattern中的分隔符把string分割成一个列表
sub(pattern,repl,string,max=0)把string中与pattern匹配的地方替换成repl字符串
group()返回匹配的字符串,start()返回匹配开始的位置,end()返回匹配结束的位置,span()返回匹配开始与结束的位置元组
group()与groups()模块也有很有用的功能,经常用于分组中
- python标准模块学习2_______re模块
- python标准模块学习1_______fnmatch模块
- python标准模块学习4_______platform模块
- python标准模块学习5_______uuid模块
- python标准模块学习6_______urlparse模块
- python标准模块学习7_______webbrowser模块
- python标准模块学习8_______hashlib模块
- python标准模块学习9_______md5模块
- python标准模块学习10_______sha模块
- python标准模块学习11_______hmac模块
- python标准模块学习12_______json模块
- python标准模块学习13_______urllib2模块
- python标准模块学习14_______codecs模块
- python标准库fileinput模块学习
- python标准库os模块学习
- 【python标准库学习】re模块
- python标准模块学习3_______ConfigParser
- Python标准模块logging
- 【面试】【Oracle常见问题总结】【01】
- ROS的坐标系TF
- [HackerRank] Time Conversion
- To be pythonic
- 写给滴滴打车的柳青
- python标准模块学习2_______re模块
- ubuntu中安装maven
- JQuery中的.attr()与.removeAttr()
- Java中桥方法的作用和真实面目
- 修改const变量
- Android优秀开源组件项目集合
- WARNING: 998: Failure to setup sound, err = -50
- 勾股定理一日一证连载9
- 正确配置了vmare网络配置,但是在NAT模式下,虚拟机没法自动获取ip地址