当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS中正则表达式的运用示例代码

iOS中正则表达式的运用示例代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

怎么做蛋包饭,nifengsoft,爽口乐多少钱

前言

有时我们需要在一大段长文本中过滤出我们需要的字段,或者检验该文本是否符合要求(该文本是否是邮箱,链接,电话号码或身份证),这时候就需要用到正则表达式了,ios中也加入了相关的类来支持正则表达式的使用。本文详细介绍了关于ios正则表达式运用的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

一、nsregularexpression

1. 正则表达式的创建

+ (nullable nsregularexpression *)regularexpressionwithpattern:(nsstring *)pattern options:(nsregularexpressionoptions)options error:(nserror **)error;

- (nullable instancetype)initwithpattern:(nsstring *)pattern options:(nsregularexpressionoptions)options error:(nserror **)error

该类中的属性

  • pattern 返回正则表达式模式
  • options 返回创建正则表达式选项时使用的选项
  • numberofcapturegroups 返回正则表达式模式

options 定义的枚举类型如下:

 typedef ns_options(nsuinteger, nsregularexpressionoptions) {
 nsregularexpressioncaseinsensitive  = 1 << 0, //不区分大小写的
 nsregularexpressionallowcommentsandwhitespace = 1 << 1, //忽略空格和# -
 nsregularexpressionignoremetacharacters = 1 << 2, //整体化
 nsregularexpressiondotmatcheslineseparators = 1 << 3, //匹配任何字符,包括行分隔符
 nsregularexpressionanchorsmatchlines  = 1 << 4, //允许^和$在匹配的开始和结束行
 nsregularexpressionuseunixlineseparators = 1 << 5, //(查找范围为整个无效)
 nsregularexpressionuseunicodewordboundaries = 1 << 6 //(查找范围为整个无效)
 };

2. 搜索字符串

//枚举允许block处理每个正则表达式匹配的字符串
- (void)enumeratematchesinstring:(nsstring *)string options:(nsmatchingoptions)options range:(nsrange)range usingblock:(void (ns_noescape ^)(nstextcheckingresult * _nullable result, nsmatchingflags flags, bool *stop))block;

//返回一个数组,包含字符串中正则表达式的所有匹配项
- (nsarray<nstextcheckingresult *> *)matchesinstring:(nsstring *)string options:(nsmatchingoptions)options range:(nsrange)range;

//返回字符串指定范围内匹配数
- (nsuinteger)numberofmatchesinstring:(nsstring *)string options:(nsmatchingoptions)options range:(nsrange)range;

//返回字符串指定范围内第一个匹配项。
- (nullable nstextcheckingresult *)firstmatchinstring:(nsstring *)string options:(nsmatchingoptions)options range:(nsrange)range;

//返回字符串指定范围内第一个匹配的范围
- (nsrange)rangeoffirstmatchinstring:(nsstring *)string options:(nsmatchingoptions)options range:(nsrange)range;

nsmatchingoptions的定义如下:

typedef ns_options(nsuinteger, nsmatchingoptions) {
 nsmatchingreportprogress   = 1 << 0,  /* 在长时间运行的匹配操作中定期调用block */
 nsmatchingreportcompletion  = 1 << 1,  /* 完成任何匹配后,调用block一次*/
 nsmatchinganchored    = 1 << 2,  /*指定匹配仅限于搜索范围开始时的匹配 */
 nsmatchingwithtransparentbounds = 1 << 3,  /* 定匹配可以检查超出搜索范围的范围的字符串的部分,以用于诸如字边界检测,前瞻等。如果搜索范围包含整个字符串,该常量将不起作用 */
 nsmatchingwithoutanchoringbounds = 1 << 4  /* 指定^并且$不会自动匹配搜索范围的开始和结束,但仍将与整个字符串的开头和结尾相匹配。如果搜索范围包含整个字符串,则该常量不起作用 */
};

3.替换字符串

//返回与模板字符串替换的匹配正则表达式的新字符串
- (nsstring *)stringbyreplacingmatchesinstring:(nsstring *)string options:(nsmatchingoptions)options range:(nsrange)range withtemplate:(nsstring *)templ;

//返回替换的个数
- (nsuinteger)replacematchesinstring:(nsmutablestring *)string options:(nsmatchingoptions)options range:(nsrange)range withtemplate:(nsstring *)templ;

//自定义替换功能
- (nsstring *)replacementstringforresult:(nstextcheckingresult *)result instring:(nsstring *)string offset:(nsinteger)offset template:(nsstring *)templ;

