The OC version:

// Check mobile phone number - (BOOL)checkoutPhoneNum: (NSString *)phoneNum {NSString *regexStr = @"^ 1 \ \ d {9} [3] 14,7,9 [5] | \ \ d {8} | 15 [^ 4] \ \ d {8} | 17 [^ 2,4,9] \ \ d {8} $";
    NSError *error;
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
    if (error) return NO;
    NSInteger count = [regular numberOfMatchesInString:phoneNum options:NSMatchingReportCompletion range:NSMakeRange(0, phoneNum.length)];
    if (count > 0) {
        return YES;
    } else {
        returnNO; }}Copy the code

Swift (3.0.1) :

func checkoutPhoneNum(for regex: String, in phoneNum: String) -> Bool {
    do {
        let regex = try NSRegularExpression(pattern: regex)
        let nsString = phoneNum as NSString
        let results = regex.matches(in: phoneNum, range: NSRange(location: 0, length: nsString.length))
        let resultArray = results.map { nsString.substring(with: $0.range) }
        print(resultArray.count)
        if resultArray.count > 0 {
            return true
        } else {
            return false
        }
    } catch let error {
        print("Invalid regular expression: \ (error. LocalizedDescription)")
        return false}}Copy the code

See github