coreData增删改查正则

wuhuang 2013-05-27

coreData增删改查正则coreData增删改查正则

百度30分钟学会正则表达式

#if 0

\b是正则表达式规定的一个特殊代码(好吧,某些人叫它元字符,metacharacter),代表着单词的开头或结尾,也就是单词的分界处。虽然通常英文的单词是由空格,标点符号或者换行来分隔的,但是\b并不匹配这些单词分隔字符中的任何一个,它只匹配一个位置。

现在\bhi\b.*\bLucy\b的意思就很明显了:先是一个单词hi,然后是任意个任意字符(但不能是换行),最后是Lucy这个单词。

0\d\d-\d\d\d\d\d\d\d\d匹配这样的字符串:以0开头,然后是两个数字,然后是一个连字号“-”,最后是8个数字(也就是中国的电话号码。当然,这个例子只能匹配区号为3位的情形)。

0\d{2}-\d{8}。 这里\d后面的{2}({8})的意思是前面\d必须连续重复匹配2次(8次)。

表1.常用的元字符

代码说明

.匹配除换行符以外的任意字符

\w匹配字母或数字或下划线或汉字

\s匹配任意的空白符

\d匹配数字

\b匹配单词的开始或结束

^匹配字符串的开始

$匹配字符串的结束

比如一个网站如果要求你填写的QQ号必须为5位到12位数字时,可以使用:^\d{5,12}$

表2.常用的限定符

代码/语法说明

*重复零次或更多次

+重复一次或更多次

?重复零次或一次

{n}重复n次

{n,}重复n次或更多次

{n,m}重复n到m次

Windows\d+匹配Windows后面跟1个或更多数字

^\w+匹配一行的第一个单词(或整个字符串的第一个单词,具体匹配哪个意思得看选项设置)

-(BOOL)createNewPersonWithImage:(NSData *)aImage xing:(NSString *)aXing xingP:(NSString *)aXingP
ming:(NSString *)aMing mingP:(NSString *)aMingP company:(NSString *)aCompany phone:(NSInteger)aPhone email:(NSString *)aEmail url:(NSString *)aUrl
{
BOOL result=NO;
if ([aXing length]==0||[aMing length]==0) {
NSLog(@"First and Last names are mandatory.");
return NO;
}
ContactBook *newContactPerson=[NSEntityDescription insertNewObjectForEntityForName:@"ContactBook" inManagedObjectContext:self.managedObjectContext];
if (newContactPerson==nil) {
NSLog(@"Failed to create the new person.");
return NO;
}
newContactPerson.image=aImage;
newContactPerson.xing=aXing;
newContactPerson.xingP=aXingP;
newContactPerson.ming=aMing;
newContactPerson.mingP=aMingP;
newContactPerson.company=aCompany;
newContactPerson.phone=[NSNumber numberWithInteger:aPhone];
newContactPerson.email=aEmail;
newContactPerson.url=aUrl;
 
NSError *savingError=nil;
if ([self.managedObjectContext save:&savingError]) {
return YES;
}else{
NSLog(@"Failed to save the new person. Error = %@", savingError);}
return result;
}


