正则表达式基础(1)
发布时间:2020-12-14 04:33:38 所属栏目:百科 来源:网络整理
导读:常用元符号 代码 说明 . 匹配除换行符以外的任意字符 w 匹配字母或数字或下划线或汉字 s 匹配任意的空白符 d 匹配数字 b 匹配单词的开始或结束 ^ 匹配字符串的开始 $ 匹配字符串的结束 代码 说明 * 重复零次或更多次 + 重复一次或更多次 ? 重复零次或一
常用元符号
//只能是数字或英文,或者两者都存在
方法一:
NSString *regex=@"^[a-z0-9A-Z]+$";
//创建谓词对象设定条件表达式
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
//判断的字符串
NSString *str=@"Hello123";
//对字符串进行判断
if ([predicate evaluateWithObject:str]) {
NSLog(@"匹配到了");
}
方法二:
NSString *url=@"HelloWorld2016";
NSError *error;
//创建
NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:@"^[a-z0-9A-Z]+$" options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *array=[regex matchesInString:url options:0 range:NSMakeRange(0,url.length)];
for (NSTextCheckingResult *result in array) {
NSRange range=[result range];
NSString *str=[url substringWithRange:range];
NSLog(@"%@",str);
}
打印结果: ^表示匹配字符串的开始,$表示匹配字符串的结束,+表示重复一次或更多次,[a-z0-9A-Z]表示匹配的内容为 (a…z或0…9或A…Z) //截取制定内容 qq.
NSString *url=@"1229436624@qq.com";
NSError *error;
//创建
NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:@"[^@]*." options:0 error:&error];
if (!error) {//没有错误
//获取特定字符串范围
NSTextCheckingResult *match=[regex firstMatchInString:url options:0 range:NSMakeRange(0,url.length)];
if (match) {
//截取特定字符串
NSString *result=[url substringWithRange:match.range];
NSLog(@"%@",result);
}
}else{
//打印错误
NSLog(@"error==>%@",error);
}
//匹配-开始0结束
NSString *regex=@"-d*d0";
NSString *str=@"-340aaaa";
NSError *error;
NSRegularExpression *regular=[NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches=[regular matchesInString:str options:0 range:NSMakeRange(0,str.length)];
//遍历匹配后的每一条记录
for (NSTextCheckingResult *matchs in matches) {
NSRange range = [matchs range];
NSString *mStr = [str substringWithRange:range];
NSLog(@"%@",mStr);
}
- 表示已-开始,d* 表示重复零次或更多次,d0表示已0结束 // b为元字符,代表单词的开始和结束 .表示非空格字符 *表示数量
// bXXb
NSString *url=@"hello zw";
//创建
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"self matches %@",@"bhellob.*bzwb"];
if ([predicate evaluateWithObject:url]) {
NSLog(@"匹配到了");
}
}
//匹配5-12个数字
NSString *url=@"2222222222";
NSError *error;
//创建,匹配连续的5-12个数字
//1.为整个字符中有连续的5-12个数字
// NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:@"d{5,12}" options:0 error:&error];
//2.为整个字符串为5-12的
NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:@"^d{5,12}$" options:0 error:&error];
if (!error) {//没有错误
//获取特定字符串范围
NSTextCheckingResult *match=[regex firstMatchInString:url options:0 range:NSMakeRange(0,result);
}
}else{
//打印错误
NSLog(@"error==>%@",error);
}
d{5,12}表示5-12个数字,{}表示范围数 //匹配区域号码
NSString *url=@"0122-2222222";
NSError *error;
NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:@"0d{2}-d{8}|0d{3}-d{7}" options:0 error:&error];
if (!error) {//没有错误
//获取特定字符串范围
NSTextCheckingResult *match=[regex firstMatchInString:url options:0 range:NSMakeRange(0,error);
}
//匹配abc中任意一个
NSString *url=@"abcd";
NSError *error;
NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:@"[abc]" options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches=[regex matchesInString:url options:0 range:NSMakeRange(0,url.length)];
//遍历匹配后的每一条记录
for (NSTextCheckingResult *matchs in matches) {
NSRange range=[matchs range];
NSString *mStr = [url substringWithRange:range];
NSLog(@"%@",mStr);
}
//匹配除abc之外的
NSString *url=@"abcd";
NSError *error;
// NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:@"^abc" options:0 error:&error];
//匹配除abc之外的
NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:@"[^abc]" options:0 error:&error];
if (!error) {
NSTextCheckingResult *result=[regex firstMatchInString:url options:0 range:NSMakeRange(0,url.length)];
if (result) {
NSString *str=[url substringWithRange:result.range];
NSLog(@"%@",str );
}
}
//只能为中文或字母
NSString *Regex = @"^[A-Za-z|u4e00-u9fa5]+$";
NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",Regex];
// 是否为纯英文
NSString *Regex = @"^[A-Za-z]+$";
NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",Regex];
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |