python3 正则表达式(更新中)
来源:互联网 发布:月薪8000能招到java 编辑:程序博客网 时间:2024/06/05 05:10
1、特殊符号(先解释,后面会有例子)
2、re模块函数
3、使用match()方法匹配字符串
>>>import re>>>m = re.match('foo','food on the table')>>>m.group()#group()返回匹配对象'foo'
4、使用search()方法查找
>>>import re>>>m = re.rearch('foo',seafood')>>>m.group()'foo'
5、匹配多个字符串
>>>bt ='bat|bet|bit'>>>m = re.match(bt,'bat')>>>m.group()'bat'
6、匹配单个字符
>>>anyend = '.end'>>>m = re.match(anyend,'bend')>>>m.group()'bend'
7、创建字符集【】
>>>m = re.match('[cr][23][dp][o2]','c3po')>>>m.group()'c3po'
8、group()和groups()区别
#一个括号代表一个组#groups返回一个元祖>>>m = re.match('(\w\w\w)-(\d\d\d)','abc-123')>>>m.group()'abc-123'>>>m.group(1)'abc'>>>m.group(2)'123'>>>m.groups()('abc','123')
9、findall查找所有的,finditer迭代
>>>re.findall(r'(th\w+)','This and that,re.I)#re.I忽略大小写,r是使用原生字符['This','that']>>>it = re.finsiter(r'th\w+)','This and that',re.I)>>> g = it.__next__()>>>g.groups()('This',)>>g.group(1)'This'>>>g = it.__next__()>>>g.groups()('that',)
10、使用sub()和subn()搜索和替换
>>>re.sub('X','Mr.Smith','attn:X',\n')'attn:Mr.Smith,\n'#subn会返回('attn:Mr.Smith,\n',1),替换了一次#\N是分组编号>>>re.sub(r'(\d{1,2})/(\d{1,2})/(\d{2}|\d{4}',... r'\2/\1/\3','2/20/91')'20/2/91'#替换的结果就是结果的第一个组变成第二个组,第二个组变成第一个
11、贪婪与不贪婪
#贪婪>>>pat = '.+\d+-\d+-\d+'>>>date = 'Thu Feb 15 17:46:04 2007::uziff@jiojf.g::1212345-6-8'>>>re.search(pat,date).group()'Thu Feb 15 17:46:04 2007::uziff@jiojf.g::1212345-6-8'#不贪婪>>>pat = '.+?\d+-\d+-\d+'......>>>re.search(pat,date).group()'1212345-6-8'
阅读全文
0 0
- python3 正则表达式(更新中)
- 正则表达式(更新中......)
- 正则表达式基础(更新中。。。)
- 正则表达式总结(持续更新中...)
- javascript--正则表达式--更新中
- 正则表达式 python3.x (一)
- Python3:正则表达式
- Python3 re(正则表达式)
- python3正则表达式
- python3之正则表达式
- Python3正则表达式
- Python3 正则表达式
- Python3 re(正则表达式)
- Python3正则表达式
- Python3正则表达式使用
- Python3.x 正则表达式
- [总结]常用正则表达式(持续更新中……)
- 最新手机号码正则表达式,持续更新中
- 一些比较好的论文集合链接
- DialogFragment实现DatePicker
- 基于UE4+ OpenCV 的混合现实 (webCamera, mix-reality, blue screen matting)
- 高精度阶乘
- Mybatis中日志配置文件无效
- python3 正则表达式(更新中)
- <context:component-scan>使用说明
- 实验二 Linux用户管理
- [队内测试Day10.22T3][codevs1684]垃圾陷阱
- HDU1596 find the safest road(最短路)
- C++风格_引用参数
- [noip2015]信息传递 题解
- 英文投稿的状态
- 前端基础 JavaScript Data、Math对象 笔记