Regex solution for Objective-C

前端 未结 7 1733
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 11:25

What is the best method for using Regular Expressions within Objective-C?

There seems to be some open source project that provide regex

7条回答
  •  -上瘾入骨i
    2020-12-13 11:53

    NSPredicate *predicate;
    predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '.*@.*\..*'"];
    BOOL result = [predicate evaluateWithObject:@"mail@domain.com"];
    

    According to the Predicate guide:

    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).

    There's even an example written by Apple that can be found in the guide.

    Instead of SELF you could also use a key path. (And possibly some other literals too.)

提交回复
热议问题