IOS中NSPredicate和NSRegularExpression校验正则表达式区别
程序员文章站
2023-12-18 23:48:34
在代码开发过程中,我们经常需要用来校验邮箱、手机号等等,这个时候就需要用到正则表达式。在ios开发中,能用来做正则校验的有两个 nspredicate 和 nsregula...
在代码开发过程中,我们经常需要用来校验邮箱、手机号等等,这个时候就需要用到正则表达式。在ios开发中,能用来做正则校验的有两个 nspredicate 和 nsregularexpression 。
nspredicate
nspredicate 能用来简单做正则校验,但是它的问题是存在校验不出来的情况。
//nsstring+regex.h #import <foundation/foundation.h> @interface nsstring (regex) #pragma mark - nspredicate - (bool)mars_matchedbyprdicatetoregex:(nsstring *)regex; @end //nsstring+regex.m #import "nsstring+regex.h" @implementation nsstring (regex) #pragma mark - nspredicate - (bool)mars_matchedbyprdicatetoregex:(nsstring *)regex{ nspredicate *predicate = [nspredicate predicatewithformat:@"self matches %@", regex]; return [predicate evaluatewithobject:self]; } @end
nsregularexpression (推荐)
nsregularexpression 相对于 nspredicate 功能就强大的多了,这也是ios正则校验的正统路子。
//nsstring+regex.h #import <foundation/foundation.h> @interface nsstring (regex) #pragma mark - nsregularexpression //校验是否匹配 - (bool)mars_matchedtoregex:(nsstring *)regex; //匹配到的第一个字符串 - (nsstring *)mars_firstmatchtoregex:(nsstring *)regex; //所有匹配的字符串 - (nsarray *)mars_matchestoregex:(nsstring *)regex; //替换匹配到的字符串 - (nsstring *)mars_stringbyreplacematchestoregex:(nsstring *)regex replacestring:(nsstring *)replacestring; @end //nsstring+regex.m #import "nsstring+regex.h" @implementation nsstring (regex) #pragma mark - nsregualrexpression //校验是否匹配 - (bool)mars_matchedtoregex:(nsstring *)regex{ nserror *error; nsregularexpression *regularexpression = [nsregularexpression regularexpressionwithpattern:regex options:nsregularexpressioncaseinsensitive error:&error]; nsuinteger number = [regularexpression numberofmatchesinstring:self options:0 range:nsmakerange(0, self.length)]; return number != 0; } //匹配到的第一个字符串 - (nsstring *)mars_firstmatchtoregex:(nsstring *)regex{ nserror *error; nsregularexpression *regularexpression = [nsregularexpression regularexpressionwithpattern:regex options:nsregularexpressioncaseinsensitive error:&error]; nstextcheckingresult *firstmatch = [regularexpression firstmatchinstring:self options:0 range:nsmakerange(0, self.length)]; if (firstmatch) { nsstring *result = [self substringwithrange:firstmatch.range]; return result; } return nil; } //所有匹配的字符串 - (nsarray *)mars_matchestoregex:(nsstring *)regex{ nserror *error; nsregularexpression *regularexpression = [nsregularexpression regularexpressionwithpattern:regex options:nsregularexpressioncaseinsensitive error:&error]; nsarray *matcharray = [regularexpression matchesinstring:self options:0 range:nsmakerange(0, self.length)]; nsmutablearray *array = [nsmutablearray array]; if (matcharray.count != 0) { for (nstextcheckingresult *match in matcharray) { nsstring *result = [self substringwithrange:match.range]; [array addobject:result]; } } return array; } //替换匹配到的字符串 - (nsstring *)mars_stringbyreplacematchestoregex:(nsstring *)regex replacestring:(nsstring *)replacestring{ nserror *error; nsregularexpression *regularexpression = [nsregularexpression regularexpressionwithpattern:regex options:nsregularexpressioncaseinsensitive error:&error]; return [regularexpression stringbyreplacingmatchesinstring:self options:0 range:nsmakerange(0, self.length) withtemplate:replacestring]; } @end
最后我们看到,还是推荐大家使用nsregularexpression来做正则的校验,如果大家在学习中有更好的解决方法或者心得,可以在下方的留言区讨论。