Created
June 11, 2014 11:01
-
-
Save frozzare/e2f58b897ed74c8478ae to your computer and use it in GitHub Desktop.
Regex example
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 Foundation | |
extension String { | |
func exec (str: String) -> Array<String> { | |
var err : NSError? | |
let regex = NSRegularExpression(pattern: self, options: NSRegularExpressionOptions(0), error: &err) | |
if err { | |
return Array<String>() | |
} | |
let nsstr = str as NSString | |
let all = NSRange(location: 0, length: nsstr.length) | |
var matches : Array<String> = Array<String>() | |
regex.enumerateMatchesInString(str, options: NSMatchingOptions(0), range: all) { | |
(result : NSTextCheckingResult!, _, _) in | |
matches.append(nsstr.substringWithRange(result.range)) | |
} | |
return matches | |
} | |
} | |
let res = "\\w+".exec("Hello world") | |
println(res) // ["Hello", "world"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following works in Swift 2.0: