标签:io ar sp on bs amp as tt res
swift的正则表达式,可以自定义一个类,方便调用
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) -> Bool {
let matches = self.internalExpression.matchesInString(input, options: nil, range:NSMakeRange(0, countElements(input)))
return matches.count > 0
}
}
实例调用
if Regex("^[a-zA-Z]{3,20}$").test("ABC") {
println("符合")
} else {
println("不符合")
}
标签:io ar sp on bs amp as tt res
原文地址:http://www.cnblogs.com/--wu/p/4094984.html