+(NSArray *)findAll
{
//从coreData数据库读取数据
AppDelegate *appDelegate=[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context=[appDelegate managedObjectContext];
NSError *error;
NSFetchRequest *request=[[NSFetchRequest alloc]init];
NSEntityDescription *entityDes=[NSEntityDescription entityForName:@"ContactBook" inManagedObjectContext:context];
[request setEntity:entityDes];
NSSortDescriptor *sortXing=[[NSSortDescriptor alloc]initWithKey:@"xingP" ascending:YES];
NSSortDescriptor *sortMing=[[NSSortDescriptor alloc]initWithKey:@"mingP" ascending:YES];
NSArray *sortArray=[[NSArray alloc ]initWithObjects:sortXing,sortMing, nil];
request.sortDescriptors=sortArray;
NSArray *persons=[context executeFetchRequest:request error:&error];
return persons;
}
 
 
+(void)deleted:(ContactBook *)person
{
AppDelegate *appDelegate=[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context=[appDelegate managedObjectContext];
[context deleteObject:person];
NSError *savingError;
[context save:&savingError];//执行完删除操作后,存储上下文,更新数据库中的内容
}
 
+(NSArray *)findByName:(NSString *)name
{
AppDelegate *appDelegate=[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context=[appDelegate managedObjectContext];
NSError *error;
NSFetchRequest *request=[[NSFetchRequest alloc]init];
NSEntityDescription *entityDes=[NSEntityDescription entityForName:@"ContactBook" inManagedObjectContext:context];
[request setEntity:entityDes];
NSPredicate *pred=[NSPredicate predicateWithFormat:@"ming like [cd]%@",[NSString stringWithFormat:@"*%@*",name]];
[request setPredicate:pred];
NSArray *persons=[context executeFetchRequest:request error:&error];
return persons;
}
 
 
+(BOOL)checkEmaliAddress:(NSString*)address{
NSString *emailRegex=@"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
NSPredicate *p=[NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
return [p evaluateWithObject:address];
}



- (void)viewDidLoad

{

[superviewDidLoad];

//初始化管理器。

manager=[[CLLocationManageralloc]init];

manager.delegate=self;

manager.distanceFilter=1000;

manager.desiredAccuracy=kCLLocationAccuracyBest;

[managerstartUpdatingLocation];//开始当前服务。

//初始化地图。

mapView1=[[MKMapViewalloc]initWithFrame:CGRectMake(0, 0, 320, 460)];

// mapView1.delegate=self;

mapView1.mapType=MKMapTypeStandard;

[self.view addSubview:mapView1];

// [mapView1 release];

}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

NSLog(@"chenggongle");

mapView1.region=MKCoordinateRegionMake(CLLocationCoordinate2DMake(40+0.002, 116.34+0.014), MKCoordinateSpanMake(0.001, 0.001));

//添加注解。

myAnnotation=[[MyAnnotationalloc]init];

myAnnotation.coordinate=CLLocationCoordinate2DMake(40+0.002, 116.34+0.014);

myAnnotation.title=@"金马大厦";

myAnnotation.subtitle=@"博看文思";//这里如果还想添加图片按钮等则需要代理。

[mapView1addAnnotation:myAnnotation];

[myAnnotationrelease];

}

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{

for (MKPinAnnotationView *pinView in views) {

if ([pinView.annotation.title isEqualToString:@"Current Location"]) {

pinView.pinColor=MKPinAnnotationColorRed;

pinView.rightCalloutAccessoryView=nil;

continue;

}

//其他定位的注释是紫色带着一个按钮。

pinView.pinColor=MKPinAnnotationColorPurple;

UIButton *button=[UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];

[button addTarget:selfaction:@selector(go:) forControlEvents:UIControlEventTouchUpInside];

pinView.rightCalloutAccessoryView=button;

UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];

imageView.image=[UIImage imageNamed:@"fanqie.jpg"];

pinView.leftCalloutAccessoryView=imageView;

[imageView release];

}

}

-(void)go:(id)sender{

NSLog(@"chegnsagdfsf");

}

#import <CoreLocation/CoreLocation.h>

#import <MapKit/MapKit.h>

#import "MyAnnotation.h"

@interface MyMapViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate>

@property(retain,nonatomic)CLLocationManager *manager;//定位用的。

@property(retain,nonatomic)MKMapView *mapView1;//显示地图用的。

@property(retain,nonatomic)MyAnnotation *myAnnotation;

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>//继承注解需要这个头文件。

@interface MyAnnotation : NSObject<MKAnnotation>

@property (nonatomic) CLLocationCoordinate2D coordinate;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) NSString *subtitle;

NSString *match=@"^/w+@/w+(/./w+){1,2}$";//匹配邮箱。

NSString *match=@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";//匹配邮箱。

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",match];

NSLog(@"((((((()))))))%@",textField1.text);

if ([predicate evaluateWithObject:textField1.text]) {

NSLog(@"匹配成功");

}

else{

NSLog(@"匹配失败");

相关推荐

jyj00 / 0评论 2020-01-29