正则表达式练习:匹配手机号、匹配单词、匹配ip

1、匹配手机号

>>> re.findall(r"\d{11}","abc13988889999cde13810635189")
['13988889999', '13810635189']
>>>
>>> re.findall(r"\d{11}","abc13988889999cde1381063518923")
['13988889999', '13810635189']

 

>>> re.findall(r"\b\d{11}\b","abc 13988889999 cde 13810635189")
['13988889999', '13810635189']

 

2、匹配单词

>>> re.findall(r"[a-zA-Z]+","I am a good boy!You too!~")
['I', 'am', 'a', 'good', 'boy', 'You', 'too']

>>> len(re.findall(r"[a-zA-Z]+","I am a good boy!You too!~")) 7

 

3、匹配IP:xxx.xxx.xxx.xxx   四段数字,三个点(匹配最简单的,比较复杂的就不验证了)

>>> re.search(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}","0.1.22.168")
<_sre.SRE_Match object; span=(0, 10), match='0.1.22.168'>

 

上一篇:网页缓存


下一篇:爬虫获取pubmed中文献的标题和摘要