Skip to content

Instantly share code, notes, and snippets.

@suhanlee
Created April 15, 2016 05:18
Show Gist options
  • Save suhanlee/0889144525bccc5316bb9a090ad93732 to your computer and use it in GitHub Desktop.
Save suhanlee/0889144525bccc5316bb9a090ad93732 to your computer and use it in GitHub Desktop.
regular expression swift example
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