码迷,mamicode.com
首页 > 其他好文 > 详细

二十七、正则表达式补充

时间:2018-08-02 17:42:44      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:name   ble   arch   import   strong   列表   groups   key   arc   


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("h\w+",origin)
print (r.group()) #获取匹配到得所有结果
print(r.groups()) #获取模型中匹配到的分组结果
print (r.groupdict()) #获取模型中匹配到的分组中的所有执行了key的分组
print ("-------------------------match1----------------------------")
---------------------------------------------------------------------
hello
()
{}
-------------------------match1----------------------------
---------------------------------------------------------------------

r=re.match("(h\w+)",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("(t\w+).*(?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+\w\d+","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---------------------------
---------------------------------------------------------------------

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

二十七、正则表达式补充

标签:name   ble   arch   import   strong   列表   groups   key   arc   

原文地址:https://www.cnblogs.com/chushujin/p/9408350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!