补充正则表达式,和NSRegularExpression

前端之家收集整理的这篇文章主要介绍了补充正则表达式,和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:urlString options:0range:NSMakeRange(0,[urlStringlength])];

if(firstMatch){

NSRangeresultRange= [firstMatch rangeAtIndex: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.text options:0 range:NSMakeRange(0,[textField.text length])];

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 =[NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];

return[emailTest evaluateWithObject:email];

}

2 。匹配9-15个由字母/数字组成的字符串的正则表达式:


NSString * regex = @"^[A-Za-z0-9]{9,15}$";
NSPredicate*pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL isMatch= [pred evaluateWithObject:txtfldPhoneNumber.text];


3.判断手机号码是否正确

- (BOOL)isMobileNumber:(NSString *)mobileNum
{
/**
* 手机号码
* 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 联通:130,131,132,152,155,156,185,186
* 电信:133,1349,153,180,189
*/
NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
/**
10 * 中国移动:China Mobile
11 * 134[0-8],188
12 */
NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
/**
15 * 中国联通:China Unicom
16 * 130,186
17 */
NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
/**
20 * 中国电信:China Telecom
21 * 133,189
22 */
NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
/**
25 * 大陆地区固话及小灵通
26 * 区号:010,020,021,022,023,024,025,027,028,029
27 * 号码:七位或八位
28 */
// NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";

NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CT];

if (([regextestmobile evaluateWithObject:mobileNum] == YES)
|| ([regextestcm evaluateWithObject:mobileNum] == YES)
|| ([regextestct evaluateWithObject:mobileNum] == YES)
|| ([regextestcu evaluateWithObject:mobileNum] == YES))
{
return YES;
}
else
{
return NO;
}

}


Cocoa用NSPredicate描述查询的方式,原理类似于在数据库中进行查询

用BETWEEN,IN,BEGINWITH,ENDWITH,CONTAINS,LIKE这些谓词来构造NSPredicate,必要的时候使用SELF直接对自己进行匹配

