利用正则规则匹配进行判断是否是手机号、是否是身份证号码、是否是中文。

RegexUtil.h

#import

@interface RegexUtil : NSObject

// 是否是手机号码(简单判断)

+ (BOOL)isMobileSimple:(NSString *)mobile;

// 是否是手机号码(精准判断)

+ (BOOL)isMobileExact:(NSString *)mobile;

// 是否是中文

+ (BOOL)isZh:(NSString *)input;

// 是否是15位身份证号码

+ (BOOL)isIDCard15:(NSString *)idCard;

// 是否是18位身份证号码(简单判断)

+ (BOOL)isIDCard18:(NSString *)idCard;

// 是否是18位身份证号码(精准判断)

+ (BOOL)isIDCard18Exact:(NSString *)idCard;

@end

RegexUtil.m

#import

#import "RegexUtil.h"

// ##################################### 正则匹配规则 #####################################

/**

* Regex of simple mobile.

*/

#define REGEX_MOBILE_SIMPLE @"^[1]\\d{10}$"

/**

* Regex of exact mobile.

*

china mobile: 134(0-8), 135, 136, 137, 138, 139, 147, 150, 151, 152, 157, 158, 159, 165, 172, 178, 182, 183, 184, 187, 188, 195, 197, 198

*

china unicom: 130, 131, 132, 145, 155, 156, 166, 167, 175, 176, 185, 186, 196

*

china telecom: 133, 149, 153, 162, 173, 177, 180, 181, 189, 190, 191, 199

*

china broadcasting: 192

*

global star: 1349

*

virtual operator: 170, 171

*/

#define REGEX_MOBILE_EXACT @"^((13[0-9])|(14[579])|(15[0-35-9])|(16[2567])|(17[0-35-8])|(18[0-9])|(19[0-35-9]))\\d{8}$"

/**

* Regex of telephone number.

*/

#define REGEX_TEL @"^0\\d{2,3}[- ]?\\d{7,8}$"

/**

* Regex of id card number which length is 15.

*/

#define REGEX_ID_CARD15 @"^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$"

/**

* Regex of id card number which length is 18.

*/

#define REGEX_ID_CARD18 @"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$"

/**

* Regex of email.

*/

#define REGEX_EMAIL @"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"

/**

* Regex of url.

*/

#define REGEX_URL @"[a-zA-z]+://[^\\s]*"

/**

* Regex of Chinese character.

*/

#define REGEX_ZH @"^[\\u4e00-\\u9fa5]+$"

/**

* Regex of username.

*

scope for "a-z", "A-Z", "0-9", "_", "Chinese character"

*

can't end with "_"

*

length is between 6 to 20

*/

#define REGEX_USERNAME @"^[\\w\\u4e00-\\u9fa5]{6,20}(?

/**

* Regex of date which pattern is "yyyy-MM-dd".

*/

#define REGEX_DATE @"^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$"

/**

* Regex of ip address.

*/

#define REGEX_IP @"((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"

///

// The following come from http://tool.oschina.net/regex

///

/**

* Regex of double-byte characters.

*/

#define REGEX_DOUBLE_BYTE_CHAR @"[^\\x00-\\xff]"

/**

* Regex of blank line.

*/

#define REGEX_BLANK_LINE @"\\n\\s*\\r"

/**

* Regex of QQ number.

*/

#define REGEX_QQ_NUM @"[1-9][0-9]{4,}"

/**

* Regex of postal code in China.

*/

#define REGEX_CHINA_POSTAL_CODE @"[1-9]\\d{5}(?!\\d)"

/**

* Regex of integer.

*/

#define REGEX_INTEGER @"^(-?[1-9]\\d*)|0$"

/**

* Regex of positive integer.

*/

#define REGEX_POSITIVE_INTEGER @"^[1-9]\\d*$"

/**

* Regex of negative integer.

*/

#define REGEX_NEGATIVE_INTEGER @"^-[1-9]\\d*$"

/**

* Regex of non-negative integer.

*/

#define REGEX_NOT_NEGATIVE_INTEGER @"^[1-9]\\d*|0$"

/**

* Regex of non-positive integer.

*/

#define REGEX_NOT_POSITIVE_INTEGER @"^-[1-9]\\d*|0$"

/**

* Regex of positive float.

*/

#define REGEX_FLOAT @"^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$"

/**

* Regex of positive float.

*/

#define REGEX_POSITIVE_FLOAT @"^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$"

/**

* Regex of negative float.

*/

#define REGEX_NEGATIVE_FLOAT @"^-[1-9]\\d*\\.\\d*|-0\\.\\d*[1-9]\\d*$"

/**

* Regex of positive float.

*/

#define REGEX_NOT_NEGATIVE_FLOAT @"^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0$"

/**

* Regex of negative float.

*/

#define REGEX_NOT_POSITIVE_FLOAT @"^(-([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*))|0?\\.0+|0$"

