iOS中使用正则表达式NSRegularExpression 来验证textfiled输入的内容
何谓正则表达式
正则表达式(regularexpression),在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或替换那些符合某个模式的文本内容。正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。正则表达式通常缩写成“regex”,单数有regexp、regex,复数有regexps、regexes、regexen。
正则表达式组成
正则表达式有两种类型的字符组成
第一种:用来匹配的字符,或者叫常规字符
第二种:控制字符或具有特殊含义的元字符
iphone4.0以后就开始支持正则表达式的使用了,在ios4.0中正则表达式的使用是使用NSRegularExpression类来调用。
1.下面一个简单的使用正则表达式的一个例子:NSRegularExpression类
-(void)parseString{
//组装一个字符串,需要把里面的网址解析出来
NSString*urlString=@"sfdsfhttp://www.baidu.com";
//NSRegularExpression类里面调用表达的方法需要传递一个NSError的参数。下面定义一个
NSError*error;
//http+:[^\\s]*这个表达式是检测一个网址的。
NSRegularExpression*regex=[NSRegularExpressionregularExpressionWithPattern:@"http+:[^\\s]*"options:0error:&error];
if(regex!=nil){
NSTextCheckingResult*firstMatch=[regexfirstMatchInString:urlStringoptions:0range:NSMakeRange(0,[urlStringlength])];
if(firstMatch){
NSRangeresultRange=[firstMatchrangeAtIndex:0];//等同于firstMatch.range---相匹配的范围
//从urlString当中截取数据
NSString*result=[urlStringsubstringWithRange:resultRange];
//输出结果
NSLog(@"%@",result);
}
}
}
2.使用正则表达式来判断
//初始化一个NSRegularExpression对象,并设置检测对象范围为:0-9
NSRegularExpression*regex2=[NSRegularExpressionregularExpressionWithPattern:@"^[0-9]*$"options:0error:nil];
if(regex2)
{//对象进行匹配
NSTextCheckingResult*result2=[regex2firstMatchInString:textField.textoptions:0range:NSMakeRange(0,[textField.textlength])];
if(result2){
}
}
1.判断邮箱格式是否正确的代码:NSPredicatel类
//利用正则表达式验证
NSPredicatel类:主要用来指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配。谓词是指在计算机中表示计算真假值的函数。原理和用法都类似于SQL查询中的where,作用相当于数据库的过滤取。主要用于从集合中分拣出符合条件的对象,也可以用于字符串的正则匹配
-(BOOL)isValidateEmail:(NSString*)email
{
NSString*emailRegex=@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate*emailTest=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",emailRegex];
return[emailTestevaluateWithObject:email];
}
2.匹配9-15个由字母/数字组成的字符串的正则表达式:
NSString*regex=@"^[A-Za-z0-9]{9,15}$";
NSPredicate*pred=[NSPredicatepredicateWithFormat:@"SELFMATCHES%@",regex];
BOOLisMatch=[predevaluateWithObject:txtfldPhoneNumber.text];
Cocoa用NSPredicate描述查询的方式,原理类似于在数据库中进行查询
用BETWEEN,IN,BEGINWITH,ENDWITH,CONTAINS,LIKE这些谓词来构造NSPredicate,必要的时候使用SELF直接对自己进行匹配
//基本的查询
NSPredicate*predicate;
predicate=[NSPredicatepredicateWithFormat:@"name=='Herbie'"];
BOOLmatch=[predicateevaluateWithObject:car];
NSLog(@"%s",(match)?"YES":"NO");
//在整个cars里面循环比较
predicate=[NSPredicatepredicateWithFormat:@"engine.horsepower>150"];
NSArray*cars=[garagecars];
for(Car*carin[garagecars]){
if([predicateevaluateWithObject:car]){
NSLog(@"%@",car.name);
}
}
//输出完整的信息
predicate=[NSPredicatepredicateWithFormat:@"engine.horsepower>150"];
NSArray*results;
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
//含有变量的谓词
NSPredicate*predicateTemplate=[NSPredicatepredicateWithFormat:@"name==$NAME"];
NSDictionary*varDict;
varDict=[NSDictionarydictionaryWithObjectsAndKeys:
@"Herbie",@"NAME",nil];
predicate=[predicateTemplatepredicateWithSubstitutionVariables:varDict];
NSLog(@"SNORGLE:%@",predicate);
match=[predicateevaluateWithObject:car];
NSLog(@"%s",(match)?"YES":"NO");
//注意不能使用$VARIABLE作为路径名,因为它值代表值
//谓词字符窜还支持c语言中一些常用的运算符
predicate=[NSPredicatepredicateWithFormat:
@"(engine.horsepower>50)AND(engine.horsepower<200)"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"oop%@",results);
predicate=[NSPredicatepredicateWithFormat:@"name<'Newton'"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",[resultsvalueForKey:@"name"]);
//强大的数组运算符
predicate=[NSPredicatepredicateWithFormat:
@"engine.horsepowerBETWEEN{50,200}"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
NSArray*betweens=[NSArrayarrayWithObjects:
[NSNumbernumberWithInt:50],[NSNumbernumberWithInt:200],nil];
predicate=[NSPredicatepredicateWithFormat:@"engine.horsepowerBETWEEN%@",betweens];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
predicateTemplate=[NSPredicatepredicateWithFormat:@"engine.horsepowerBETWEEN$POWERS"];
varDict=[NSDictionarydictionaryWithObjectsAndKeys:betweens,@"POWERS",nil];
predicate=[predicateTemplatepredicateWithSubstitutionVariables:varDict];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
//IN运算符
predicate=[NSPredicatepredicateWithFormat:@"nameIN{'Herbie','Snugs','Badger','Flap'}"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",[resultsvalueForKey:@"name"]);
predicate=[NSPredicatepredicateWithFormat:@"SELF.nameIN{'Herbie','Snugs','Badger','Flap'}"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",[resultsvalueForKey:@"name"]);
names=[carsvalueForKey:@"name"];
predicate=[NSPredicatepredicateWithFormat:@"SELFIN{'Herbie','Snugs','Badger','Flap'}"];
results=[namesfilteredArrayUsingPredicate:predicate];//这里限制了SELF的范围
NSLog(@"%@",results);
//BEGINSWITH,ENDSWITH,CONTAINS
//附加符号,[c],[d],[cd],c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分
predicate=[NSPredicatepredicateWithFormat:@"nameBEGINSWITH'Bad'"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
predicate=[NSPredicatepredicateWithFormat:@"nameBEGINSWITH'HERB'"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
predicate=[NSPredicatepredicateWithFormat:@"nameBEGINSWITH[cd]'HERB'"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
//LIKE运算符(通配符)
predicate=[NSPredicatepredicateWithFormat:@"nameLIKE[cd]'*er*'"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
predicate=[NSPredicatepredicateWithFormat:@"nameLIKE[cd]'???er*'"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
//基本的查询
NSPredicate*predicate;
predicate=[NSPredicatepredicateWithFormat:@"name=='Herbie'"];
BOOLmatch=[predicateevaluateWithObject:car];
NSLog(@"%s",(match)?"YES":"NO");
//在整个cars里面循环比较
predicate=[NSPredicatepredicateWithFormat:@"engine.horsepower>150"];
NSArray*cars=[garagecars];
for(Car*carin[garagecars]){
if([predicateevaluateWithObject:car]){
NSLog(@"%@",car.name);
}
}
//输出完整的信息
predicate=[NSPredicatepredicateWithFormat:@"engine.horsepower>150"];
NSArray*results;
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
//含有变量的谓词
NSPredicate*predicateTemplate=[NSPredicatepredicateWithFormat:@"name==$NAME"];
NSDictionary*varDict;
varDict=[NSDictionarydictionaryWithObjectsAndKeys:
@"Herbie",@"NAME",nil];
predicate=[predicateTemplatepredicateWithSubstitutionVariables:varDict];
NSLog(@"SNORGLE:%@",predicate);
match=[predicateevaluateWithObject:car];
NSLog(@"%s",(match)?"YES":"NO");
//注意不能使用$VARIABLE作为路径名,因为它值代表值
//谓词字符窜还支持c语言中一些常用的运算符
predicate=[NSPredicatepredicateWithFormat:
@"(engine.horsepower>50)AND(engine.horsepower<200)"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"oop%@",results);
predicate=[NSPredicatepredicateWithFormat:@"name<'Newton'"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",[resultsvalueForKey:@"name"]);
//强大的数组运算符
predicate=[NSPredicatepredicateWithFormat:
@"engine.horsepowerBETWEEN{50,200}"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
NSArray*betweens=[NSArrayarrayWithObjects:
[NSNumbernumberWithInt:50],[NSNumbernumberWithInt:200],nil];
predicate=[NSPredicatepredicateWithFormat:@"engine.horsepowerBETWEEN%@",betweens];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
predicateTemplate=[NSPredicatepredicateWithFormat:@"engine.horsepowerBETWEEN$POWERS"];
varDict=[NSDictionarydictionaryWithObjectsAndKeys:betweens,@"POWERS",nil];
predicate=[predicateTemplatepredicateWithSubstitutionVariables:varDict];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
//IN运算符
predicate=[NSPredicatepredicateWithFormat:@"nameIN{'Herbie','Snugs','Badger','Flap'}"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",[resultsvalueForKey:@"name"]);
predicate=[NSPredicatepredicateWithFormat:@"SELF.nameIN{'Herbie','Snugs','Badger','Flap'}"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",[resultsvalueForKey:@"name"]);
names=[carsvalueForKey:@"name"];
predicate=[NSPredicatepredicateWithFormat:@"SELFIN{'Herbie','Snugs','Badger','Flap'}"];
results=[namesfilteredArrayUsingPredicate:predicate];//这里限制了SELF的范围
NSLog(@"%@",results);
//BEGINSWITH,ENDSWITH,CONTAINS
//附加符号,[c],[d],[cd],c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分
predicate=[NSPredicatepredicateWithFormat:@"nameBEGINSWITH'Bad'"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
predicate=[NSPredicatepredicateWithFormat:@"nameBEGINSWITH'HERB'"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
predicate=[NSPredicatepredicateWithFormat:@"nameBEGINSWITH[cd]'HERB'"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
//LIKE运算符(通配符)
predicate=[NSPredicatepredicateWithFormat:@"nameLIKE[cd]'*er*'"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
predicate=[NSPredicatepredicateWithFormat:@"nameLIKE[cd]'???er*'"];
results=[carsfilteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
以上就是小编给大家分享的iOS中使用正则表达式NSRegularExpression来验证textfiled输入的内容,希望大家喜欢。