概述
1. PHP (use with preg_match)
%^(?:(?:https?|ftp)://)(?:S+(?::S*)?@|d{1,3}(?:.d{1,3}){3}|(?:(?:[a-zdx{00a1}-x{ffff}]+-?)*[a-zdx{00a1}-x{ffff}]+)(?:.(?:[a-zdx{00a1}-x{ffff}]+-?)*[a-zdx{00a1}-x{ffff}]+)*(?:.[a-zx{00a1}-x{ffff}]{2,6}))(?::d+)?(?:[^s]*)?$%iu
2. PHP (with validate filter)
if (filter_var($url, FILTER_VALIDATE_URL) !== false)...
3. Python
http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+
4. Javascript
/((([A-Za-z]{3,9}:(?://)?)(?:[-;:&=+$,w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=+$,w]+@)[A-Za-z0-9.-]+)((?:/[+~%/.w-_]*)???(?:[-+=&;%@.w_]*)#?(?:[.!/\w]*))?)/
5. HTML5
<input type="url" />
5.1 Perl
^(((ht|f)tp(s?))://)?(www.|[a-zA-Z].)[a-zA-Z0-9-.]+.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(:[0-9]+)*(/($|[a-zA-Z0-9.,;?'+&%$#=~_-]+))*$
5.2 Ruby
/A(?:(?:https?|ftp)://)(?:S+(?::S*)?@)?(?:(?!10(?:.d{1,3}){3})(?!127(?:.d{1,3}){3})(?!169.254(?:.d{1,3}){2})(?!192.168(?:.d{1,3}){2})(?!172.(?:1[6-9]|2d|3[0-1])(?:.d{1,3}){2})(?:[1-9]d?|1dd|2[01]d|22[0-3])(?:.(?:1?d{1,2}|2[0-4]d|25[0-5])){2}(?:.(?:[1-9]d?|1dd|2[0-4]d|25[0-4]))|(?:(?:[a-zu00a1-uffff0-9]+-?)*[a-zu00a1-uffff0-9]+)(?:.(?:[a-zu00a1-uffff0-9]+-?)*[a-zu00a1-uffff0-9]+)*(?:.(?:[a-zu00a1-uffff]{2,})))(?::d{2,5})?(?:/[^s]*)?z/i
6. Go (use the govalidator IsURL())
package main
import (
"fmt"
"github.com/asaskevich/govalidator"
)
func main() {
str := "http://www.urlregex.com"
validURL := govalidator.IsURL(str)
fmt.Printf("%s is a valid URL : %v n", str, validURL)
}
7. Objective-C
(http|https)://((\w)*|([0-9]*)|([-|_])*)+([\.|/]((\w)*|([0-9]*)|([-|_])*))+
8. Swift
((?:http|https)://)?(?:www.)?[wd-_]+.w{2,3}(.w{2})?(/(?<=/)(?:[wd-./_]+)?)?
8.1 Use it in a function:
func canOpenURL(string: String?) -> Bool {
let regEx = "((https|http)://)((\w|-)+)(([.]|[/])((\w|-)+))+"
let predicate = NSPredicate(format:"SELF MATCHES %@", argumentArray:[regEx])
return predicate.evaluateWithObject(string)
}
8.2 Usage:
if canOpenURL("http://www.urlregex.com") {
print("valid url.")
} else {
print("invalid url.")
}
9. Swift (use canOpenURL)
UIApplication.sharedApplication().canOpenURL(urlString)
10. Java
^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]
11. VB.NET
(http(s)?://)?([w-]+.)+[w-]+[.com]+(/[/?%&=]*)?
12. C#
^(ht|f)tp(s?)://[0-9a-zA-Z]([-.w]*[0-9a-zA-Z])*(:(0-9)*)*(/?)([a-zA-Z0-9-.?,'/+&%$#_]*)?$
13. MySQL
SELECT field FROM table
WHERE field
REGEXP "^(https?://|www.)[.A-Za-z0-9-]+.[a-zA-Z]{2,4}
14. Bonus: What does the following regex do?
/^1?$|^(11+?)1+$/
最后
以上就是高兴铃铛为你收集整理的正则表达式匹配URL的全部内容,希望文章能够帮你解决正则表达式匹配URL所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复