//通过根据需要添加反斜杠转义来返回模板字符串,以保护符合模式元字符的任何字符
+ (nsstring *)escapedtemplateforstring:(nsstring *)string;

使用示例

 nsstring *str = @"aabbcccdeaargdo14141214aaghfh56821d3gad4";
 nsregularexpression *expression = [nsregularexpression regularexpressionwithpattern:@"aa" options:nsregularexpressioncaseinsensitive error:null];
 if (expression != nil) {
  //匹配到的第一组
  nstextcheckingresult *firstmatch = [expression firstmatchinstring:str options:nsmatchingreportprogress range:nsmakerange(0, str.length)];
  nsrange range = [firstmatch rangeatindex:0];
  nsstring *result = [str substringwithrange:range];
  nslog(@"匹配到的第一组:%@",result);
  //匹配到的个数
  nsinteger number = [expression numberofmatchesinstring:str options:nsmatchingreportprogress range:nsmakerange(0, str.length)];
  nslog(@"匹配到的个数%ld",number);
  //配到到的所有数据
  nsarray *allmatch = [expression matchesinstring:str options:nsmatchingreportprogress range:nsmakerange(0, str.length)];
  for (int i = 0; i < allmatch.count; i ++) {
   nstextcheckingresult *matchitem = allmatch[i];
   nsrange range = [matchitem rangeatindex:0];
   nsstring *result = [str substringwithrange:range];
   nslog(@"匹配到的数据:%@",result);
  }
  //匹配到第一组的位置
  nsrange firstrange = [expression rangeoffirstmatchinstring:str options:nsmatchingreportprogress range:nsmakerange(0, str.length)];
  nslog(@"匹配到第一组的位置:开始位置%lu--长度%lu",(unsigned long)firstrange.location,(unsigned long)firstrange.length);
  
  //替换字符串
  nsstring *resultstr = [expression stringbyreplacingmatchesinstring:str options:nsmatchingreportprogress range:nsmakerange(0, str.length) withtemplate:@"bbbb"];
  nslog(@"替换后的字符串:%@",resultstr);
  
  nsinteger resultnum = [expression replacematchesinstring:[str mutablecopy] options:nsmatchingreportprogress range:nsmakerange(0, str.length) withtemplate:@"bbbb"];
  nslog(@"替换的个数;%ld",(long)resultnum);
 }

打印log:
2017-08-13 23:28:53.898 nsregularexpressiondemo[82046:8220904] 匹配到的第一组:aa
nsregularexpressiondemo[82046:8220904] 匹配到的个数3
nsregularexpressiondemo[82046:8220904] 匹配到的数据:aa
nsregularexpressiondemo[82046:8220904] 匹配到的数据:aa
nsregularexpressiondemo[82046:8220904] 匹配到的数据:aa
nsregularexpressiondemo[82046:8220904] 匹配到第一组的位置:开始位置0--长度2
nsregularexpressiondemo[82046:8220904] 替换后的字符串:bbbbbbcccdebbbbrgdo14141214bbbbghfh56821d3gad4
nsregularexpressiondemo[82046:8220904] 替换的个数;3

二、字符串

//nsstringcompareoptions --> nsregularexpressionsearch
- (nsrange)rangeofstring:(nsstring *)searchstring options:(nsstringcompareoptions)mask;
- (nsrange)rangeofstring:(nsstring *)searchstring options:(nsstringcompareoptions)mask range:(nsrange)rangeofreceivertosearch;
- (nsrange)rangeofstring:(nsstring *)searchstring options:(nsstringcompareoptions)mask range:(nsrange)rangeofreceivertosearch locale:(nullable nslocale *)locale

从上面的api可以看出,只能匹配到第一组

使用示例

 nsstring *str = @"aabbcccdeaargdo14141214aaghfh56821d3gad4";
 nsrange strmatchstr = [str rangeofstring:@"aa" options:nsregularexpressionsearch];
 nslog(@"匹配到字符串的位置:开始位置%lu--长度%lu",(unsigned long)strmatchstr.location,(unsigned long)strmatchstr.length)
打印log:
nsregularexpressiondemo[82080:8224265] 匹配到字符串的位置:开始位置0--长度2

三、谓词

使用示例

 nsstring *str2 = @"aabbcc";
 nspredicate *predicate = [nspredicate predicatewithformat:@"self matches %@",@"^aa(.)*cc$"];
 bool ismatch = [predicate evaluatewithobject:str2];
 nslog(@"匹配的结果:%d",ismatch);
打印log:
nsregularexpressiondemo[82679:8253019] 匹配的结果:1

四、正则表达式

可以参考这篇文章:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网