[cpp]
//基本的查询
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat: @"name =='Herbie'"];
BOOL match =[predicate evaluateWithObject: car];
NSLog(@"%s",(match) ? "YES" : "NO");
//在整个cars里面循环比较
predicate =[NSPredicate predicateWithFormat: @"engine.horsepower >150"];
NSArray*cars = [garage cars];
for (Car*car in [garage cars]) {
if ([predicate evaluateWithObject: car]){
NSLog (@"%@",car.name);
}
}
//输出完整的信息
predicate =[NSPredicate predicateWithFormat: @"engine.horsepower >150"];
NSArray*results;
results =[cars filteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
//含有变量的谓词
NSPredicate*predicateTemplate = [NSPredicate predicateWithFormat:@"name ==$NAME"];
NSDictionary*varDict;
varDict =[NSDictionarydictionaryWithObjectsAndKeys:
@"Herbie",@"NAME",nil];
predicate =[predicateTemplate predicateWithSubstitutionVariables:varDict];
NSLog(@"SNORGLE: %@",predicate);
match =[predicate evaluateWithObject: car];
NSLog (@"%s",(match) ? "YES" :"NO");
//注意不能使用$VARIABLE作为路径名,因为它值代表值
//谓词字符窜还支持c语言中一些常用的运算符

predicate =[NSPredicate predicateWithFormat:
@"(engine.horsepower > 50) AND (engine.horsepower <200)"];
results =[cars filteredArrayUsingPredicate:predicate];
NSLog (@"oop%@",results);

predicate =[NSPredicate predicateWithFormat: @"name <'Newton'"];
results =[cars filteredArrayUsingPredicate:predicate];
NSLog(@"%@",[results valueForKey:@"name"]);
//强大的数组运算符
predicate =[NSPredicate predicateWithFormat:
@"engine.horsepower BETWEEN { 50,200}"];
results =[cars filteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);

NSArray*betweens = [NSArray arrayWithObjects:
[NSNumber numberWithInt: 50],[NSNumber numberWithInt: 200],nil];
predicate =[NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN %@",betweens];
results =[cars filteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
predicateTemplate = [NSPredicate predicateWithFormat:@"engine.horsepower BETWEEN $POWERS"];
varDict =[NSDictionary dictionaryWithObjectsAndKeys: betweens,@"POWERS",nil];
predicate =[predicateTemplate predicateWithSubstitutionVariables:varDict];
results =[cars filteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
//IN运算符
predicate =[NSPredicate predicateWithFormat: @"name IN { 'Herbie','Snugs','Badger','Flap' }"];
results =[cars filteredArrayUsingPredicate:predicate];
NSLog(@"%@",[results valueForKey:@"name"]);
predicate =[NSPredicate predicateWithFormat: @"SELF.name IN { 'Herbie',[results valueForKey:@"name"]);

names =[cars valueForKey: @"name"];
predicate =[NSPredicate predicateWithFormat: @"SELF IN { 'Herbie','Flap' }"];
results =[names filteredArrayUsingPredicate:predicate];//这里限制了SELF的范围
NSLog(@"%@",results);
//BEGINSWITH,ENDSWITH,CONTAINS
//附加符号,[c],[d],[cd],c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分
predicate =[NSPredicate predicateWithFormat: @"name BEGINSWITH'Bad'"];
results =[cars filteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);

predicate =[NSPredicate predicateWithFormat: @"name BEGINSWITH'HERB'"];
results =[cars filteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);

predicate =[NSPredicate predicateWithFormat: @"name BEGINSWITH[cd]'HERB'"];
results =[cars filteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
//LIKE运算符(通配符)
predicate =[NSPredicate predicateWithFormat: @"name LIKE[cd]'*er*'"];
results =[cars filteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);

predicate =[NSPredicate predicateWithFormat: @"name LIKE[cd]'???er*'"];
results =[cars filteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);

//基本的查询 NSPredicate *predicate; predicate = [NSPredicate predicateWithFormat: @"name =='Herbie'"]; BOOL match =[predicate evaluateWithObject: car]; NSLog(@"%s",(match) ? "YES" : "NO"); //在整个cars里面循环比较 predicate =[NSPredicate predicateWithFormat: @"engine.horsepower >150"]; NSArray*cars = [garage cars]; for (Car*car in [garage cars]) { if ([predicate evaluateWithObject: car]) { NSLog (@"%@",car.name); } } //输出完整的信息 predicate =[NSPredicate predicateWithFormat: @"engine.horsepower >150"]; NSArray*results; results =[cars filteredArrayUsingPredicate: predicate]; NSLog(@"%@",results); //含有变量的谓词 NSPredicate*predicateTemplate = [NSPredicate predicateWithFormat:@"name ==$NAME"]; NSDictionary*varDict; varDict =[NSDictionary dictionaryWithObjectsAndKeys: @"Herbie",nil]; predicate =[predicateTemplate predicateWithSubstitutionVariables:varDict]; NSLog(@"SNORGLE: %@",predicate); match =[predicate evaluateWithObject: car]; NSLog (@"%s",(match) ? "YES" : "NO"); //注意不能使用$VARIABLE作为路径名,因为它值代表值 //谓词字符窜还支持c语言中一些常用的运算符 predicate =[NSPredicate predicateWithFormat: @"(engine.horsepower > 50) AND (engine.horsepower <200)"]; results =[cars filteredArrayUsingPredicate: predicate]; NSLog (@"oop%@",results); predicate =[NSPredicate predicateWithFormat: @"name < 'Newton'"]; results =[cars filteredArrayUsingPredicate: predicate]; NSLog(@"%@",[results valueForKey: @"name"]); //强大的数组运算符 predicate =[NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN { 50,200 }"]; results =[cars filteredArrayUsingPredicate: predicate]; NSLog(@"%@",results); NSArray*betweens = [NSArray arrayWithObjects: [NSNumber numberWithInt: 50],nil]; predicate =[NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN %@",betweens]; results =[cars filteredArrayUsingPredicate: predicate]; NSLog(@"%@",results); predicateTemplate = [NSPredicate predicateWithFormat:@"engine.horsepower BETWEEN $POWERS"]; varDict =[NSDictionary dictionaryWithObjectsAndKeys: betweens,nil]; predicate =[predicateTemplate predicateWithSubstitutionVariables:varDict]; results =[cars filteredArrayUsingPredicate: predicate]; NSLog(@"%@",results); //IN运算符 predicate =[NSPredicate predicateWithFormat: @"name IN { 'Herbie','Flap' }"]; results =[cars filteredArrayUsingPredicate: predicate]; NSLog(@"%@",[results valueForKey: @"name"]); predicate =[NSPredicate predicateWithFormat: @"SELF.name IN { 'Herbie',[results valueForKey: @"name"]); names =[cars valueForKey: @"name"]; predicate =[NSPredicate predicateWithFormat: @"SELF IN { 'Herbie','Flap' }"]; results =[names filteredArrayUsingPredicate:predicate];//这里限制了SELF的范围 NSLog(@"%@",results); //BEGINSWITH,CONTAINS //附加符号,[c],c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分 predicate =[NSPredicate predicateWithFormat: @"name BEGINSWITH'Bad'"]; results =[cars filteredArrayUsingPredicate: predicate]; NSLog(@"%@",results); predicate =[NSPredicate predicateWithFormat: @"name BEGINSWITH'HERB'"]; results =[cars filteredArrayUsingPredicate: predicate]; NSLog(@"%@",results); predicate =[NSPredicate predicateWithFormat: @"name BEGINSWITH[cd]'HERB'"]; results =[cars filteredArrayUsingPredicate: predicate]; NSLog(@"%@",results); //LIKE运算符(通配符) predicate =[NSPredicate predicateWithFormat: @"name LIKE[cd]'*er*'"]; results =[cars filteredArrayUsingPredicate: predicate]; NSLog(@"%@",results); predicate =[NSPredicate predicateWithFormat: @"name LIKE[cd]'???er*'"]; results =[cars filteredArrayUsingPredicate: predicate]; NSLog(@"%@",results);

原文链接:https://www.f2er.com/regex/360486.html

猜你在找的正则表达式相关文章