1. 程式人生 > >Python 正則表示式驗證URL

Python 正則表示式驗證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