python中search用法_Python3中正则模块re.compile、re.match及re.search函数用法详解
本文实例讲述了Python3中正则模块re.compile、re.match及re.search函数用法。分享给大家供大家参考,具体如下:
re模块 re.compile、re.match、 re.search
正则匹配的时候,第一个字符是 r,表示 raw string 原生字符,意在声明字符串中间的特殊字符不用转义。
比如表示 ‘\n',可以写 r'\n',或者不适用原生字符 ‘\n'。
推荐使用 re.match
re.compile() 函数
编译正则表达式模式,返回一个对象。可以把常用的正则表达式编译成正则表达式对象,方便后续调用及提高效率。
re.compile(pattern, flags=0)
pattern 指定编译时的表达式字符串
flags 编译标志位,用来修改正则表达式的匹配方式。支持 re.L|re.M 同时匹配
flags 标志位参数
re.I(re.IGNORECASE)
使匹配对大小写不敏感
re.L(re.LOCAL)
做本地化识别(locale-aware)匹配
re.M(re.MULTILINE)
多行匹配,影响 ^ 和 $
re.S(re.DOTALL)
使 . 匹配包括换行在内的所有字符
re.U(re.UNICODE)
根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B.
re.X(re.VERBOSE)
该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。
示例:
1
2
3
4
5
6
7
import re
content= 'Citizen wang , always fall in love with neighbour,WANG'
rr= re.compile(r'wan\w', re.I)# 不区分大小写
print(type(rr))
a= rr.findall(content)
print(type(a))
print(a)
findall 返回的是一个 list 对象
['wang', 'WANG']
re.match() 函数
总是从字符串‘开头曲匹配',并返回匹配的字符串的 match 对象 。
re.match(pattern, string[, flags=0])
pattern 匹配模式,由 re.compile 获得
string 需要匹配的字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import re
pattern= re.compile(r'hello')
a= re.match(pattern,'hello world')
b= re.match(pattern,'world hello')
c= re.match(pattern,'hell')
d= re.match(pattern,'hello ')
if a:
print(a.group())
else:
print('a 失败')
if b:
print(b.group())
else:
print('b 失败')
if c:
print(c.group())
else:
print('c 失败')
if d:
print(d.group())
else:
print('d 失败')
hello
b 失败
c 失败
hello
match 的方法和属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import re
str = 'hello world! hello python'
pattern= re.compile(r'(?Phell\w)(?P\s)(?P.*ld!)')# 分组,0 组是整个 hello world!, 1组 hello,2组 ld!
match= re.match(pattern,str)
print('group 0:', match.group(0))# 匹配 0 组,整个字符串
print('group 1:', match.group(1))# 匹配第一组,hello
print('group 2:', match.group(2))# 匹配第二组,空格
print('group 3:', match.group(3))# 匹配第三组,ld!
print('groups:', match.groups())# groups 方法,返回一个包含所有分组匹配的元组
print('start 0:', match.start(0),'end 0:', match.end(0))# 整个匹配开始和结束的索引值
print('start 1:', match.start(1),'end 1:', match.end(1))# 第一组开始和结束的索引值
print('start 2:', match.start(1),'end 2:', match.end(2))# 第二组开始和结束的索引值
print('pos 开始于:', match.pos)
print('endpos 结束于:', match.endpos)# string 的长度
print('lastgroup 最后一个被捕获的分组的名字:', match.lastgroup)
print('lastindex 最后一个分组在文本中的索引:', match.lastindex)
print('string 匹配时候使用的文本:', match.string)
print('re 匹配时候使用的 Pattern 对象:', match.re)
print('span 返回分组匹配的 index (start(group),end(group)):', match.span(2))
返回结果:
group 0: hello world!
group 1: hello
group 2:
group 3: world!
groups: ('hello', ' ', 'world!')
start 0: 0 end 0: 12
start 1: 0 end 1: 5
start 2: 0 end 2: 6
pos 开始于: 0
endpos 结束于: 25
lastgroup 最后一个被捕获的分组的名字: last
lastindex 最后一个分组在文本中的索引: 3
string 匹配时候使用的文本: hello world! hello python
re 匹配时候使用的 Pattern 对象: re.compile('(?Phell\\w)(?P\\s)(?P.*ld!)')
span 返回分组匹配的 index (start(group),end(group)): (5, 6)
re.search 函数
对整个字符串进行搜索匹配,返回第一个匹配的字符串的 match 对象。
re.search(pattern, string[, flags=0])
pattern 匹配模式,由 re.compile 获得
string 需要匹配的字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import re
str = 'say hello world! hello python'
pattern= re.compile(r'(?Phell\w)(?P\s)(?P.*ld!)')# 分组,0 组是整个 hello world!, 1组 hello,2组 ld!
search= re.search(pattern,str)
print('group 0:', search.group(0))# 匹配 0 组,整个字符串
print('group 1:', search.group(1))# 匹配第一组,hello
print('group 2:', search.group(2))# 匹配第二组,空格
print('group 3:', search.group(3))# 匹配第三组,ld!
print('groups:', search.groups())# groups 方法,返回一个包含所有分组匹配的元组
print('start 0:', search.start(0),'end 0:', search.end(0))# 整个匹配开始和结束的索引值
print('start 1:', search.start(1),'end 1:', search.end(1))# 第一组开始和结束的索引值
print('start 2:', search.start(1),'end 2:', search.end(2))# 第二组开始和结束的索引值
print('pos 开始于:', search.pos)
print('endpos 结束于:', search.endpos)# string 的长度
print('lastgroup 最后一个被捕获的分组的名字:', search.lastgroup)
print('lastindex 最后一个分组在文本中的索引:', search.lastindex)
print('string 匹配时候使用的文本:', search.string)
print('re 匹配时候使用的 Pattern 对象:', search.re)
print('span 返回分组匹配的 index (start(group),end(group)):', search.span(2))
注意 re.search 和 re.match 匹配的 str 的区别
打印结果:
group 0: hello world!
group 1: hello
group 2:
group 3: world!
groups: ('hello', ' ', 'world!')
start 0: 4 end 0: 16
start 1: 4 end 1: 9
start 2: 4 end 2: 10
pos 开始于: 0
endpos 结束于: 29
lastgroup 最后一个被捕获的分组的名字: last
lastindex 最后一个分组在文本中的索引: 3
string 匹配时候使用的文本: say hello world! hello python
re 匹配时候使用的 Pattern 对象: re.compile('(?Phell\\w)(?P\\s)(?P.*ld!)')
span 返回分组匹配的 index (start(group),end(group)): (9, 10)
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
希望本文所述对大家Python程序设计有所帮助。
python中search用法_Python3中正则模块re.compile、re.match及re.search函数用法详解相关推荐
- python中import re_Python3中正则模块re.compile、re.match及re.search函数用法详解
本文实例讲述了Python3中正则模块re.compile.re.match及re.search函数用法.分享给大家供大家参考,具体如下: re模块 re.compile.re.match. re.s ...
- python six模块详解_对python中的six.moves模块的下载函数urlretrieve详解
实验环境:windows 7,anaconda 3(python 3.5),tensorflow(gpu/cpu) 函数介绍:所用函数为six.moves下的urllib中的函数,调用如下urllib ...
- python format函数实例_python中强大的format函数实例详解
python中format函数用于字符串的格式化 自python2.6开始,新增了一种格式化字符串的函数str.format(),此函数可以快速处理各种字符串. 语法 它通过{}和:来代替%. 请看下 ...
- python中subplot是什么意思_python matplotlib中的subplot函数使用详解
python里面的matplotlib.pylot是大家比较常用的,功能也还不错的一个包.基本框架比较简单,但是做一个功能完善且比较好看整洁的图,免不了要网上查找一些函数.于是,为了节省时间,可以一劳 ...
- python中mat函数_python matplotlib中的subplot函数使用详解
python里面的matplotlib.pylot是大家比较常用的,功能也还不错的一个包.基本框架比较简单,但是做一个功能完善且比较好看整洁的图,免不了要网上查找一些函数.于是,为了节省时间,可以一劳 ...
- python语言中split-python中的split()函数和os.path.split()函数使用详解
Python中有split()和os.path.split()两个函数: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. os.path.split():将文件 ...
- python getopt_python 5种 statsPython中的getopt函数使用详解
函数原型: getopt.getopt(args, shortopts, longopts=[]) 参数解释: args:args为需要解析的参数列表.一般使用sys.argv[1:],这样可以过滤掉 ...
- python中def func是什么意思_Python的函数参数详解
原标题:Python的函数参数详解 前言 Python中函数的参数可以分为两大类形参和实参~ def func(x, y): # x, y 就是形参 print(x, y) func(2, 3) # ...
- python中的complex是什么意思_Python 内置函数complex详解,pythoncomplex
Python 内置函数complex详解,pythoncomplex 英文文档: class complex([real[, imag]]) Return a complex number with ...
最新文章
- 《Cacti实战》——第1章 认识Cacti
- UML科普文,一篇文章掌握14种UML图
- CISCO-CCNA课程介绍
- php intl make 错误,无法在Debian上为php安装’intl’扩展名
- tag+标签+php,ZBLOG PHP代码实现侧栏彩色标签TAG关键字样式方法
- Java黑皮书课后题第3章:*3.20(科学:风寒温度)编写一个程序,提示用户输入一个温度值和一个风速值。如果输入值合法,那么显示风寒温度,否则显示温度或风速是不合法数据
- Spring Boot集成JPA的Column注解命名字段无效的问题
- strstr 函数的实现
- php mvc 路由,PHP MVC框架路由学习笔记
- 【转】电脑显示有软驱
- 非关系数据库-NoSQL探讨
- java学生成绩管理系统源码(java学生信息管理 java成绩管理系统 java学生信息管理系统)
- json html api文档,jsonEditor api介绍
- Bzoj1001 [BeiJing2006]狼抓兔子
- epson LQ 600KIIH 针式打印机走纸问题
- iar 堆栈设置_IAR开发STM32堆栈设置
- 《Linux运维实战:搭建自己的Confluence知识管理系统》
- PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版
- Highcharts插件常用配置
- php hook 键盘,python使用pyhook监控键盘并实现切换歌曲的功能