python 正则语法
来源:互联网 发布:2017淘宝现在还能刷吗 编辑:程序博客网 时间:2024/06/16 07:34
通配符: . 匹配任何字符
import restr1='imooc python'pa=re.compile('.')ma=re.findall(str1)print ma输出:i
对特殊字符进行转义: \
字符集:
[a-z]可以匹配任意小写字母
import restr1='imooc.sjh a ImoocSjh,,,!!'pa=re.compile('[a-z]')ma=re.findall(pa,str1)print ma输出:['i', 'm', 'o', 'o', 'c', 's', 'j', 'h', 'a', 'm', 'o', 'o', 'c', 'j', 'h']
[a-zA-Z0-9]可以匹配任意字母数字
import restr1='imooc.sjh a ImoocSjh,,,!!'pa=re.compile('[a-zA-Z0-9]')ma=re.findall(pa,str1)print ma输出:['i', 'm', 'o', 'o', 'c', 's', 'j', 'h', 'a', 'I', 'm', 'o', 'o', 'c', 'S', 'j', 'h']
反转字符集 [^a]可以匹配除a以外的字符
import restr1='imooc.sjh a ImoocSjh,,,!!'pa=re.compile('[^a]')ma=re.findall(pa,str1)print ma输出:['i', 'm', 'o', 'o', 'c', '.', 's', 'j', 'h', ' ', ' ', 'I', 'm', 'o', 'o', 'c', 'S', 'j', 'h', ',', ',', ',', '!', '!']
选择符:| 管道符号,只匹配指定的字符串
str1='imooc.sjh a ImoocSjh,test,,!!'pa=re.compile('sjh|test')ma=re.findall(pa,str1)输出:['sjh', 'test']
子模式:当不需要对整个模式使用选择符 时,用()括起
str1='imooc.sjh a ImoocSjh,stest,,!!'pa=re.compile('s(jh|test)')ma=re.findall(pa,str1)输出:['jh', 'test']
可选项:在子模式后面加 ? 可以出现在匹配字符串,但不是必须的。
1.
str1='http://www.imooc.com'pa=re.compile('(http://)?(www\.)?(imooc\.com)')ma=re.findall(pa,str1)输出:[('http://', 'www.', 'imooc.com')]
2.
str1='www.imooc.com'pa=re.compile('(http://)?(www\.)?(imooc\.com)')ma=re.findall(pa,str1)输出:[('', 'www.', 'imooc.com')]
3.
str1='http://www.imooc'pa=re.compile('(http://)?(www\.)?(imooc\.com)')ma=re.findall(pa,str1)输出:[]
重复子模式:(pattern)*:模式可以重复0或多次
(pattern)+:模式可以重复1或多次
(pattern){m,n}:模式可以重复m~n次
1.
str1=' www.python.org .python.org wwwwwww.python.org w.python.org wwww.python.org'pa=re.compile('w*\.python\.org')ma=re.findall(pa,str1)输出:['www.python.org', '.python.org', 'wwwwwww.python.org', 'w.python.org', 'wwww.python.org']
2.
str1=' www.python.org .python.org wwwwwww.python.org w.python.org wwww.python.org'pa=re.compile('w+\.python\.org')ma=re.findall(pa,str1)输出(没有匹配'.python.org'):['www.python.org', 'wwwwwww.python.org', 'w.python.org', 'wwww.python.org']
3.
str1=' www.python.org .python.org wwwwwww.python.org w.python.org wwww.python.org'pa=re.compile('w{3,4}\.python\.org')ma=re.findall(pa,str1)输出:['www.python.org', 'wwww.python.org', 'wwww.python.org']
阅读全文
0 0
- python 正则语法
- 正则表达式语法学习(Python/Qt/Vim)
- 正则表达式语法学习(Python/Qt/Vim)
- Python篇----正则表达式语法(基础)
- 关于Python语法、正则表达式的学习
- python正则表达式re模块语法
- Python 正则表达式学习(二)正则表达式语法
- 正则语法
- 正则语法
- 正则语法
- 正则语法
- 正则语法
- 正则语法
- Python支持的正则表达式元字符和语法
- c#中使用python语法的正则表达式.
- Python基础--正则表达式基本语法以及re模块
- Python支持的正则表达式元字符和语法
- python-正则表达式 匹配多个字符串的语法规则
- flannel
- 多线程共享变量问题
- HTML和CSS入门(3)
- 右值引用与转移语义
- Android TextView中显示单行过长的用...代替
- python 正则语法
- seekg(0,ios::beg)不起作用的原因和解决方法
- 微信公众号点击子菜单获取文章列表
- 消失多年的老人正在盼望后来人
- 关于css中的box-sizing:border-box
- SpringBoot项目打成war和jar
- Linux部分命令归纳
- 《汇编语言》(王爽)检测点2.3
- Leetcode算法课程第九周(补)