Created
April 15, 2016 05:18
-
-
Save suhanlee/0889144525bccc5316bb9a090ad93732 to your computer and use it in GitHub Desktop.
regular expression swift example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Regex { | |
let internalExpression: NSRegularExpression? | |
let pattern: String | |
init(_ pattern: String) { | |
self.pattern = pattern | |
do { | |
self.internalExpression = try NSRegularExpression(pattern: pattern, options: .CaseInsensitive) | |
} catch { | |
self.internalExpression = nil | |
print("\(error)") | |
} | |
} | |
func test(input: String) -> Bool { | |
let matches = self.internalExpression?.matchesInString(input, options:[], range: NSMakeRange(0, input.characters.count)) | |
return matches?.count > 0 | |
} | |
} | |
var reg = Regex("^https?:\\/\\/.*") | |
if reg.test("http://www.example.com") { | |
print("pattern matched") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment