iphone - 使用 NSPredicate 将以数字或符号开头的字符串过滤到 NSArray

标签 iphone ios regex nspredicate

如何使用 NSPredicate 将以数字或符号开头的字符串过滤到 NSArray。

例子:

array = {"John", "Mary", "Aroldo", "1John", "+Mary"}

to newArray = {"1John," "+Mary"}

最佳答案

这里有 4 种方法。所有示例都使用否定字符类。你的请求是过滤掉数字和符号,但你也可以说你想过滤掉以非字母字符开头的单词。

- (void)testFilterArray0
{
    predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[^a-zA-Z].*"];
    filtered = [unfiltered filteredArrayUsingPredicate:predicate];
    STAssertTrue([filtered isEqualToArray:expected], nil);
}

- (void)testFilteredArray1
{
    predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[^\\p{L}].*"];
    filtered = [unfiltered filteredArrayUsingPredicate:predicate];
    STAssertTrue([filtered isEqualToArray:expected], nil);
}

- (void)testFilteredArray2
{
    predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[^\\p{Letter}].*"];
    filtered = [unfiltered filteredArrayUsingPredicate:predicate];
    STAssertTrue([filtered isEqualToArray:expected], nil);
}

- (void)testFilteredArray3
{
    predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[^\\p{General_Category=Letter}].*"];
    filtered = [unfiltered filteredArrayUsingPredicate:predicate];
    STAssertTrue([filtered isEqualToArray:expected], nil);
}

关于iphone - 使用 NSPredicate 将以数字或符号开头的字符串过滤到 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15117984/

相关文章:

iphone - Facebook 中的智能应用横幅

iphone - 使用 UI 自动化工具删除单元格的语法是什么?

javascript - 新行之前的正则表达式检查字符不起作用

jquery 使用正则表达式检索带 in 标签的数据

python - 删除列表中不包含正则表达式字符串的所有项目

ios - 执行segue会抛出错误

iphone - 如果在添加注释时缩放,MKMapView 会崩溃

ios - 推送通知 Firebase 导致崩溃

ios - 向 UITableViewCell 添加边距

ios - Swift 值在闭包中消失了