Skip to content

Commit fab5e41

Browse files
authored
Improve URL pattern matching (#2248)
* Improve URL pattern matching * Add more url matching tests
1 parent a77db05 commit fab5e41

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Source/Validations/RuleRegExp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Foundation
2626

2727
public enum RegExprPattern: String {
2828
case EmailAddress = "^[_A-Za-z0-9-+!?#$%'`*/=~^{}|]+(\\.[_A-Za-z0-9-+!?#$%'`*/=~^{}|]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z‌​]{2,})$"
29-
case URL = "((https|http)://)((\\w|-)+)(([.]|[/])((\\w|-)+))+([/?#]\\S*)?"
29+
case URL = "^(?:(?:http|https)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$"
3030
case ContainsNumber = ".*\\d.*"
3131
case ContainsCapital = "^.*?[A-Z].*?$"
3232
case ContainsLowercase = "^.*?[a-z].*?$"

Tests/ValidationsTests.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,14 @@ class ValidationsTests: XCTestCase {
167167
XCTAssertNil(urlRule.isValid(value: nil))
168168
XCTAssertNil(urlRule.isValid(value: URL(string: "")))
169169
XCTAssertNil(urlRule.isValid(value: URL(string: "http://example.com")))
170+
XCTAssertNil(urlRule.isValid(value: URL(string: "http://to.co")))
170171
XCTAssertNil(urlRule.isValid(value: URL(string: "https://example.com/path/to/file.ext?key=value#location")))
171-
172+
XCTAssertNil(urlRule.isValid(value: URL(string: "https://example.com:8080/path/to/file.ext?key=value#location")))
173+
XCTAssertNil(urlRule.isValid(value: URL(string: "https://localhost")))
174+
XCTAssertNil(urlRule.isValid(value: URL(string: "https://localhost:8080")))
175+
172176
XCTAssertNotNil(urlRule.isValid(value: URL(string: "example.com")))
177+
XCTAssertNotNil(urlRule.isValid(value: URL(string: "www.example.com")))
173178
XCTAssertNotNil(urlRule.isValid(value: URL(string: "http://")))
174179
}
175180
}

0 commit comments

Comments
 (0)