Python 正则表达式(re模块)
来源:互联网 发布:阿里云腾讯云对比报告 编辑:程序博客网 时间:2024/04/29 22:20
re模块是python的正则表达式模块,下面介绍正则表达式的两种用法:
直接使用表达式
import restrA = "123"a = re.match(r"\d",strA) #字符串前加r可以让字符串中的字符不转意print(a)'''结果:<_sre.SRE_Match object; span=(0, 1), match='1'>'''print(dir(a))#使用dir查看对象属性'''['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'end', 'endpos', 'expand', 'group', 'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs', 'span', 'start', 'string']'''#可以使用a.start()获取匹配起始位置#使用a.end()获取匹配结束为止
使用编译后的正则
使用编译后的正则有几点好处:1.编译可以检查正则表达式的正确性,不正确会报错。2可以提高效率。
import ree = re.compile(r"\d")a = e.match("123")#返回的对象和上述例子中的返回值相同类型
子表达式提取例子:
a = re.match(r"(\d)(\d)","123")#使用a.groups()获取匹配的子表达式元组('1', '2')
字符串切割:
re.split(r'\s+','a b c')#使用正则不论有多少空格都能正确分割,即使中间还有其他的分隔符,只要使用正确的表达式一般都能正确分割#['a', 'b', 'c']
0 0
- Python re(正则表达式)模块
- Python 正则表达式(re模块)
- python中的re模块(正则表达式)
- python中的正则表达式(re模块)
- python re模块(正则表达式)
- python中的正则表达式(re模块)
- python正则表达式:re模块
- Python 正则表达式 re模块
- Python 正则表达式,re模块
- Python re模块正则表达式
- Python正则表达式re模块
- Python正则表达式,re模块
- python 正则表达式 re模块
- Python 正则表达式 re模块
- python---re模块,正则表达式
- python正则表达式模块re
- Python正则表达式re模块
- python模块RE模块正则表达式
- 广度优先搜索(队列实现)
- Android网络状态的获取
- C++第一次实验-项目1
- VS2017在win7 64位机中x86和x64下基本类型的占用空间大小
- 如何去掉UITableView的单元格点击效果
- Python 正则表达式(re模块)
- 标准输入、输出流 , 合并流 , 序列化流的使用
- C++第1次实验【项目3】两点距离
- 拜占庭将军问题
- Maven:如何在构建中修改pom.xml中version版本号
- AES加密
- K-近邻kNN算法简介
- Elasticsearch整合Mysql新闻搜索
- 从C#到TypeScript_0