最近在调试正则表达式时,发现判断数字的正则表达式中的\d
对全角字符判断的不准,有的能判断有时候又判断不出来。然后经过一番搜索发现\d
判断的不只是0-9
,而是一个 Unicode 字符集,
参考这里的描述:
\d ✓ ✓ Match any character with the Unicode General Category of Nd (Number, Decimal Digit.)
例如下面的测试:
- (void)testDRegex
{
NSArray *numbers = @[@"a",@"A",@"1",@"123456",
@"㆒㆓㆔",@"⑴⑵⑶",
@"①②③④⑤",@"𝟘𝟙𝟚𝟛",
@"᮰᮱᮲᮳᮴᮵᮶᮷᮸᮹",@"ⅠⅡⅢⅣⅤⅥ",
@"𐒠𐒡𐒢𐒣𐒤𐒥𐒦𐒧𐒨𐒩",@"ⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹⅺⅻ",@"𝍩𝍪𝍫𝍬𝍭𝍮𝍯𝍰𝍱",
@"𝟎𝟏",@"𑶨𑶧𑶧",
@"𞥐"];
for (NSString *string in numbers) {
BOOL mobile1R = [ValidateUtil validateString:string withRegex:@"^\\d*$"];
NSLog(@"Test \\d method:, string: %@,result: %@",string,@(mobile1R));
}
}
输出结果为:
Test \d method:, string: 123456,result: 1
Test \d method:, string: ㆒㆓㆔,result: 0
Test \d method:, string: ⑴⑵⑶,result: 0
Test \d method:, string: ①②③④⑤,result: 0
Test \d method:, string: 𝟘𝟙𝟚𝟛,result: 1
Test \d method:, string: ᮰᮱᮲᮳᮴᮵᮶᮷᮸᮹,result: 1
Test \d method:, string: ⅠⅡⅢⅣⅤⅥ,result: 0
Test \d method:, string: 𐒠𐒡𐒢𐒣𐒤𐒥𐒦𐒧𐒨𐒩,result: 1
Test \d method:, string: ⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹⅺⅻ,result: 0
Test \d method:, string: 𝍩𝍪𝍫𝍬𝍭𝍮𝍯𝍰𝍱,result: 0
Test \d method:, string: 𝟎𝟏,result: 1
Test \d method:, string: 𑶨𑶧𑶧,result: 1
Test \d method:, string: 𞥐,result: 1
可以明显看到\d
判断的不只是0-9
等数字。
点击查看Unicode Characters in the 'Number, Decimal Digit' Category
最后结论:为了正确匹配0-9数字,还是只写[0-9]
吧,不能再写\d
了
参考:
1、http://userguide.icu-project.org/strings/regexp
2、http://fresky.github.io/2013/06/04/d-0-9-difference-in-regex/
3、https://stackoverflow.com/questions/890686/should-i-use-d-or-0-9-to-match-digits-in-a-perl-regex
4、https://stackoverflow.com/questions/16621738/d-is-less-efficient-than-0-9?newsletter=1&nlcode=55866%7cc739