Created
November 24, 2014 20:17
-
-
Save bradley/ed290aab1a2f5cfb21d1 to your computer and use it in GitHub Desktop.
swift output ints from regex
This file contains 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
// Playground - noun: a place where people can play | |
import Foundation | |
class Regex { | |
let internalExpression: NSRegularExpression | |
let pattern: String | |
init(_ pattern: String) { | |
self.pattern = pattern | |
var error: NSError? | |
self.internalExpression = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: &error)! | |
} | |
func test(input: String) -> Array<AnyObject> { | |
let matches: Array = self.internalExpression.matchesInString(input, options: nil, range:NSMakeRange(0, countElements(input))) | |
return matches | |
} | |
} | |
var str = "Hello I am a string 123 and I have numbers hail satan 666" | |
var matches = Regex("\\d").test(str) | |
for match in matches { | |
let string = str as NSString | |
let capture_int: String = string.substringWithRange(match.range) | |
println(capture_int) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment