[iOS]利用系统NSRegularExpression使用正则表达式
// Created by 李东旭 on 16/1/22. // Copyright © 2016年 李东旭. All rights reserved. // #import <UIKit/UIKit.h> #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 使用系统的类使用正则表达式(当然你也可以使用第三方RegexKitLite这个类) // 1. 获取字符串 NSString *user = @"abc123def456kasdf999"; // 2. 定义正则表达式(规则) // 如果我们的规则是“abc” 然后字符串是"abcdsafasdfabcdsaf" 那么找到的就是2个,说白了,正则表达式就是找东西用的 // 这里假设查找数字 (后面字符串就是条件,具体要查什么,可以上网百度或者看http://www.cnblogs.com/lidongxu/p/5153707.html) NSString *patter = @"[0-9]"; // 3. 创建NSRegularExpression(调用正则表达式)(在iOS4开始) NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:patter options:0 error:nil]; // 4. 测试字符串(最后设置测试哪部分,这里设置字符串user全部) NSArray *resultArr = [regular matchesInString:user options:0 range:NSMakeRange(0, user.length)]; // 5.遍历结果 for (NSTextCheckingResult *result in resultArr) { // 打印匹配的字符串在user的位置 NSLog(@"%@", NSStringFromRange(result.range)); } } @end

[iOS]利用系统NSRegularExpression使用正则表达式的更多相关文章
- UIView封装动画--iOS利用系统提供方法来做转场动画
UIView封装动画--iOS利用系统提供方法来做转场动画 UIViewAnimationOptions option; if (isNext) { option=UIViewAnimationOpt ...
- UIView封装动画--iOS利用系统提供方法来做关键帧动画
iOS利用系统提供方法来做关键帧动画 ios7以后才有用. /*关键帧动画 options:UIViewKeyframeAnimationOptions类型 */ [UIView animateKey ...
- UIView封装动画--iOS 利用系统提供方法来做弹性运动
iOS 利用系统提供方法来做弹性运动 /*创建弹性动画 damping:阻尼,范围0-1,阻尼越接近于0,弹性效果越明显 velocity:弹性复位的速度 */ [UIView animateWith ...
- iOS开发之详解正则表达式
本文由Charles翻自raywenderlich原文:NSRegularExpression Tutorial: Getting Started更新提示:本教程被James Frost更新到了iOS ...
- [iOS] 利用 NSAttributedString 进行富文本处理
/iOS /[iOS] 利用 NSAttributedString 进行富文本处理 2016年4月4日 刘小龙 iOS 许多时候我们需要以各种灵活的形式展现文本信息,即富文本.普通的 text 属性显 ...
- Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面
Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面 关于欢迎界面 很多App第一次启动都会有一个欢迎界面,欢迎界面往往决定这用户对App的第一映像,所以欢 ...
- iOS 捕获系统外异常
iOS 捕获系统外异常 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太 ...
- (八十一)利用系统自带App来实现导航
利用系统的地图App进行导航,只需要传入起点和终点.启动参数,调用MKMapItem的类方法openMapWithItems:launchOptions:来实现定位,调用此方法后会打开系统的地图App ...
- 利用系统函数模拟实现nginx 系统脚本启动的特殊颜色专业效果
利用系统函数模拟实现nginx 系统脚本启动的特殊颜色专业效果/etc/init.d/nginxd {start/stop/restart/reload}利用if语句实现: ============= ...
随机推荐
- [转载]EF Code First 学习笔记:约定配置
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...
- 第三次作业,github的基本操作
chengjiangtao@pc MINGW32 ~$ git config --global user.name "chengjiangtao" chengjiangtao@pc ...
- zabbix语言设置
1.怎样支持中文: https://www.zabbix.org/wiki/How_to/install_locale 官方解决方法 实际操作中,进入/var/lib/locales/supporte ...
- 【Search a 2D Matrix】cpp
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- python-面向对象(股票对象举例)
股票对象实例 class Stock(object): def __init__(self,stockCode ,stockName,averagePrice_yesterday,averagePri ...
- 设计模式之状态模式(State)
状态模式原理:随着状态的变化,对象的行为也发生变化 代码如下: #include <iostream> #include <string> #include <list& ...
- Lessons learned from manually classifying CIFAR-10
Lessons learned from manually classifying CIFAR-10 Apr 27, 2011 CIFAR-10 Note, this post is from 201 ...
- git在terminal中自动补全
1. curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~ ...
- PE文件结构学习
PE:Portable Executable File Format(可移植的执行体).Windows平台主流可执行文件格式..exe与.dll文件都是PE格式.32位的叫做PE32,64位的叫做PE ...
- Shell日期时间和时间戳的转换
Gitlab的备份文件是以时间戳显示的,类似:1438624820_gitlab_backup.tar 为了更易于阅读,想把文件名转换成日期格式:2015-08-04_gitlab_backup.ta ...