Last active
June 8, 2016 10:40
-
-
Save craigsdennis/37db5a8859f578ecb314 to your computer and use it in GitHub Desktop.
Regular Expressions in Swift
This file contains hidden or 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
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