python之re模块

背景:

在写监控脚本过程中发现会使用到正则表达式以及re模块来匹配相应内容并校验判断,然后在使用过程中对re模块下的一些方法使用场景不是很熟悉,所以对re模块进行一个简单概要的学习和记录,下次即使忘记了也可以参照这篇快速回忆

参考地址:https://www.cnblogs.com/chengege/p/11190782.html

一,re模块之re.compile()

re.compile(string)就是返回一个pattern对象,参数是原生字符串对象

pattern=re.compile('##(.+?)##')
print(pattern)
#输出
re.compile('##(.+?)##')

二,re模块之re.match(pattern, string)

特点:
1,会从string的开头开始,尝试匹配pattern,一直向后匹配,如果遇到无法匹配的字符,立即返回None。如果不是开始位置匹配成功的话,match()就返回None

2,匹配未结束已经到达string的末尾,也会返回None

3,匹配到值时会返回一个对象,比如返回对象:<_sre.SRE_Match object; span=(0, 4), match='shjf'>返回的对象不能直接使用,需要使用返回对象.group()获取到匹配的值

4,匹配到值后就会停止继续匹配,不再匹配其它符合条件的值

注意:
只有re.match()方法是从开始位置匹配,其它的方法都是扫描整个string查找匹配

#例子1:这种情况匹配不到值,返回None(因为是从开头开始匹配)
pattern=re.compile('s(.+?)f')
b="3shjf33"
ss=re.match(pattern,b)
print(ss)    
#打印:
None

#例子1:匹配到值后就会停止继续匹配,
pattern=re.compile('s(.+?)f')
b="shjf33"
ss=re.match(pattern,b)
print(ss)
print(ss.group())
#打印:  
<_sre.SRE_Match object; span=(0, 4), match='shjf'>
shjf

三,re模块之re.search(pattern, string)

特点:
1,与match方法极其类似,区别在于search()会扫描整个string查找匹配,不是从开始位置匹配

2,其它与match一致:(1)匹配不到值返回None;(2)只返回第一个匹配到的值;(3)使用group()方法获取匹配到的值

pattern = re.compile(r'world')
match = re.search(pattern,'hello, world! ,world!')  
print(match)
print(match.group())  
#打印
<_sre.SRE_Match object; span=(0, 5), match='world'>
world

四,re模块之re.split(pattern, string)

特点:
1,扫描整个string查找匹配,无论是否匹配导致,返回对象一定是list列表类型

2,当没有匹配到值时,会将整个字符串作为值返回

3,匹配到值时,将匹配到的string值作为分割线后返回列表,当最开始或者最末尾匹配时,分割成空字符串

4,没有group()方法

#例子1:
pattern = re.compile(r'world')
match = re.split(pattern,'hello,wod')  #没有匹配到值
print(match)
#打印:
['hello,wod']

#例子2:
pattern = re.compile(r'world')
match = re.split(pattern,'world,heworld,llo,world')  #匹配到多个值
print(match)
#打印:
['', ',he', ',llo,', '']  #当最开始或者最末尾匹配时,会分割成空字符串

五,re模块之re.findall(pattern, string)

特点:
1,扫描整个string查找匹配,无论是否匹配到值,返回对象一定是list列表类型

2,当没有匹配到值时,返回空列表[]

3,匹配到值时,将匹配到的string值作为分割线后返回列表,当最开始或者最末尾匹配时,分割成空字符串

4,没有group()方法

pattern = re.compile(r'w(.+?)d')
match = re.findall(pattern,'hello,world! hello,w11d! hello,wssd! hello,orld')
print(match)
#打印
['orl', '11', 'ss']

六,re模块之re.finditer(pattern, string)

特点:
1,扫描整个string查找匹配,无论是否匹配到值,返回对象一定是迭代器,即使没有匹配导致,在for循环中使用group()方法也不会报错,只是没有值

2,匹配到值时,是存在迭代器中,需要使用for循环获取匹配值

4,有group()方法

#例子1
match = re.finditer(pattern,'hello,lo,orld')
print(match)
for i in match:
    print(i.group())  #没有匹配到值,使用group()也不会报错
#打印
<callable_iterator object at 0x03473D90>

#例子2
pattern = re.compile(r'w(.+?)d')
match = re.finditer(pattern,'hello,world! hello,w11d! hello,wssd! hello,orld')
print(match)
for i in match:
    print(i.group())
#打印
<callable_iterator object at 0x03DE3D90>
world
w11d
wssd
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python的re模块是一种用于匹配字符串的模块,它基于正则表达式实现。正则表达式可以模糊匹配字符串,并提取所需的字符串部分。re模块Python中是唯一的,并且在所有编程语言中都可以使用。通常,我们使用re模块的compile函数生成pattern对象,然后使用该对象调用相应的方法进行正则匹配。不过在大多数情况下,在Python中真的不需要使用re.compile函数,可以直接使用re模块的对应方法进行操作。例如,常用的正则表达式方法,如findall、search、sub和match,可以直接使用re模块的函数进行调用。在这些方法中,compile函数的调用已经被放在了对应方法的实现中。因此,我们可以直接使用re模块的函数进行正则匹配,而不需要显式地调用compile函数。例如,可以使用re.findall(pattern, string)进行匹配操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python之re模块](https://blog.csdn.net/cxu123321/article/details/104075998)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python中的re模块](https://blog.csdn.net/m0_43609475/article/details/119924047)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值