Python 正则表达式验证URL

本文介绍了一系列用于匹配URL的正则表达式规则,包括允许几乎所有URL格式、要求域名且不允许包含用户名或密码、允许省略协议头以及指定图片路径等场景。
1. Allow almost any URL
^(https?|ftp|file)://.+$

eg. 
https://nihao.com/dddni.jpg

2. Require a domain name, and donot allow a username or password
^(https?|ftp)://[a-zA-Z0-9-]+(\.[a-zA-Z0-9]+)+$

eg.
https://nihao.com

3. Require a domain name, and donot allow a username or password.
Allow the scheme (http or ftp) to be omitted if it can be inferred from the subdomain(www or ftp)
^((https?|ftp)://|(www|ftp)\.)[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+([/?].*)?$

eg.
https://nihao.com/dddni.jpg
www.nihao.com/dddni.jpg
http://www.nihao.com/dddni.jpg

4. Require a domain name and a path that points to an image file. Donot allow a username, password or parameters
^(https?|ftp)://[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+(/[\w-]+)*/[\w-]+\.(gif|png|jpg)$

eg. 
http://www.nihao.com/dddni.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值