アーカイブ

NSPredicateでの正規表現について。その3 このエントリーを含むはてなブックマーク はてなブックマーク - NSPredicateでの正規表現について。その3

2015 年 8 月 14 日 コメント 2 件

NSPredicateでの正規表現について。その2 » サイキョウライン

え?なんなの?¥dと¥Dは全角半角関係ないの?どこ仕様?それどこ仕様よ?

ってことで、念のため仕様を追ってみました。

Predicate Format String Syntax

MATCHES
The left hand expression equals the right hand expression using a regex-style comparison according to ICU v3 (for more details see the ICU User Guide for Regular Expressions).

ここの「Regular Expressions」に付いてるリンクから、

Regular Expressions – ICU User Guide

\d  ✓ ✓  Match any character with the Unicode General Category of Nd (Number, Decimal Digit.)

Regular Expressions – ICU User Guide

\D  ✓ ✓  Match any character that is not a decimal digit.

「Unicode General Category of Nd (Number, Decimal Digit.)」ってなに?→ググる。

Unicode Characters in the ‘Number, Decimal Digit’ Category

U+0030 DIGIT ZERO 0

なるほどってことで、それぞれのコードを確認します。

NSLog(@"\\u%04x", [@"0" characterAtIndex:0]);  //=> \u0030
NSLog(@"\\u%04x", [@"0" characterAtIndex:0]); //=> \uff10

んー、たしかに別物ですよねぇ。。。えぇ。。。

Unicode Characters in the ‘Number, Decimal Digit’ Category

U+FF10 FULLWIDTH DIGIT ZERO 0

。。。ん?
これもここにあるっていうことは「\d」の解釈は仕様として正しいってこと?「\d」を使ってるのがダメ?

NSPredicate *p1 = [NSPredicate predicateWithFormat:@"self matches '.*[^0-9]+?.*'"];
NSLog(@"%d", [p1 evaluateWithObject:@"1234567"]);        //=> 0
NSLog(@"%d", [p1 evaluateWithObject:@"1234567"]); //=> 0

NSPredicate *p2 = [NSPredicate predicateWithFormat:@"self matches '.*[^0-9]+?.*'"];
NSLog(@"%d", [p2 evaluateWithObject:@"1234567"]);        //=> 0
NSLog(@"%d", [p2 evaluateWithObject:@"1234567"]); //=> 0

ということは、この結果はどういうことなの?どう説明するの?

えー、だいぶ混乱してまいりました。

カテゴリー: iOS タグ: