Skip to content

Instantly share code, notes, and snippets.

@craigsdennis
Last active June 8, 2016 10:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigsdennis/37db5a8859f578ecb314 to your computer and use it in GitHub Desktop.
Save craigsdennis/37db5a8859f578ecb314 to your computer and use it in GitHub Desktop.
Regular Expressions in Swift
let title = "Regular Expressions in 10 different languages"
// Match
let pattern = ".*\\d.*"
if let match = title.rangeOfString(pattern, options: .RegularExpressionSearch) {
println("We had a match!")
}
let re = NSRegularExpression(pattern: "(\\d+)", options: nil, error: nil)!
let matches = re.matchesInString(
title,
options: nil,
range: NSRange(location: 0, length: count(title.utf16))
)
for match in matches as! [NSTextCheckingResult] {
let substring = (title as NSString).substringWithRange(match.rangeAtIndex(1))
println("There are \(substring) languages represented.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment