regex - 在 Swift 3 中验证 URL

标签 regex swift

<分区>

我正在尝试在 Swift 3 中验证 URL,但我似乎找不到适合我需要的正则表达式。我所追求的正则表达式需要接受以下组合:

http://google.com
http://google.com/foo/bar:30/35
https://google.com
https://google.com/foo/bar:30/35
www.google.com
google.com

最佳答案

func validateUrl (urlString: NSString) -> Bool {
    let urlRegEx = "((?:http|https)://)?(?:www\\.)?[\\w\\d\\-_]+\\.\\w{2,3}(\\.\\w{2})?(/(?<=/)(?:[\\w\\d\\-./_]+)?)?"
    return NSPredicate(format: "SELF MATCHES %@", urlRegEx).evaluateWithObject(urlString)
}

这有效。

关于regex - 在 Swift 3 中验证 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39996840/

相关文章:

python - 获取干净数据: Beautiful Soup is enough or I must use Regex as well?

java - GWT : how to get regex(Pattern and Matcher) working in client side

ios - 如何避免 firestore 中的重复文件?

ios - GradientLayer 中的 Swift -Animation 未出现在单元格中

PHP 正则表达式修复被黑的 Wordpress 网站

Java 使用正则表达式匹配器获取组

python - 正则表达式不过是写在匹配

Swift Google Maps 圆滑的多段线

json - 将 UISearchController 与 swift 中的异步结果更新集成

ios - 使用 SwiftyJSONAccelerator 进行 Swift JSON 解析