大佬教程收集整理的这篇文章主要介绍了Objective-c中的正则表达式无法正常工作,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@implementation NSString (regexMatch) - (BOOL)matchesRegex:(NSString *)regexPattern { NSError *error = nil; NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:regexPattern options:NSRegularExpressionSearch error:&error]; if(error) { NSLog(@"Error on maches regex: %@ on %@: %@",regex,self,error); return NO; } NSUInteger maches = [regex numberOfMatchesinstring:self options:NSRegularExpressionSearch range:NsmakeRange(0,[self length])]; return (maches > 0); } @end
正则表达式:
const static NSString *REGEX_VALID_PASS = @"(^((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[-_,.@#$%'\"]).{6,20})$)";
单元测试:
//password // at least: // - between 6 and 20 chars // - a capital character // - a small character // - a digit // - a special char: - _,. @ # $% ´ ' STAssertTrue([@"Pass1-" matchesRegex:rEGEX_VALID_PASS],@"password 1 @L_801_1@"); STAssertTrue([@"aPrettyLongPassw0rd@" matchesRegex:rEGEX_VALID_PASS],@"password 2 @L_801_1@"); STAssertTrue([@"Pw987321-_,.@#$%'`" matchesRegex:rEGEX_VALID_PASS],@"password 3 @L_801_1@"); //Returns TRUE ??? STAssertfalse([@"password1@" matchesRegex:rEGEX_VALID_PASS],@"Wrong password 4 works (No capital letter)"); //Returns TRUE ??? STAssertfalse([@"passworD2#" matchesRegex:rEGEX_VALID_PASS],@"Wrong password 5 works (No small letter)"); STAssertfalse([@"password8" matchesRegex:rEGEX_VALID_PASS],@"Wrong password 6 works (No special letter)"); STAssertfalse([@"password$" matchesRegex:rEGEX_VALID_PASS],@"Wrong password 7 works (No digit letter)"); STAssertfalse([@"PrettyLongpasswordIsWrong123#" matchesRegex:rEGEX_VALID_PASS],@"Wrong password 8 works (> 20)"); STAssertfalse([@"Pw@3" matchesRegex:rEGEX_VALID_PASS],@"Wrong password 9 works (< 6)");
密码’password1 @’和’passworD2#’返回TRUE但不正确.
错误:
file://localhost/Users/user/app/AppTest.m: error: testRegex (AppTest) @L_801_1@: "[@"password1@" matchesRegex:rEGEX_VALID_PASS]" should be false. Wrong password 4 works (No capital letter) and file://localhost/Users/user/app/AppTest.m: error: testRegex (AppTest) @L_801_1@: "[@"passworD2#" matchesRegex:rEGEX_VALID_PASS]" should be false. Wrong password 5 works (No small letter)
使用指定的NSRegularExpressionCaseInsensitive选项,(?=.* [a-z])将匹配任何字母,包括大写字母;类似地,(?=.* [A-Z])也将匹配小写字母.
您的失败单元测试期望有不同的行为:它们都检查密码是否包含大写和小写字母,而输入包含相同大小写的字母.
以上是大佬教程为你收集整理的Objective-c中的正则表达式无法正常工作全部内容,希望文章能够帮你解决Objective-c中的正则表达式无法正常工作所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。