Skip to content

Instantly share code, notes, and snippets.

@s3itz
Created May 15, 2015 01:38
Show Gist options
  • Save s3itz/7c789c349acfb11e5583 to your computer and use it in GitHub Desktop.
Save s3itz/7c789c349acfb11e5583 to your computer and use it in GitHub Desktop.
String.scan ...
import Cocoa
extension String {
func scan(regex: String) -> [String] {
let regex = NSRegularExpression(pattern: regex,
options: nil, error: nil)!
let nsString = self as NSString
let results = regex.matchesInString(self, options: NSMatchingOptions(0), range: NSMakeRange(0, (self as NSString).length))
as! [NSTextCheckingResult]
return map(results) { nsString.substringWithRange($0.range)}
}
}
let x = "Hello World".scan("l")
println(x.count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment