最近在調試正則表達式時,發現判斷數字的正則表達式中的\d
對全角字符判斷的不許,有的能判斷有時候又判斷不出來。而後通過一番搜索發現\d
判斷的不僅是0-9
,而是一個 Unicode 字符集,git
參考這裏的描述:github
\d ✓ ✓ Match any character with the Unicode General Category of Nd (Number, Decimal Digit.)正則表達式
例以下面的測試:bash
- (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));
}
}
複製代碼
輸出結果爲:less
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
等數字。ide
點擊查看Unicode Characters in the 'Number, Decimal Digit' Category
測試
[0-9]
吧,不能再寫\d
了參考:ui
一、http://userguide.icu-project.org/strings/regexpspa
二、http://fresky.github.io/2013/06/04/d-0-9-difference-in-regex/調試
三、https://stackoverflow.com/questions/890686/should-i-use-d-or-0-9-to-match-digits-in-a-perl-regex
四、https://stackoverflow.com/questions/16621738/d-is-less-efficient-than-0-9?newsletter=1&nlcode=55866%7cc739