首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

正则表达式的简略使用

2012-11-23 
正则表达式的简单使用在 4.0 之后,系统就有了它自己的类(NSRegularExpression,NSRegularExpression)来使用

正则表达式的简单使用

在 4.0 之后,系统就有了它自己的类(NSRegularExpression,NSRegularExpression)来使用正则表达式,,之前都是要添加第三方类库 RegexKitLite 来使用

这两个类的简单使用:

NSString *str = @"3sdfh*odsi";        //匹配第一个字符是数字    NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:@"\\b\\d.*" options:0 error:nil];    if (regex1 != nil) {        NSTextCheckingResult *result1 = [regex1 firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];        if (result1) {            NSLog(@"第一个是数字");        }else{            NSLog(@"第一个不是数字");        }    }    //匹配特殊字符  \W (W是大写)匹配任意不是字母,数字,下划线,汉字的字符    NSRegularExpression *regex2 = [NSRegularExpression regularExpressionWithPattern:@".*\\W.*" options:0 error:nil];    if (regex2) {        NSTextCheckingResult *result2 = [regex2 firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];        if (result2) {            NSLog(@"有特殊字符");        }else{            NSLog(@"没有特殊字符");        }    }


热点排行