第一章 正则表达式
来源:互联网 发布:c 定义二维数组 编辑:程序博客网 时间:2024/05/29 19:10
1.1 正则表达式符号表
1.1.1 符号
1.1.2 特殊字符
1.1.3扩展表示法
1.2 Python中的正则
Python通过标准库中的re模式支持正则表达式
1.2.1 re模块函数
1.2.2 re模块函数和正则表达式对象方法
1.2.3 常用的匹配对象方法(查看文档以获取更多信息)
1.2.4 常用模块属性(用于大多数正则表达式函数的标记)
1.2.5 一些示例
1.2.5.1 match()方法
#从起始部分进行匹配>>> import re>>> m=re.match('foo','foo')>>> if m is not None: m.group()结果: 'foo'
1.2.5.2 search()方法
#中间查找>>> import re>>> m=re.search('foo','seafood')>>> if m is not None:m.group()结果:'foo'
1.2.5.3 重复特殊字符以及分组
1)
>>> import re>>> patt='\w+@(\w+\.)*\w+\.com'>>> re.match(patt,'nobody@www.xxx.yyy.zzz.com').group()'nobody@www.xxx.yyy.zzz.com'
2)
>>> import re>>> 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')
1.2.5.4 使用findall()和finditer()
>>> s='This and that.'>>> re.findall(r'(th\w+) and (th\w+)',s,re.I)[('This', 'that')]>>> re.findall('(th\w+) and (th\w+)',s,re.I)[('This', 'that')]>>> re.findall(r'((th\w+) and (th\w+))',s,re.I)[('This and that', 'This', 'that')]>>> it=next(re.finditer(r'(th\w+) and (th\w+)',s,re.I))>>> it.group(1)'This'
1.2.5.5 使用sub()和subn()
>>> import re>>> re.sub('[ae]','X','abcdef')'XbcdXf'>>> re.subn('[ae]','X','abcdef')('XbcdXf', 2)
1.2.5.6 使用split()
DATA=( 'Mountain View, CA 94040', 'Sunnyvale, CA', 'Los Altos, 94023', 'Cupertino 95014', 'Palo Alto CA', )for datum in DATA: print(re.split(', |(?= (?:\d{5}|[A-Z]{2})) ',datum))结果:['Mountain View', 'CA', '94040']['Sunnyvale', 'CA']['Los Altos', '94023']['Cupertino', '95014']['Palo Alto', 'CA']
1.2.5.7 扩展符号的使用
#Python的正则支持大量的扩展符号#re.I/IGNORECASE>>> re.findall(r'(?i)yes','yes?Yes. YES!!')['yes', 'Yes', 'YES']#re.M/MULTILINE>>> re.findall(r'(?im)(^th[\w ]+)',"""This line is the first,another line,that line, it's the best""")['This line is the first', 'that line']#re.S/DOTALL>>> re.findall(r'(?s)th.+','''the first linethe second linethe third line''')['the first line\nthe second line\nthe third line\n']#re.X/VERBOSE>>> re.search(r'''(?x)\((\d{3})\) #区号[ ] #空白符(\d{3}) #前缀- #横线(\d{4}) #终点数字''','(800) 555-1212').groups()('800', '555', '1212')#(?:...)>>> re.findall(r'http://(?:\w+\.)*(\w+\.com)', 'http://google.com http://www.google.com http://code.google.com')['google.com', 'google.com', 'google.com']>>> re.findall(r'(?:\w+\.)*(\w+\.com)', 'http://google.com http://www.google.com http://code.google.com')['google.com', 'google.com', 'google.com']#(?P<name>)>>> re.search(r'\((?P<areacode>\d{3})\) (?P<prefix>\d{3})-(?:\d{4})','(800) 555-1212').groupdict(){'areacode': '800', 'prefix': '555'}#\g<name>>>> re.sub(r'\((?P<areacode>\d{3})\) (?P<prefix>\d{3})-(?:\d{4})','(\g<areacode>) \g<prefix>-xxxx','(800) 555-1212')'(800) 555-xxxx'#(?P=name)>>> re.match(r'\((?P<areacode>\d{3})\) (?P<prefix>\d{3})-(?P<number>\d{4}) (?P=areacode)-(?P=prefix)-(?P=number) 1(?P=areacode)(?P=prefix)(?P=number)','(800) 555-1212 800-555-1212 18005551212')结果:<_sre.SRE_Match object; span=(0, 39), match='(800) 555-1212 800-555-1212 18005551212'>#(?!...)>>>re.findall(r'(?m)^\s+(?!noreply|postmaster)(\w+)',''' sales@phptr.com postmaster@phptr.com eng@phptr.com noreply@phptr.com admin@phptr.com''')['sales', 'eng', 'admin']#查找匹配bool(re.search(r'(?:(x)|y)(?(1)y|x)','xy'))True>>> bool (re.search(r'(?:(x)|y)(?(1)y|x)','xx'))False
0 0
- 第一章 什么是正则表达式
- 第一章 正则表达式
- 简单入门正则表达式 - 第一章 概述
- <精通正则表达式>学习笔记 第一章
- <<python 核心编程>>第一章 正则表达式
- 第一章 正则表达式字符匹配攻略
- 第一章 正则表达式字符匹配攻略
- Python核心编程 第一章--正则表达式
- 《精通正则表达式》读书笔记摘要: 第一章 入门(1)
- 《精通正则表达式》读书笔记摘要: 第一章 入门(2)
- 《精通正则表达式》读书笔记摘要: 第一章 入门(3)
- 【代码之美】第一章——正则表达式匹配器
- linux shell 第一章 正则表达式及元字符组合
- C#高级学习第一章String和正则表达式
- 【正则表达式】正则表达式
- Python核心编程第三版练习参考(第一章:正则表达式)
- 正则表达式
- 正则表达式
- BZOJ2226 基础数论
- linux 下查看文件第多少行
- CPU卡程序设计实例(二十三)卡和ESAM之间内部认证
- Flask之Web表单使用
- CPU卡程序设计实例(二十四)生成过程密钥
- 第一章 正则表达式
- Linux环境下安装Tomcat
- C语言课程设计-MP3播放器(2)
- 阿里云服务器CentOS7配置Java Web项目运行环境(MySql,Tomcat,Jre)
- PAT1003. 我要通过!(20)
- 一种少见的Dll注入方式(基于MS 应用程序验证机制)
- php中函数参数传递
- 第一个AWT窗体
- Win10下OpenCV环境搭建(VS2017+OpenCV3.2.0)