初始化一个   NSRegularExpression 对象 注:_str是要匹配的字符串

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?" options:NSRegularExpressionCaseInsensitive error:nil];

获得所有匹配了表达式的字符串。

NSArray *array =    nil;

array = [regex matchesInString:_str options:0 range:NSMakeRange(0, [_str length])];
    NSString *str1 = nil;
    for (NSTextCheckingResult* b in array)

{

str1 是每个和表达式匹配好的字符串。

str1 = [_str substringWithRange:b.range];
        NSLog(@" str 1 is %@",str1);

}

获得匹配的字符串的个数

NSUInteger numberOfMatches = [regex numberOfMatchesInString:_str options:0 range:NSMakeRange(0, [_str length])];

替换匹配的字符串  $0很重要 $0不行的话 $1依此类推 打印了看结果

NSString *modifiedString = [regex stringByReplacingMatchesInString:_str
                                                               options:0
                                                                 range:NSMakeRange(0, [_str length])
                                                          withTemplate:@"<a href=\"$0\">$0</a>"];
    NSLog(@"######## the modified string is %@",modifiedString);

ios 使用NSRegularExpression解析正则表达式的更多相关文章

  1. iOS - JSON 数据解析

     iOS - JSON 数据解析 前言 NS_CLASS_AVAILABLE(10_7, 5_0) @interface NSJSONSerialization : NSObject @availab ...

  2. 2016 - 1- 23 iOS中xml解析 (!!!!!!!有坑要解决!!!!!!)

    一: iOS中xml解析的几种方式简介 1.官方原生 NSXMLParser :SAX方式解析,使用起来比较简单 2.第三方框架 libxml2 :纯C 同时支持DOM与SAX GDataXML: D ...

  3. iOS push全方位解析(二)【译文】"——生成OpenSSL证书,Provisioning Profile

    这是一篇来自raywenderlich的教程,内容翔实!结构简单透彻.讲解循序渐进.文章质量上乘!是一篇难的的博文!使用半瓶的英语水平翻译了一下: 1.[iOS push全方位解析](一) push的 ...

  4. (转)iOS Wow体验 - 第二章 - iOS用户体验解析(2)

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第二章译文精选的第二部分,其余章节将陆续放出.上一 ...

  5. iOS开发 XML解析和下拉刷新,上拉加载更多

    iOS开发 XML解析和下拉刷新,上拉加载更多 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示 ...

  6. iOS使用TFHpple解析html

    iOS 开发中解析html 网上有很多写好的解析框架 今天就来讲一下如何用框架TFHpple来解析html 使用TFHpple解析html github地址:https://github.com/to ...

  7. iOS 基础函数解析 - Foundation Functions Reference

    iOS 基础函数解析 - Foundation Functions Reference 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名- ...

  8. 【iOS系列】-iOS的多线程解析

    [iOS系列]-iOS的多线程解析 iOS的多线程实现技术: 1:GCD -- Grand Central Dispatch 是基于C语言的底层API 用Block定义任务,使用起来非常灵活便捷 提供 ...

  9. NSRegularExpression iOS自带的正则表达式

    以前做验证邮箱,电话号码的时候通常用第三方的正则表达式或者NSPredicate(点这里查看以前的文章),在后期,苹果推出了自己的正则表达式来提供给开发者调用,很方便,功能也强大. 具体可以查看官方文 ...

随机推荐

  1. 如何将excel中的一个表格内容转成xml格式的文件

    转自:http://www.cnblogs.com/sansi/archive/2012/02/06/2340471.html 感谢作者,解决了折磨我几天的问题,顿时心情开朗~ ----------- ...

  2. LeetCode: 485 Max Consecutive Ones(easy)

    题目: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...

  3. sql server 2008 删除某数据库所有表

    /* ------sqlserver 2008 删除某数据库所有表-------- */ declare @tname varchar(8000) set @tname='' select @tnam ...

  4. typescript学习笔记(一)----基础类型

    1.使用typescript前第一个操作就是全局配置typescript环境 ---------------npm install -g typescript 2.typescript(以下称为ts, ...

  5. Codeforces Round #396 (Div. 2) C

    Mahmoud wrote a message s of length n. He wants to send it as a birthday present to his friend Moaz ...

  6. C. On Number of Decompositions into Multipliers 组合数学

    http://codeforces.com/contest/397/problem/C 给出n个数字,m = a[1] * a[2] * a[3] ... * a[n] 要求把m分成n个不一样的乘积, ...

  7. 1049 - Deg-route

    http://www.ifrog.cc/acm/problem/1049 这些数学题我一般都是找规律的.. 先暴力模拟了前面的那些,然后发现(x, y) = (x, y - 1) + (x - 1, ...

  8. Sql Server 排序规则字符集的冲突问题

    可通过如下sql 进行修改: 如果整个DB都不一致: Alter database Expense_Portal collate Chinese_PRC_CI_AS 某张Table的栏位不一致: ) ...

  9. android开发学习 ------- @SuppressWarnings 注解的使用

    @SuppressWarnings 该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. @SuppressWarnings 批注允许您选择性地取消特定代码段(即,类或方法 ...

  10. Code First约定-数据注释

    通过实体框架Code First,可以使用您自己的域类表示 EF 执行查询.更改跟踪和更新函数所依赖的模型.Code First 利用称为“约定先于配置”的编程模式.这就是说,Code First 将 ...