二十七正则表达式补充

Posted chushujin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二十七正则表达式补充相关的知识,希望对你有一定的参考价值。


import re

‘‘‘
正则表达式:
re.match:从头匹配
re.search:浏览全部字符串,匹配第一个符合规则的字符串
re.findall():将匹配到得的所有内容都放置在一个列表中
#re.finditer():
re.split():
re.sub():
‘‘‘

‘‘‘
1.match
‘‘‘
origin = "hello tom bcd tom lge tom acd 19"
r=re.match("hw+",origin)
print (r.group()) #获取匹配到得所有结果
print(r.groups()) #获取模型中匹配到的分组结果
print (r.groupdict()) #获取模型中匹配到的分组中的所有执行了key的分组
print ("-------------------------match1----------------------------")
---------------------------------------------------------------------
hello
()
{}
-------------------------match1----------------------------
---------------------------------------------------------------------

r=re.match("(hw+)",origin)
print (r.group())
print(r.groups())
print (r.groupdict())
print ("--------------------------match2---------------------------")
---------------------------------------------------------------------
hello
(‘hello‘,)
{}
--------------------------match2---------------------------
---------------------------------------------------------------------

r=re.match("(h)(w+)",origin)
print (r.group())
print(r.groups())
print (r.groupdict())
print ("--------------------------match3---------------------------")
---------------------------------------------------------------------
hello
(‘h‘, ‘ello‘)
{}
--------------------------match3---------------------------
---------------------------------------------------------------------

r=re.match("(?P<n1>h)(?P<n2>w+)",origin)
print (r.group())
print(r.groups())
print (r.groupdict())
print ("--------------------------match4---------------------------")

---------------------------------------------------------------------


hello
(‘h‘, ‘ello‘)
{‘n1‘: ‘h‘, ‘n2‘: ‘ello‘}
--------------------------match4---------------------------
---------------------------------------------------------------------

‘‘‘
2.search:全字符串匹配
‘‘‘
origin = "hello tom bcd tom lge tom acd 19"
r=re.search("(tw+).*(?P<name>d)$",origin)
print (r.group()) #获取匹配到得所有结果
print(r.groups()) #获取模型中匹配到的分组结果
print (r.groupdict()) #获取模型中匹配到的分组中的所有执行了key的分组
print ("--------------------------search1---------------------------")
---------------------------------------------------------------------

tom bcd tom lge tom acd 19
(‘tom‘, ‘9‘)
{‘name‘: ‘9‘}
--------------------------search1---------------------------
---------------------------------------------------------------------

origin = "hello tom bcd tom lge tom acd 19"
r=re.search("t(w+)",origin)
print (r.group()) #获取匹配到得所有结果
print(r.groups()) #获取模型中匹配到的分组结果
print (r.groupdict()) #获取模型中匹配到的分组中的所有执行了key的分组
print ("--------------------------search2---------------------------")
---------------------------------------------------------------------
tom
(‘om‘,)
{}
--------------------------search2---------------------------
---------------------------------------------------------------------

‘‘‘
3.findall:匹配到的字符串放到列表(分组和不分组)
分组提取:从左到右,从外到内,有几个括号就取几次
‘‘‘
r=re.findall("d+wd+","a2b3c4d5")
print (r)
print ("--------------------------findall1---------------------------")
---------------------------------------------------------------------

[‘2b3‘, ‘4d5‘]
--------------------------findall1---------------------------
---------------------------------------------------------------------

r=re.findall("","a2b3c4d5")
print (r)
print ("--------------------------findall2---------------------------")
---------------------------------------------------------------------
[‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘]
--------------------------findall2---------------------------
---------------------------------------------------------------------

origin = "hello tomm bcd tomm lge tomm acd 19"
r=re.findall("(t)(w+)(m)",origin) #(w+)中显示search中groups中的所有元素
print (r)
print ("--------------------------findall3---------------------------")
---------------------------------------------------------------------
[(‘t‘, ‘om‘, ‘m‘), (‘t‘, ‘om‘, ‘m‘), (‘t‘, ‘om‘, ‘m‘)]
--------------------------findall3---------------------------
---------------------------------------------------------------------

origin = "hello tomm bcd tomm lge tomm acd 19"
r=re.findall("((t)(w+)(m))",origin) #(w+)中显示search中groups中的所有元素
print (r)
print ("--------------------------findall4---------------------------")
---------------------------------------------------------------------
[(‘tomm‘, ‘t‘, ‘om‘, ‘m‘), (‘tomm‘, ‘t‘, ‘om‘, ‘m‘), (‘tomm‘, ‘t‘, ‘om‘, ‘m‘)]
--------------------------findall4---------------------------
---------------------------------------------------------------------

origin = "hello tomn bcd tomn lge tomn acd 19"
r=re.findall("(t)(w+(m))(n)",origin) #(w+)中显示search中groups中的所有元素
print (r)
print ("--------------------------findall5---------------------------")
---------------------------------------------------------------------
[(‘t‘, ‘om‘, ‘m‘, ‘n‘), (‘t‘, ‘om‘, ‘m‘, ‘n‘), (‘t‘, ‘om‘, ‘m‘, ‘n‘)]
--------------------------findall5---------------------------
---------------------------------------------------------------------

‘‘‘
4.finditer()
‘‘‘
origin = "hello tomn bcd tomn lge tomn acd 19"
r=re.finditer("(t)(w+(m))(?P<name>n)",origin) #(w+)中显示search中groups中的所有元素
print (r)
for i in r:
print (r)
print (i.group())
print(i.groups())
print(i.groupdict())
print ("--------------------------finditer1---------------------------")
---------------------------------------------------------------------

<callable_iterator object at 0x00000000025CF940>
<callable_iterator object at 0x00000000025CF940>
tomn
(‘t‘, ‘om‘, ‘m‘, ‘n‘)
{‘name‘: ‘n‘}
<callable_iterator object at 0x00000000025CF940>
tomn
(‘t‘, ‘om‘, ‘m‘, ‘n‘)
{‘name‘: ‘n‘}
<callable_iterator object at 0x00000000025CF940>
tomn
(‘t‘, ‘om‘, ‘m‘, ‘n‘)
{‘name‘: ‘n‘}
--------------------------finditer1---------------------------
---------------------------------------------------------------------

============================================================================================================














































































































































以上是关于二十七正则表达式补充的主要内容,如果未能解决你的问题,请参考以下文章

Linux学习笔记(二十七)sed

Puppet正则表达式(十七)

十七. 正则以及grep ,sed,awk的简单应用

python五十七课——正则表达式(多个字符)

SQL 基础正则表达式(二十三)

Java Review (二十正则表达式)