复制#import <Foundation/Foundation.h>
@interface NSString (Regex)
- (BOOL)is_Email;
- (BOOL)is_Phone_Num;
- (BOOL)is_Car_No;
- (BOOL)is_Url;
- (BOOL)is_Postal_Code;
- (BOOL)is_Chinese;
- (BOOL)is_Valid_IP;
- (BOOL)is_IdCard_Num;
- (BOOL)isValidWithMinLenth:(NSInteger)min_lenth
maxLenth:(NSInteger)max_lenth
containChinese:(BOOL)contain_chinese
containDigtal:(BOOL)contain_digtal
containLetter:(BOOL)contain_letter
containOtherCharacter:(NSString *)contain_other_character
firstCannotBeDigtal:(BOOL)first_cannot_be_digtal;
- (NSString *)strip;
- (NSString *)remove_Html_Format;
- (BOOL)is_Tax_No;
@end
#import "NSString+Regex.h"
@implementation NSString (Regex)
- (BOOL)is_Email {
NSString *email_regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *email_test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", email_regex];
return [email_test evaluateWithObject:self];
}
- (BOOL)is_Phone_Num {
NSString *phone_regex = @"^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[0-9])[0-9]{8}$";
NSPredicate *phone_test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phone_regex];
return [phone_test evaluateWithObject:self];
}
- (BOOL)is_Car_No {
NSString *car_regex = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";
NSPredicate *car_test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", car_regex];
return [car_test evaluateWithObject:self];
}
- (BOOL)is_Url {
NSString *regex = @"^((http)|(https))+:[^\\s]+\\.[^\\s]*$";
NSPredicate *gc_pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [gc_pre evaluateWithObject:self];
}
- (BOOL)is_Postal_Code {
NSString *phone_regex = @"^[0-8]\\d{5}(?!\\d)$";
NSPredicate *phone_test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phone_regex];
return [phone_test evaluateWithObject:self];
}
- (BOOL)is_Chinese {
NSString *phone_regex = @"^[\u4e00-\u9fa5]+$";
NSPredicate *phone_test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phone_regex];
return [phone_test evaluateWithObject:self];
}
- (BOOL)is_Valid_IP {
NSString *regex = [NSString stringWithFormat:@"^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"];
NSPredicate *gc_pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL gc_rc = [gc_pre evaluateWithObject:self];
if (gc_rc) {
NSArray *componds_a = [self componentsSeparatedByString:@","];
BOOL gc_v = YES;
for (NSString *gc_s in componds_a) {
if (gc_s.integerValue > 255) {
gc_v = NO;
break;
}
}
return gc_v;
}
return NO;
}
- (BOOL)is_IdCard_Num {
NSString *gc_value = [self copy];
gc_value = [gc_value stringByReplacingOccurrencesOfString:@"X" withString:@"x"];
gc_value = [gc_value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
int length = 0;
if (!gc_value) {
return NO;
}
else {
length = (int)gc_value.length;
if (length != 15 && length != 18) {
return NO;
}
}
NSArray *areas_array = @[@"11", @"12", @"13", @"14", @"15", @"21", @"22", @"23", @"31", @"32", @"33", @"34", @"35", @"36", @"37", @"41", @"42", @"43", @"44", @"45", @"46", @"50", @"51", @"52", @"53", @"54", @"61", @"62", @"63", @"64", @"65", @"71", @"81", @"82", @"91"];
NSString *value_start2 = [gc_value substringToIndex:2];
BOOL area_flag = NO;
for (NSString *area_code in areas_array) {
if ([area_code isEqualToString:value_start2]) {
area_flag = YES;
break;
}
}
if (!area_flag) {
return NO;
}
NSRegularExpression *regularExpression;
NSUInteger numberofMatch;
int gc_year = 0;
switch (length) {
case 15:
gc_year = [gc_value substringWithRange:NSMakeRange(6, 2)].intValue + 1900;
if (gc_year % 4 == 0 || (gc_year % 100 == 0 && gc_year % 4 == 0)) {
regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$" options:NSRegularExpressionCaseInsensitive error:nil];
}
else {
regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$" options:NSRegularExpressionCaseInsensitive error:nil];
}
numberofMatch = [regularExpression numberOfMatchesInString:gc_value options:NSMatchingReportProgress range:NSMakeRange(0, gc_value.length)];
if (numberofMatch > 0) {
return YES;
}
else {
return NO;
}
case 18:
gc_year = [gc_value substringWithRange:NSMakeRange(6, 4)].intValue;
if (gc_year % 4 == 0 || (gc_year % 100 == 0 && gc_year % 4 == 0)) {
regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"options:NSRegularExpressionCaseInsensitive error:nil];
}
else {
regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
options:NSRegularExpressionCaseInsensitive error:nil];
}
numberofMatch = [regularExpression numberOfMatchesInString:gc_value options:NSMatchingReportProgress range:NSMakeRange(0, gc_value.length)];
if (numberofMatch > 0) {
int gc_s = ([gc_value substringWithRange:NSMakeRange(0, 1)].intValue + [gc_value substringWithRange:NSMakeRange(10, 1)].intValue) * 7 + ([gc_value substringWithRange:NSMakeRange(1, 1)].intValue + [gc_value substringWithRange:NSMakeRange(11, 1)].intValue) * 9 + ([gc_value substringWithRange:NSMakeRange(2, 1)].intValue + [gc_value substringWithRange:NSMakeRange(12, 1)].intValue) * 10 + ([gc_value substringWithRange:NSMakeRange(3, 1)].intValue + [gc_value substringWithRange:NSMakeRange(13, 1)].intValue) * 5 + ([gc_value substringWithRange:NSMakeRange(4, 1)].intValue + [gc_value substringWithRange:NSMakeRange(14, 1)].intValue) * 8 + ([gc_value substringWithRange:NSMakeRange(5, 1)].intValue + [gc_value substringWithRange:NSMakeRange(15, 1)].intValue) * 4 + ([gc_value substringWithRange:NSMakeRange(6, 1)].intValue + [gc_value substringWithRange:NSMakeRange(16, 1)].intValue) * 2 + [gc_value substringWithRange:NSMakeRange(7, 1)].intValue * 1 + [gc_value substringWithRange:NSMakeRange(8, 1)].intValue * 6 + [gc_value substringWithRange:NSMakeRange(9, 1)].intValue * 3;
int gc_y = gc_s % 11;
NSString *gc_m = @"F";
NSString *gc_jym = @"10X98765432";
gc_m = [gc_jym substringWithRange:NSMakeRange(gc_y, 1)];
if ([gc_m isEqualToString:[[gc_value substringWithRange:NSMakeRange(17, 1)] uppercaseString]]) {
return YES;
}
else {
return NO;
}
}
else {
return NO;
}
default:
return NO;
}
return NO;
}
- (BOOL)isValidWithMinLenth:(NSInteger)min_lenth
maxLenth:(NSInteger)max_lenth
containChinese:(BOOL)contain_chinese
containDigtal:(BOOL)contain_digtal
containLetter:(BOOL)contain_letter
containOtherCharacter:(NSString *)contain_other_character
firstCannotBeDigtal:(BOOL)first_cannot_be_digtal {
BOOL gc_rc = YES;
if (first_cannot_be_digtal) {
NSString *regex = @"^[0-9]+.*";
NSPredicate *gc_pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
gc_rc = [gc_pre evaluateWithObject:self];
if (gc_rc) {
return NO;
}
}
NSString *gc_hanzi = contain_chinese ? @"\u4e00-\u9fa5" : @"";
NSString *digtal_regex = contain_digtal ? @"0-9" : @"";
NSString *containOtherCharacterRegex = contain_other_character ? contain_other_character : @"";
NSString *character_regex = [NSString stringWithFormat:@"[%@A-Za-z%@%@]", gc_hanzi, digtal_regex, containOtherCharacterRegex];
NSString *regex = [NSString stringWithFormat:@"%@{%@,%@}", character_regex, @(min_lenth), @(max_lenth)];
NSPredicate *gc_pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [gc_pre evaluateWithObject:self];
}
- (NSString *)strip {
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (NSString *)remove_Html_Format {
NSString *gc_str = [NSString stringWithFormat:@"%@", self];
NSError *error;
NSRegularExpression *gc_regex = [NSRegularExpression regularExpressionWithPattern:@"<[^>]*>" options:NSRegularExpressionCaseInsensitive error:&error];
if (!error) {
gc_str = [gc_regex stringByReplacingMatchesInString:gc_str options:0 range:NSMakeRange(0, gc_str.length) withTemplate:@"$2$1"];
}
else {
NSLog(@"%@", error);
}
NSArray *html_code = @[
@"\"", @"'", @"&", @"<", @">",
@"", @"¡", @"¢", @"£", @"¤",
@"¥", @"¦", @"§", @"¨", @"©",
@"ª", @"«", @"¬", @"", @"®",
@"¯", @"°", @"±", @"²", @"³",
@"´", @"µ", @"¶", @"·", @"¸",
@"¹", @"º", @"»", @"¼", @"½",
@"¾", @"¿", @"×", @"÷", @"À",
@"Á", @"Â", @"Ã", @"Ä", @"Å",
@"Æ", @"Ç", @"È", @"É", @"Ê",
@"Ë", @"Ì", @"Í", @"Î", @"Ï",
@"Ð", @"Ñ", @"Ò", @"Ó", @"Ô",
@"Õ", @"Ö", @"Ø", @"Ù", @"Ú",
@"Û", @"Ü", @"Ý", @"Þ", @"ß",
@"à", @"á", @"â", @"ã", @"ä",
@"å", @"æ", @"ç", @"è", @"é",
@"ê", @"ë", @"ì", @"í", @"î",
@"ï", @"ð", @"ñ", @"ò", @"ó",
@"ô", @"õ", @"ö", @"ø", @"ù",
@"ú", @"û", @"ü", @"ý", @"þ",
@"ÿ", @"∀", @"∂", @"∃", @"∅",
@"∇", @"∈", @"∉", @"∋", @"∏",
@"∑", @"−", @"∗", @"√", @"∝",
@"∞", @"∠", @"∧", @"∨", @"∩",
@"∪", @"∫", @"∴", @"∼", @"≅",
@"≈", @"≠", @"≡", @"≤", @"≥",
@"⊂", @"⊃", @"⊄", @"⊆", @"⊇",
@"⊕", @"⊗", @"⊥", @"⋅", @"Α",
@"Β", @"Γ", @"Δ", @"Ε", @"Ζ",
@"Η", @"Θ", @"Ι", @"Κ", @"Λ",
@"Μ", @"Ν", @"Ξ", @"Ο", @"Π",
@"Ρ", @"Σ", @"Τ", @"Υ", @"Φ",
@"Χ", @"Ψ", @"Ω", @"α", @"β",
@"γ", @"δ", @"ε", @"ζ", @"η",
@"θ", @"ι", @"κ", @"λ", @"μ",
@"ν", @"ξ", @"ο", @"π", @"ρ",
@"ς", @"σ", @"τ", @"υ", @"φ",
@"χ", @"ψ", @"ω", @"ϑ", @"ϒ",
@"ϖ", @"Œ", @"œ", @"Š", @"š",
@"Ÿ", @"ƒ", @"ˆ", @"˜", @"",
@"", @"", @"", @"", @"",
@"", @"–", @"—", @"‘", @"’",
@"‚", @"“", @"”", @"„", @"†",
@"‡", @"•", @"…", @"‰", @"′",
@"″", @"‹", @"›", @"‾", @"€",
@"™", @"←", @"↑", @"→", @"↓",
@"↔", @"↵", @"⌈", @"⌉", @"⌊",
@"⌋", @"◊", @"♠", @"♣", @"♥",
@"♦",
];
NSArray *gc_code = @[
@""", @"'", @"&", @"<", @">",
@" ", @"¡", @"¢", @"£", @"¤",
@"¥", @"¦", @"§", @"¨", @"©",
@"ª", @"«", @"¬", @"­", @"®",
@"¯", @"°", @"±", @"²", @"³",
@"´", @"µ", @"¶", @"·", @"¸",
@"¹", @"º", @"»", @"¼", @"½",
@"¾", @"¿", @"×", @"÷", @"À",
@"Á", @"Â", @"Ã", @"Ä", @"Å",
@"Æ", @"Ç", @"È", @"É", @"Ê",
@"Ë", @"Ì", @"Í", @"Î", @"Ï",
@"Ð", @"Ñ", @"Ò", @"Ó", @"Ô",
@"Õ", @"Ö", @"Ø", @"Ù", @"Ú",
@"Û", @"Ü", @"Ý", @"Þ", @"ß",
@"à", @"á", @"â", @"ã", @"ä",
@"å", @"æ", @"ç", @"è", @"é",
@"ê", @"ë", @"ì", @"í", @"î",
@"ï", @"ð", @"ñ", @"ò", @"ó",
@"ô", @"õ", @"ö", @"ø", @"ù",
@"ú", @"û", @"ü", @"ý", @"þ",
@"ÿ", @"∀", @"∂", @"&exists;", @"∅",
@"∇", @"∈", @"∉", @"∋", @"∏",
@"∑", @"−", @"∗", @"√", @"∝",
@"∞", @"∠", @"∧", @"∨", @"∩",
@"∪", @"∫", @"∴", @"∼", @"≅",
@"≈", @"≠", @"≡", @"≤", @"≥",
@"⊂", @"⊃", @"⊄", @"⊆", @"⊇",
@"⊕", @"⊗", @"⊥", @"⋅", @"Α",
@"Β", @"Γ", @"Δ", @"Ε", @"Ζ",
@"Η", @"Θ", @"Ι", @"Κ", @"Λ",
@"Μ", @"Ν", @"Ξ", @"Ο", @"Π",
@"Ρ", @"Σ", @"Τ", @"Υ", @"Φ",
@"Χ", @"Ψ", @"Ω", @"α", @"β",
@"γ", @"δ", @"ε", @"ζ", @"η",
@"θ", @"ι", @"κ", @"λ", @"μ",
@"ν", @"ξ", @"ο", @"π", @"ρ",
@"ς", @"σ", @"τ", @"υ", @"φ",
@"χ", @"ψ", @"ω", @"ϑ", @"ϒ",
@"ϖ", @"Œ", @"œ", @"Š", @"š",
@"Ÿ", @"ƒ", @"ˆ", @"˜", @" ",
@" ", @" ", @"‌", @"‍", @"‎",
@"‏", @"–", @"—", @"‘", @"’",
@"‚", @"“", @"”", @"„", @"†",
@"‡", @"•", @"…", @"‰", @"′",
@"″", @"‹", @"›", @"‾", @"€",
@"™", @"←", @"↑", @"→", @"↓",
@"↔", @"↵", @"⌈", @"⌉", @"⌊",
@"⌋", @"◊", @"♠", @"♣", @"♥",
@"♦",
];
NSInteger gc_idx = 0;
for (NSString *gc_obj in gc_code) {
gc_str = [gc_str stringByReplacingOccurrencesOfString:(NSString *)gc_obj withString:html_code[gc_idx]];
gc_idx ++;
}
return gc_str;
}
- (BOOL)is_Tax_No {
NSString *emailRegex = @"[0-9]\\d{13}([0-9]|X)$";
NSPredicate *email_test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [email_test evaluateWithObject:self];
}
@end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
2020-08-05 Android开发_应用结构化资源储备
2020-08-05 Android开发_adb常用命令
2020-08-05 Android开发_Android 6.0 更新包与已安装应用的签名不一致
2020-08-05 Apple设备_苹果手机怎么录屏 iOS12设置录屏
2020-08-05 Apple设备_Siri语音唤醒的开启与语音矫正
2020-08-05 Git的工作流程
2018-08-05 iOS开发_自定义控件