微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

补充正则表达式,和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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


正则替换html代码中img标签的src值在开发富文本信息在移动端展示的项目中,难免会遇到后台返回的标签文本信息中img标签src属性按照相对或者绝对路径返回的形式,类似:<img src="qinhancity/v1.0.0/ima
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它是专门为文本处理设计的编程语言,也是行处理软件,通常用于扫描,过滤,统计汇总等工作,数据可以来自标准输入也可以是管道或文件。当读到第一行时,匹配条件,然后执行指定动作,在接着读取第二行数据处理,不会默认输出。如果没有定义匹配条件,则是默认匹配所有数据行,awk隐含循环,条件匹配多少次,动作就会执行多少次。逐行读取文本,默认以空格或tab键为分割符进行分割,将分割所得的各个字段,保存到内建变量中,并按模式或或条件执行编辑命令。与sed工作原理相比:s
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及他们的组合组成了一个规则,然后检查一个字符串是否与这种规则匹配来实现对字符的过滤或匹配。我们刚才在学习正则表达式的时候,我们表示数字,字母下划线的时候是用w表示的,为什么我们在书写的时候用的是w?我们可以发现我们分割空格的话,并没有达到我们预期的效果,这里我们可以使用正则表达式的方式进行分割。我们可以发现,我们和上面得到的结果不一致,既然出错了,肯定是我们的使用方式不对。看到这里我们就能感受到正则表达式的作用了,正则表达式是字符串处理的有力工具。
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发布,同步更新到和。欢迎大家投稿,,推荐或者自荐开源项目/资源/工具/文章~
本文涉及Shell函数,Shell中的echo、printf、test命令等。
常用正则表达,包括: 密码、 手机号、 身份证、 邮箱、 中文、 车牌号、 微信号、 日期 YYYY-MM-DD hh:mm:ss、 日期 YYY-MM-DD、 十六进制颜色、 邮政编号、 用户名、 QQ号
一、python【re】的用法1、re.match函数·单一匹配-推荐指数【★★】2、re.search函数·单一匹配-推荐指数【★★★★★】3、re.findall函数·多项匹配-推荐指数【★★★★★】4、re.finditer函数·多项匹配-推荐指数【★★★★】5、re.sub函数·替换函数-推荐指数【★★★★】二、正则表达式示例·总有一款适合你1、正则表达式匹配HTML指定id/class的标签2、正则表达式匹配HTML中所有a标签中的各类属性值3、获取标签的文本值
1.借助词法分析工具Flex或Lex完成(参考网络资源)2.输入:高级语言源代码(如helloworld.c)3.输出:以二元组表示的单词符号序列。通过设计、编制、调试一个具体的词法分析程序,加深对词法分析原理的理解,并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。由于各种不同的高级程序语言中单词总体结构大致相同,基本上都可用一组正则表达式描述,所以构造这样的自动生成系统:只要给出某高级语言各类单词词法结构的一组正则表达式以及识别各类单词时词法分析程序应采取的语义动作,该系统