---
title: "NSPredicate及正则表达式的使用心得 分享"
author: "mrjeye"
date: "2012-01-17T17:24:43Z"
tags: []
categories: ["移动开发"]
slug: "nspredicate"
draft: false
lastmod: "2012-01-17T17:24:43Z"
id: 1354565
url: "https://mrjeye.iteye.com/blog/1354565"
---

判断字符串首字母是否为字母。
Objective-c代码
NSString *regex = @"+";
NSPredicate *predicate = ;

if () {
}

NSString *regex = @"+";
NSPredicate *predicate = ;

if () {
}


判断Array中是否包含某一规则的对象，并返回一个数组：
NSPredicate *filterPredicate = ;
并调用：- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate; 方法即可。 本文来自织梦


获得一个数组中某些对象除外的数组：
NSPredicate *notPredicate = ;且还是要调用- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate; 方法。

同样，如果我们想找出某个范围内的对象，创建如下Predicate (这里可以用到所有的比较操作符)： NSPredicate *pre = ;
并调用：- (BOOL)evaluateWithObject:(id)object;方法。
在这里啰嗦一句，如果只是在数组中查找是否存在对象时用indexOfObject,如果不存在则返回为NSNotFound.

字符串替换：
Objective-c代码
NSError* error = NULL;
NSRegularExpression* regex = +(\")"
options:0
error:&error];
NSString* sample = @"";
NSLog(@"Start:%@",sample);
NSString* result = ;
NSLog(@"Result:%@", result);

NSError* error = NULL;
NSRegularExpression* regex = +(\")"
options:0
error:&error];
NSString* sample = @"";
NSLog(@"Start:%@",sample);
NSString* result = ;
NSLog(@"Result:%@", result);


from:http://lovelysimon.blog.sohu.com/190483498.html