zoukankan      html  css  js  c++  java
  • iOS系统自带正则表达式简单运用

        //组装一个字符串,把里面的网址解析出来

        NSString *urlString = @"sfdshttp://www.baidu.com";

        NSError *error;

        //http+:[^\s]* 这是检测网址的正则表达式

        NSRegularExpression *regex = [NSRegularExpressionregularExpressionWithPattern:@"http+:[^\s]*"options:0error:&error];

        

        if (regex != nil) {

            NSTextCheckingResult *firstMatch = [regex firstMatchInString:urlString options:0range:NSMakeRange(0, [urlString length])];

            

            if (firstMatch) {

                NSRange resultRange = [firstMatch rangeAtIndex:0];

                //urlString中截取数据

                NSString *result = [urlString substringWithRange:resultRange];

                NSLog(@"%@",result);

            }

        }

     

     

        NSString *stringC = @"weiboyuan@163.com";

        //匹配输入的联系方式是否为QQ号码或者电子邮箱

        NSString *patternQQ = @"^[1-9](\d){4,9}$";

        NSString *patternEmail = @"\b([a-zA-Z0-9%_.+\-]+)@([a-zA-Z0-9.\-]+?\.[a-zA-Z]{2,6})\b";

    //    NSError *error = NULL;

        //定义正则表达式

        NSRegularExpression *regexQQ = [NSRegularExpressionregularExpressionWithPattern:patternQQ options:0error:&error];

        NSRegularExpression *regexEmail = [NSRegularExpressionregularExpressionWithPattern:patternEmail options:0error:&error];

        //使用正则表达式匹配字符

        NSTextCheckingResult *isMatchQQ = [regexQQ firstMatchInString:stringC

                                                              options:0

                                                                range:NSMakeRange(0, [stringC length])];

        NSTextCheckingResult *isMatchEmail = [regexEmail firstMatchInString:stringC

                                                                    options:0

                                                                      range:NSMakeRange(0, [stringC length])];

        

        if (isMatchQQ || isMatchEmail)

        {

            NSLog(@" QQ或者邮箱");

        }

        else

        {

            NSLog(@"不是 QQ或者邮箱");

        }

  • 相关阅读:
    多线程——newFixedThreadPool线程池
    mysql SQL优化之嵌套查询-遁地龙卷风
    mysql练习题-查询同时参加计算机和英语考试的学生的信息-遁地龙卷风
    mysql存储过程编写-入门案例-遁地龙卷风
    编程轶事-java中的null-遁地龙卷风
    正逆向思维-编程轶事-遁地龙卷风
    MyBatis处理一行数据-MyBatis使用sum语句报错-MyBatis字段映射-遁地龙卷风
    MyBatis框架在控制台打印Sql语句-遁地龙卷风
    3d转换-正方体-Html5Css3-遁地龙卷风
    突破瓶颈-遁地龙卷风
  • 原文地址:https://www.cnblogs.com/weiboyuan/p/3440999.html
Copyright © 2011-2022 走看看