// ##################################### 正则匹配规则 #####################################

@implementation RegexUtil

static NSMutableDictionary *CITY_DIC;

+ (BOOL)isMobileSimple:(NSString *)mobile

{

return [self isMatch:REGEX_MOBILE_SIMPLE input:mobile];

}

+ (BOOL)isMobileExact:(NSString *)mobile

{

return [self isMatch:REGEX_MOBILE_EXACT input:mobile];

}

+ (BOOL)isZh:(NSString *)input

{

return [self isMatch:REGEX_ZH input:input];

}

+ (BOOL)isIDCard15:(NSString *)idCard

{

return [self isMatch:REGEX_ID_CARD15 input:idCard];

}

+ (BOOL)isIDCard18:(NSString *)idCard

{

return [self isMatch:REGEX_ID_CARD18 input:idCard];

}

+ (BOOL)isIDCard18Exact:(NSString *)idCard

{

if ([self isIDCard18:idCard]) {

NSArray *factor = [NSArray arrayWithObjects:@7, @9, @10, @5, @8, @4, @2, @1, @6, @3, @7, @9, @10, @5, @8, @4, @2, nil];

NSArray *suffix = [NSArray arrayWithObjects:@'1', @'0', @'X', @'9', @'8', @'7', @'6', @'5', @'4', @'3', @'2', nil];

if (!CITY_DIC) {

CITY_DIC = [NSMutableDictionary dictionary];

}

if (CITY_DIC && (CITY_DIC.allValues.count == 0)) {

[CITY_DIC setValue:@"北京" forKey:@"11"];

[CITY_DIC setValue:@"天津" forKey:@"12"];

[CITY_DIC setValue:@"河北" forKey:@"13"];

[CITY_DIC setValue:@"山西" forKey:@"14"];

[CITY_DIC setValue:@"内蒙古" forKey:@"15"];

[CITY_DIC setValue:@"辽宁" forKey:@"21"];

[CITY_DIC setValue:@"吉林" forKey:@"22"];

[CITY_DIC setValue:@"黑龙江" forKey:@"23"];

[CITY_DIC setValue:@"上海" forKey:@"31"];

[CITY_DIC setValue:@"江苏" forKey:@"32"];

[CITY_DIC setValue:@"浙江" forKey:@"33"];

[CITY_DIC setValue:@"安徽" forKey:@"34"];

[CITY_DIC setValue:@"福建" forKey:@"35"];

[CITY_DIC setValue:@"江西" forKey:@"36"];

[CITY_DIC setValue:@"山东" forKey:@"37"];

[CITY_DIC setValue:@"河南" forKey:@"41"];

[CITY_DIC setValue:@"湖北" forKey:@"42"];

[CITY_DIC setValue:@"湖南" forKey:@"43"];

[CITY_DIC setValue:@"广东" forKey:@"44"];

[CITY_DIC setValue:@"广西" forKey:@"45"];

[CITY_DIC setValue:@"海南" forKey:@"46"];

[CITY_DIC setValue:@"重庆" forKey:@"50"];

[CITY_DIC setValue:@"四川" forKey:@"51"];

[CITY_DIC setValue:@"贵州" forKey:@"52"];

[CITY_DIC setValue:@"云南" forKey:@"53"];

[CITY_DIC setValue:@"西藏" forKey:@"54"];

[CITY_DIC setValue:@"陕西" forKey:@"61"];

[CITY_DIC setValue:@"甘肃" forKey:@"62"];

[CITY_DIC setValue:@"青海" forKey:@"63"];

[CITY_DIC setValue:@"宁夏" forKey:@"64"];

[CITY_DIC setValue:@"新疆" forKey:@"65"];

[CITY_DIC setValue:@"台湾老" forKey:@"71"];

[CITY_DIC setValue:@"香港" forKey:@"81"];

[CITY_DIC setValue:@"澳门" forKey:@"82"];

[CITY_DIC setValue:@"台湾新" forKey:@"83"];

[CITY_DIC setValue:@"国外" forKey:@"91"];

}

if ([CITY_DIC.allKeys containsObject:[idCard substringWithRange:NSMakeRange(0, 2)]]) {

NSInteger weightSum = 0;

for (NSInteger i = 0; i < 17; ++i) {

weightSum += [[NSNumber numberWithChar:([idCard characterAtIndex:i]-'0')] integerValue] * [factor[i] integerValue];

}

NSInteger idCardMod = weightSum % 11;

char idCardLast = [idCard characterAtIndex:17];

return idCardLast == [suffix[idCardMod] integerValue];

}

}

return NO;

}

+ (BOOL)isMatch:(NSString *)regex input:(NSString *)input

{

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

BOOL isMatch = [pred evaluateWithObject:input];

return input && input.length > 0 && isMatch;

}

@end

推荐文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: