Python入门到精通

Python基础022--正则表达式

2018-03-05  本文已影响3人  不一样的丶我们

正则表达式模块、贪婪匹配和非贪婪匹配、正则分组、match和search的使用

表达式 描述
^ 匹配开头
$ 匹配结束
. 匹配任意..字符
x? 匹配字符x出现0次或者1次
x+ 匹配字符x出现1次或者多次
x* 匹配字符x出现0次或者多次
(xy)+ 分组的方式,匹配字符串xy出现1次或者多次
[xy]+ 匹配字符x或者y出现了1次或者多次
[0-9]+ 匹配一个数字出现了1次或者多次
[a-z]+ 匹配一个小写字母出现了1次或者多次
[A-Z]+ 匹配一个大写字母出现了1次或者多次
[a-zA-Z]+ 匹配一个字母[大小写均可]出现了1次或者多次
x{m,} 匹配字符x出现了m次以上
x{m,n} 匹配字符x出现了m次以上n次以下
$ 匹配字符出现了n次以下
... ...
\d 匹配一个数字,和[0-9]相同
\D 匹配一个非数字,和[^0-9]相同
\w 匹配一个字母数字或者下划线,和[a-zA-Z0-9_]相同
\s 匹配一个空白字符
\b 匹配一个字符的边缘位置 hello worhellold : \bhello\b
... ...
* 贪婪匹配符,和其他表达式一起使用
? 懒惰匹配符,和其他表达式一起使用
# re.match()使用方法
In [3]: print(re.match('com','comwww.runcomob').group())
com
In [5]: print(re.match('com','www.rencomob'))-->匹配成功,但不是从起始位置匹配,返回None
None
In [6]: print(re.match('com','Comwww.rencomob',re.I|re.M).groups())-->返回子元组,没有子元组返回空元组
()
In [7]: print(re.match('com','Comwww.rencomob',re.I|re.M).group())
Com
In [8]: print(re.match('com','Comwww.rencomob',re.I))
<_sre.SRE_Match object at 0x7f653548bd98>
In [9]: print(re.match('com','Comwww.rencomob',re.I).group())
Com

# re.search()使用方法
In [10]: print(re.search('www','www.runoog.com').span())    # 在起始位置匹配
(0, 3)
In [11]: print(re.search('com','www.runoog.com').span())    # 不在起始位置匹配
(11, 14)
In [12]: a = '132abc456'
In [13]: print(re.search('([0-9]*)([a-z]*)([0-9]*)',a).group())
132abc456
In [14]: print(re.search('([0-9]*)([a-z]*)([0-9]*)',a).group(1))    # group()可以有参数,确定匹配第几个对象
132
In [15]: print(re.search('([0-9]*)([a-z]*)([0-9]*)',a).group(2))
abc
In [16]: print(re.search('([0-9]*)([a-z]*)([0-9]*)',a).group(3))
456
In [17]: print(re.search('([0-9]*)([a-z]*)([0-9]*)',a).groups())    # groups()无参数,返回全部的子组
('132', 'abc', '456')

In [22]: pattern = re.compile(r'\d+')
In [23]: m = pattern.match('one123twothree34four')
In [24]: print m
None
In [25]: m = pattern.match('one123twothree34four',3,10)
In [26]: print m 
<_sre.SRE_Match object at 0x7f6535575238>
In [27]: m.start()
Out[27]: 3
In [28]: m.end()
Out[28]: 6
In [29]: m.span()
Out[29]: (3, 6)
In [30]: m = pattern.match('one123twothree34four',3,10).group()
In [31]: print m
123
In [35]: p = re.compile(r'\d+')
In [36]: res = p.findall('o1n2m3k4')
In [37]: print res
['1', '2', '3', '4']
In [38]: tt = 'Tina is good girl, she is cool, clever, and so on..'
In [40]: rr = re.compile(r'\w*oo\w*')
In [41]: res = rr.findall(tt)
In [42]: print res
['good', 'cool']
In [44]: res = re.findall(r'\w*oo\w*',tt)
In [45]: print res
['good', 'cool']

In [49]: s = 'http://www.baidu.com/new.html;http://news.sina.com/music.html'
In [50]: p1 = re.compile(r'http://.+\.html')
In [52]: p1.search(s).group()
Out[52]: 'http://www.baidu.com/new.html;http://news.sina.com/music.html'
In [53]: p2 = re.compile(r'http://.+?\.html')
In [54]: p2.search(s).group()
Out[54]: 'http://www.baidu.com/new.html'

In [55]: data = 'Thu Feb 15 17:46:04 2007::uzifzf@dpyivihw.gov:: 2341123-6-8'
In [56]: patt = re.compile(r'.+(\d+-\d-\d)', re.X)
In [57]: print patt.match(data).group()
Thu Feb 15 17:46:04 2007::uzifzf@dpyivihw.gov:: 2341123-6-8
In [58]: print patt.match(data).group(1)
3-6-8
In [59]: patt = re.compile(r'.+?(\d+-\d-\d)', re.X)
In [60]: print patt.match(data).group(1)
2341123-6-8

# 正则表达式分组
In [64]: s='<div><a href="https://support.google.com/chrome/?p=ui_hotword_search" rel="extern
    ...: al nofollow" target="_blank">更多</a><p>dfsl</p></div>'
In [66]: print re.search(r'<a.*>(.*)</a>',s).group(1)
更多
In [67]: print re.search(r'<a.*>(.*)</a>',s).group()
<a href="https://support.google.com/chrome/?p=ui_hotword_search" rel="external nofollow" target="_blank">更多</a>

# 命名分组
In [77]: p = re.compile(r'(?P<word>\b\w+\b)')
In [78]: m = p.search('Lots of punctuation')
In [79]: m.group('word')
Out[79]: 'Lots'
In [80]: m.group()
Out[80]: 'Lots'

URL可以通过named group方式传递指定参数,语法为: (?P<name>pattern), name 可以理解为所要传递的参数的名称,pattern代表所要匹配的模式

eg:url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive)
那么year,month将会对应views传递过来的year,month的值,而后面紧跟的则代表正则表达匹配的模式

上一篇 下一篇

猜你喜欢

热点阅读