oc 中正则表达式不区分中英文分号的问题

前几天在用正则表达试验证密码输入时发现不能区分出中英文分号。
代码设置的字符编码是UTF-8
代码:
NSString* regex = @"^[;]{0,6}$";  //  其中分号为英文分号,长度是随意写的个控制;
NSPredicate* pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

BOOL isMatch;// 正则表达试验证返回值,YES 为通过, NO 为不通过;

isMatch = [pred evaluateWithObject: @";"]; // 英文分号,返回结果 YES 是通过
isMatch = [pred evaluateWithObject: @";"]; // 中文分号,返回结果 YES 也是通过

于是这样写:

isMatch = [pred evaluateWithObject:[[NSString alloc] initWithFormat:@"%s", [@";" cStringUsingEncoding:NSUTF8StringEncoding]]];  // 英文分号,返回结果 YES 是通过

isMatch = [pred evaluateWithObject:[[NSString alloc] initWithFormat:@"%s", [@";" cStringUsingEncoding:NSUTF8StringEncoding]]]; // 中文分号,返回结果 NO 不通过;

这样写就可以正正确验证了。原因我也不明,如果是编码问题,中英文 句号 用第一种写却能分开。

--------------------------------------------------------------------
若有其他凝问或文中有错误,请及时向我指出,
我好及时改正,同时也让我们一起进步。
email : [email protected]
qq     : 1035862795
敲门砖: 代码谱写人生

猜你喜欢

转载自binary-space.iteye.com/blog/2274851