Created
May 15, 2015 01:38
-
-
Save s3itz/7c789c349acfb11e5583 to your computer and use it in GitHub Desktop.
String.scan ...
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
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