正则表达式

首先了解正则表达式,可以去看这片文章,讲的很好:http://www.jb51.net/tools/zhengze.html

正则表达式开源实现类:RegexKitLite,需要导入依赖库:libicucore.dylib

举个简单的例子:

1.查找数字串

NSString *text1= @"张三:010-888844,李四:00-64826643";

    NSString *regex1 = @"\\d{2,3}-\\d{6,8}";

    NSArray *array1 = [text1 componentMatchedByRegex:regex1];

    NSLog(@"%@",array1);

注意点:1).  \\d而不是\d  2).必须导入依赖库,否则无法使用      

 

2.查找单词(以w开头)

NSString *text2 = @"today we will go to garden and have fun";

    NSString *regex2 = @"\\bw[a-zA-Z]*\\b";

    NSArray *array2 = [text2 componentMatchedByRegex:regex2];

    NSLog(@"%@",array2);

 

总的来说,只要把开头推荐的文章好好看一遍,再稍稍练习下,一般的都可以解决,至于更难的,要自己去研究了。

 

posted @ 2016-03-16 11:12  亡鸟补笼  阅读(104)  评论(0)    收藏  举报