正则表达式练习
2017-11-09 本文已影响0人
tonyemail_st
匹配邮箱
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> y='123@qq.comaaa@163.combbb@126.comasdfasfs33333@adfcom'
>>> import re
>>> re.findall('\w+@(qq|163|126).com', y)
['qq', '163', '126']
>>> re.findall('\w+@(?:qq|163|126).com', y)
['123@qq.com', 'aaa@163.com', 'bbb@126.com']
匹配电话号码
>>> y = 'tel:010-12345678 address:changanRoad'
>>> re.findall('\d', y)
['0', '1', '0', '1', '2', '3', '4', '5', '6', '7', '8']
>>> re.findall('\d+', y)
['010', '12345678']
>>> re.findall('\d+-', y)
['010-']
>>> re.findall('\d+-\d+', y)
['010-12345678']
匹配本地磁盘路径,取出“C:\code\cnkiCrawl”
>>>y = 'localpath C:\code\cnkiCrawl'
>>>re.findall('[a-zA-Z]:\\\\\w+\\\\\w+', y)
['C:\\code\\cnkiCrawl']
匹配Hello Kitty Hello Hello Kitty Kitty Hello Kitty中的“Hello Hello Kitty Kitty”
>>> re.findall('(?:Hello ){2}(?:Kitty ){2}', y)
['Hello Hello Kitty Kitty ']
匹配aabaaabaaaab中的aab,而不匹配aaab,aaaab