正则表达式练习

匹配邮箱

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='[email protected]@[email protected]@adfcom'
>>> import re
>>> re.findall('\w+@(qq|163|126).com', y)
['qq', '163', '126']
>>> re.findall('\w+@(?:qq|163|126).com', y)
['[email protected]', '[email protected]', '[email protected]']

匹配电话号码

>>> 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

你可能感兴趣的:(正则表达式练习)