RegexKitLite的使用

作用:正则表达式的iOS开源库

官方文档:http://regexkit.sourceforge.net/RegexKitLite/

正则表达式 30分钟入门教程:

中文:http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaocheng.html

英文:http://www.codeproject.com/Articles/9099/The-Minute-Regex-Tutorial

使用:

1,github下载:https://github.com/samdeane/RegexKitLite

2,将RegexKitLite.h 和RegexKitLite.m导入到工程。

3,添加libicucore.dylib frameworks:工程 ->Targets ->Build Phases ->Link
Binary With Libarries。

(ARC设置:targets->build phases中修改compiler Flags。添加:-fobjc-arc,可以让项目支持arc。如果想让原来支持arc的不使用arc则添加-fno-objc-arc)

4,实例:对微博的#话题#、@ta、http://... 进行匹配和替换

#import <Foundation/Foundation.h>
#import "RegexKitLite.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        //微博:@用户 http://.... #话题#

        NSString *test = @"我在#话题#上课@麻子 你们@罗 在听吗?https://www.baidu.com";

        //匹配@并且@后面可有n个字符(不包含空格)
        NSString *regex = @"@\\w+";

        //匹配#
        regex = @"#\\w+#";

        //匹配超链接:1,?表示匹配1次或0次。超链接的匹配是根据链接复杂度而定的。
        regex = @"http(s)?://([A-Za-z0-9.?_-]+(/)?)*";

        //把以上3个合成一个:使用或|隔开,每隔小单元格用小括号包含。
        regex = @"(@\\w+)|(#\\w+#)|(http(s)?://([A-Za-z0-9.?_-]+(/)?)*)";

        NSArray *regexArray = [test componentsMatchedByRegex:regex];

        for (NSString *s in regexArray)
        {
            NSLog(@"匹配结果s:%@",s);
            //字符串替换
            NSString *target = [NSString stringWithFormat:@"<a href=‘%@‘>%@<a>",s,s];
            test = [test stringByReplacingOccurrencesOfString:s withString:target];
        }
        NSLog(@"替换后:%@",test);
    }
    return 0;
}

输出:

2014-04-30 11:27:06.340 regexKitLite[1269:303] 匹配结果s:#话题#
2014-04-30 11:27:06.342 regexKitLite[1269:303] 匹配结果s:@麻子
2014-04-30 11:27:06.342 regexKitLite[1269:303] 匹配结果s:@罗
2014-04-30 11:27:06.343 regexKitLite[1269:303] 匹配结果s:https://www.baidu.com
2014-04-30 11:27:06.343 regexKitLite[1269:303] 替换后:我在<a href=‘#话题#‘>#话题#<a>上课<a href=‘@麻子‘>@麻子<a> 你们<a href=‘@罗‘>@罗<a> 在听吗?<a href=‘https://www.baidu.com‘>https://www.baidu.com<a>

RegexKitLite的使用,码迷,mamicode.com

时间: 2024-09-13 01:53:27

RegexKitLite的使用的相关文章

(一一〇)正则表达式的基本使用与RegexKitLite的使用

正则表达式常常用于匹配关键字,下面先介绍基本语法. [基本语法] ①中括号表示满足其中之一即可,例如[abc],则这个位置可以是a.b.c中任意一个. ②在中括号中,可以通过-连接范围,例如a-z:多个范围之间并列不需要任何分隔符,例如[a-zA-Z] ③表示重复次数用{x},例如[a-z]{2}表示连续2次:表示重复次数的范围可用{x,y}. ④\\d表示数字. ⑤正则表达式默认的是贪婪匹配,例如[a-z]{2,4},如果出现类似abcde2ab这样的字符串,abcd满足最大长度4,因此会作为

iOS 使用正则表达式库RegexKitLite的问题

因为RegexKitLite使用ICU库,所以需要动态链接到/usr/lib/libicucore.dylib库当中去,否则你会得到错误.具体Dynamically linked to /usr/lib/libicucore.dylib方法如下 1.进入菜单Project->Edit Project Settings2.在搜索框内输入linker搜索,找到”Other Linker Flags”选项.3. 加入一个新的标签,名为-licucore. 补充:ICU(International Co

iOS中使用RegexKitLite来试用正则表达式

转:http://blog.csdn.net/nullcn/article/details/6338592 准备工作,下载RegexKitLite 软件包,解压后有2个文件,需要加载到project中. 然后还要加载framework libicucore.dylib ,因为RegexKitLite是调用这个里面的API,苹果规定过不能使用私有的api和没有发布的api.实际上RegexKitLite对NSString做了扩展,目前只支持NSString,对我来说也够了... 基本使用的例子(更

iOS中使用RegexKitLite来试用正则表达式 使用ARC 20个错误解决办法

You can also disable the ARC for the RegexKitLite only by adding a flag: select the project -> YOUR Target -> on the Tab the "Build Phases" and open the "Compile Sources" and add for "RegexKitLite.m" the flag "-fno

iOS开发 - 正则表达式 RegexKitLite框架

准备工作 使用RegexKitLite正则表达式需要以下工作: 1.RegexKitLite官方网址(内含使用教程):http://regexkit.sourceforge.net/RegexKitLite 2.下载地址:http://downloads.sourceforge.net/regexkit/RegexKitLite-4.0.tar.bz2 3.将RegexKitLite.h和RegexKitLite.m引入到工程中. 4.引入"libicucore.dylib"框架 5.

ios下最简单的正则,RegexKitLite

ios下最简单的正则,RegexKitLite 1.去RegexKitLite下载类库,解压出来会有一个例子包及2个文件,其实用到的就这2个文件,添加到工程中.备用地址:http://www.cocoachina.com/bbs/job.php?action-download-pid-135286-tid-18111-aid-11143.html- Lv 2.工程中添加libicucore.dylib frameworks. 3.现在所有的nsstring对象就可以调用RegexKitLite中

RegexKitLite 使用详解

1.去RegexKitLite下载类库,解压出来会有一个例子包及2个文件,其实用到的就这2个文件,添加到工程中. 2.工程中添加libicucore.dylib frameworks. 友情提醒:一般人导入RegexKitLite编译报错,正是因为没有导入这个类库,加上这个就OK了 3.现在所有的nsstring对象就可以调用RegexKitLite中的方法了. NSString *email = @”[email protected]”; [email isMatchedByRegex:@"\

解决RegexKitLite编译报错

在编译RegexKitLite的时候,报错如下: Undefined symbols for architecture i386: "_uregex_open", referenced from: _rkl_getCachedRegex in RegexKitLite.o "_uregex_groupCount", referenced from: _rkl_getCachedRegex in RegexKitLite.o "_uregex_setText

OC - 正则表达式 - RegexKitLite

正则表达式使用步骤: 1. 创建正则表达式对象, 设置约束条件; 1 NSString *pattern = @"\\d{1,3}"; 2 NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil]; 2. 测试字符串 1 // 2. 测试字符串 2 NSArray *results = [regex matchesInString:user