Usually use a common regular class
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<String>{
let matches = self.internalExpression.matchesInString(input, options: nil, range:NSMakeRange(0, countElements(input))) as Array<NSTextCheckingResult>
if (matches.count>0) {
var values:Array<String> = []
for result in matches {
for i in 0...(result.numberOfRanges-1) {
let range = result.rangeAtIndex(i)
var nsinput = input as NSString
values += [nsinput.substringWithRange(NSRange(location: range.location,length: range.length))]
}
}
//println(values)
return values
}else{
return []
}
}
}
The regular expression is \d in double quotation marks to write \\d, such as Take 5 numbers "\\d{5}".
For example, take 3-30 characters (including numbers or letters or-) to write a
var matchResult = Regex("[\\d\\w-]{2,30}").test(domainInput.text)
if !matchResult.isEmpty {
println(matchResult[0])
}
Swift uses regular expressions