文末练习
1、匹配网址
有一批网址:
http://www.interoem.com/messageinfo.asp?id=35
http://3995503.com/class/class09/news_show.asp?id=14
http://lib.wzmc.edu.cn/news/onews.asp?id=769
http://www.zy-ls.com/alfx.asp?newsid=377&id=6http://www.fincm.com/newslist.asp?id=4
15
需要正则后为:
http://www.interoem.com/
http://3995503.com/
http://lib.wzmc.edu.cn/
http://www.zy-ls.com/
http://www.fincm.com/
import re
web1='''http://www.interoem.com/messageinfo.asp?id=35
http://3995503.com/class/class09/news_show.asp?id=14
http://lib.wzmc.edu.cn/news/onews.asp?id=769
http://www.zy-ls.com/alfx.asp?newsid=377&id=6http://www.fincm.com/newslist.asp?id=415'''
a=re.findall(r'[a-z]*\:\/\/\w*[.]*\w*[-]*\w*[.]*\w*[.]*\w*\/',web1)
print(a)
2、匹配合法的 ip 地址
import re
pattern='^((1[0-9][0-9]\.)|(2[0-4][0-9]\.)|(25[0-5]\.)|([1-9][0-9]\.)|([0-9]\.)){3}((1[0-9]
[0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))
$'
example=input('请输入一个 IP 地址:')
ip=re.search(pattern,example)
if ip:
print('正确:',ip.group())
else:
print('IP 地址错误!')