Email 邮箱
+ (NSRegularExpression *)regexEmail
{
static NSRegularExpression *regex;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
regex = [NSRegularExpression regularExpressionWithPattern:
@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" options:kNilOptions error:NULL];
});
return regex;
}
网址 URL
+ (NSRegularExpression *)regexUrl
{
static NSRegularExpression *regex;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
regex = [NSRegularExpression regularExpressionWithPattern:@"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)" options:kNilOptions error:NULL];
});
return regex;
}
手机号码 Phone Number
+ (NSRegularExpression *)regexPhoneNum
{
static NSRegularExpression *regex;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
regex = [NSRegularExpression regularExpressionWithPattern:@"\\d{1,23}|\\d{3}-\\d{8}|\\d{3}-\\d{7}|\\d{4}-\\d{8}|\\d{4}-\\d{7}|1+[3578]+\\d{9}" options:kNilOptions error:NULL];
});
return regex;
}
时间 年月日
+ (NSRegularExpression *)regexDate
{
static NSRegularExpression *regex;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
regex = [NSRegularExpression regularExpressionWithPattern:@"([\\u4eca,\\u660e,\\u540e][\\u5929,\\u65e5])\\s*(([\\u4e0a,\\u4e0b]\\u5348)?(\\u665a\\u4e0a)?)(\\s*((0?([1-9]|[1][0-9]|2[0-3])[:,\\uff1a,\\u70b9,\\u65f6])([0-5][0-9]([:,\\uff1a,\\u5206])?)?)?)?|([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})[-年./]?(((0?[13578]|1[02])[-月./](0[1-9]|[12][0-9]|3[01]))|((0?[469]|11)[-月./]?(0[1-9]|[12][0-9]|30)([\\u53f7,\\u65e5])?)|(0?2[-月./](0[1-9]|[1][0-9]|2[0-8])([\\u53f7,\\u65e5])?))(\\s*((0?([1-9]|[1][0-9]|2[0-3])[:,\\uff1a,\\u70b9,\\u65f6])([0-5][0-9]([:,\\uff1a,\\u5206])?)?)?)?|([\\u4eca,\\u660e,\\u540e][\\u5929,\\u65e5])(([\\u4e0a,\\u4e0b]\\u5348)?(\\u665a\\u4e0a))?(\\s*((0?([1-9]|[1][0-9]|2[0-3])[:,\\uff1a,\\u70b9,\\u65f6])([0-5][0-9]([:,\\uff1a,\\u5206])?)?)?)?|(([\\u4e0a,\\u4e0b]\\u5348))(\\s*((0?([1-9]|[1][0-9]|2[0-3])[:,\\uff1a,\\u70b9,\\u65f6])([0-5][0-9]([:,\\uff1a,\\u5206])?)?)?)?|((0?([1-9]|[1][0-9]|2[0-3])[:,\\uff1a,\\u70b9,\\u65f6])(([0-5])?[0-9]([:,\\uff1a,\\u5206])?)?)|(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})[-年./])?(((0?[13578]|1[02])[-月./](0[1-9]|[12][0-9]|3[01]))|((0?[469]|11)[-月./]?(0[1-9]|[12][0-9]|30)([\\u53f7,\\u65e5])?)|(0?2[-月./](0[1-9]|[1][0-9]|2[0-8])([\\u53f7,\\u65e5])?))(\\s*((0?([1-9]|[1][0-9]|2[0-3])[:,\\uff1a,\\u70b9,\\u65f6])([0-5][0-9]([:,\\uff1a,\\u5206])?)?)?)?|((\\u665a\\u4e0a))(\\s*((0?([1-9]|[1][0-9]|2[0-3])[:,\\uff1a,\\u70b9,\\u65f6])([0-5][0-9]([:,\\uff1a,\\u5206])?)?)?)?|((0?([1-9]|[1][0-9]|2[0-3])[:,\\uff1a,\\u70b9,\\u65f6])(([0-5])?[0-9]([:,\\uff1a,\\u5206])?)?)" options:kNilOptions error:NULL];
});
return regex;
}
时间 数字
+ (NSRegularExpression *)regexTime
{
static NSRegularExpression *regex;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
regex = [NSRegularExpression regularExpressionWithPattern:@"0?([1-9]|[1][0-9]|2[0-3])[:,\\uff1a,\\u70b9,\\u65f6]([0-5][0-9]([:,\\uff1a,\\u5206])?)?" options:kNilOptions error:NULL];
});
return regex;
}
用法介绍 以URL为例
//正则匹配
NSRegularExpression *regex = [Utility regexUrl];
NSArray *resultStr = [regex matchesInString:string options:kNilOptions range:NSRangeMake(0,string.length)];
for (NSTextCheckingResult *at in resultStr){
if (at.range.location == NSNotFound && at.range.length <= 1){
continue;
}
// 给url添加下划线
NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
[attrStrings setYy_attributes:attribtDic];
}
网友评论