正则表达式基础(1)

常用元符号

代码说明
.匹配除换行符以外的任意字符
\w匹配字母或数字或下划线或汉字
\s匹配任意的空白符
\d匹配数字
\b匹配单词的开始或结束
^匹配字符串的开始
$匹配字符串的结束

代码说明
*重复零次或更多次
+重复一次或更多次
?重复零次或一次
{n}重复n次
{n,}重复n次或更多次
{n,m}重复n到m次

代码说明
\W匹配任意不是字母,数字,下划线,汉字的字符
\S匹配任意不是空白符的字符
\D匹配任意非数字的字符
\B匹配不是单词开头或结束的位置
[^x]匹配除了x以外的任意字符
[^aeiou]匹配除了aeiou这几个字母以外的任意字符
//只能是数字或英文,或者两者都存在
方法一:
 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);
    }

这里写图片描述
[^@] 表示匹配内容为不包含@, * 表示一个或任意多个,.表示最后一个字符为. ( \c表示最后一个字符为c )

//匹配-开始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为元字符,代表单词的开始和结束 .表示非空格字符  *表示数量
//   \bXX\b
 NSString *url=@"hello zw";
 //创建
 NSPredicate *predicate=[NSPredicate predicateWithFormat:@"self matches %@",@"\\bhello\\b.*\\bzw\\b"];

    if ([predicate evaluateWithObject:url]) {
        NSLog(@"匹配到了");
    }
   }
//匹配5-12个数字
NSString *url=@"1111111111";
    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, url.length)];
        if (match) {
            //截取特定字符串
            NSString *result=[url substringWithRange:match.range];
            NSLog(@"%@",result);
        }
    }else{
        //打印错误
        NSLog(@"error==>%@",error);
    }

\d{5,12}表示5-12个数字,{}表示范围数

//匹配区域号码
 NSString *url=@"0122-2222222";
    NSError *error;
    NSRegularExpression *regex=[NSRegularExpression regularExpressionWithPattern:@"0\\d{2}-\\d{8}|0\\d{3}-\\d{7}" 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);
    }
 //匹配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];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值