[IOS]开源库RegexKitLite正则表达式的使用

1.去RegexKitLite下载类库,解压出来会有一个例子包及2个文件,其实用到的就这2个文件,添加到工程中。

2.工程中添加libicucore.dylib frameworks。

友情提醒:一般人导入RegexKitLite编译报错,正是因为没有导入这个类库,加上这个就OK了

3.现在所有的nsstring对象就可以调用RegexKitLite中的方法了。

NSString *email = @”kkk@aaa.com”;

[email isMatchedByRegex:@"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b”];

返回YES,证明是email格式,需要注意的是RegexKitLite用到的正则表达式和wiki上的略有区别。

searchString = @”http://www.example.com:8080/index.html”;

regexString  = @”\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?”;

NSInteger portInteger = [[searchString stringByMatching:regexString capture:1L] integerValue];

NSLog(@”portInteger: ‘%ld’”, (long)portInteger);

// 2008-10-15 08:52:52.500 host_port[8021:807] portInteger: ‘8080′

取string中http的例子。

下面给出常用的一些正则表达式(其实就是RegexKitLite官网上的,怕同鞋偷情不看)

CharacterDescription

\aMatch a BELL, \u0007

\AMatch at the beginning of the input. Differs from ^ in that \A will not match after a new-line within the input.

\b, outside of a [Set]Match if the current position is a word boundary. Boundaries occur at the transitions between word \w and non-word \W characters, with combining marks
ignored.

See also: RKLUnicodeWordBoundaries

\b, within a [Set]Match a BACKSPACE, \u0008.

\BMatch if the current position is not a word boundary.

\cxMatch a Control-x character.

\dMatch any character with the Unicode General Category of Nd (Number, Decimal Digit).

\DMatch any character that is not a decimal digit.

\eMatch an ESCAPE, \u001B.

\ETerminates a \Q…\E quoted sequence.

\fMatch a FORM FEED, \u000C.

\GMatch if the current position is at the end of the previous match.

\nMatch a LINE FEED, \u000A.

\N{Unicode Character Name}Match the named Unicode Character.

\p{Unicode Property Name}Match any character with the specified Unicode Property.

\P{Unicode Property Name}Match any character not having the specified Unicode Property.

\QQuotes all following characters until \E.

\rMatch a CARRIAGE RETURN, \u000D.

\sMatch a white space character. White space is defined as [\t\n\f\r\p{Z}].

\SMatch a non-white space character.

\tMatch a HORIZONTAL TABULATION, \u0009.

\uhhhhMatch the character with the hex value hhhh.

\UhhhhhhhhMatch the character with the hex value hhhhhhhh. Exactly eight hex digits must be provided, even though the largest Unicode code point is \U0010ffff.

\wMatch a word character. Word characters are [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}].

\WMatch a non-word character.

\x{h…}Match the character with hex value hhhh. From one to six hex digits may be supplied.

\xhhMatch the character with two digit hex value hh.

\XMatch a Grapheme Cluster.

\ZMatch if the current position is at the end of input, but before the final line terminator, if one exists.

\zMatch if the current position is at the end of input.

\nBack Reference. Match whatever the nth capturing group matched. n must be a number ≥ 1 and ≤ total number of capture groups in the pattern.Note:Octal
escapes, such as \012, are not supported.

[pattern]Match any one character from the set. See ICU Regular Expression Character Classes for a full description of what may appear in the pattern.

.Match any character.

^Match at the beginning of a line.

$Match at the end of a line.

\Quotes the following character. Characters that must be quoted to be treated as literals are * ? + [ ( ) { } ^ $ | \ . /

OperatorsOperatorDescription

|Alternation. A|B matches either A or B.

*Match zero or more times. Match as many times as possible.

+Match one or more times. Match as many times as possible.

?Match zero or one times. Prefer one.

{n}Match exactly n times.

{n,}Match at least n times. Match as many times as possible.

{n,m}Match between n and m times. Match as many times as possible, but not more than m.

*?Match zero or more times. Match as few times as possible.

+?Match one or more times. Match as few times as possible.

??Match zero or one times. Prefer zero.

{n}?Match exactly n times.

{n,}?Match at least n times, but no more than required for an overall pattern match.

{n,m}?Match between n and m times. Match as few times as possible, but not less than n.

*+Match zero or more times. Match as many times as possible when first encountered, do not retry with fewer even if overall match fails. Possessive match.

++Match one or more times. Possessive match.

?+Match zero or one times. Possessive match.

{n}+Match exactly n times. Possessive match.

{n,}+Match at least n times. Possessive match.

{n,m}+Match between n and m times. Possessive match.

(…)Capturing parentheses. Range of input that matched the parenthesized subexpression is available after the match.

(?:…)Non-capturing parentheses. Groups the included pattern, but does not provide capturing of matching text. Somewhat more efficient than capturing parentheses.

(?>…)Atomic-match parentheses. First match of the parenthesized subexpression is the only one tried; if it does not lead to an overall pattern match, back up the search for
a match to a position before the (?> .

(?#…)Free-format comment (?#comment).

(?=…)Look-ahead assertion. True if the parenthesized pattern matches at the current input position, but does not advance the input position.

(?!…)Negative look-ahead assertion. True if the parenthesized pattern does not match at the current input position. Does not advance the input position.

(?<=…)Look-behind assertion. True if the parenthesized pattern matches text preceding the current input position, with the last character of the match being the input character
just before the current position. Does not alter the input position. The length of possible strings matched by the look-behind pattern must not be unbounded (no * or + operators).

(?<!…)Negative Look-behind assertion. True if the parenthesized pattern does not match text preceding the current input position, with the last character of the match being
the input character just before the current position. Does not alter the input position. The length of possible strings matched by the look-behind pattern must not be unbounded (no * or + operators).

(?ismwx-ismwx:…)Flag settings. Evaluate the parenthesized expression with the specified flags enabled or -disabled.

(?ismwx-ismwx)Flag settings. Change the flag settings. Changes apply to the portion of the pattern following the setting. For example, (?i) changes to a case insensitive
match.

See also: Regular Expression Options

同时需要注意的是转义字符哦~~在safari上复制会直接转换(网站蛮人性化的)

同时也提供了转换工具,safari测试支持,可能下载的时候有点慢,耐心等待,链接

时间: 2024-08-19 04:28:22

[IOS]开源库RegexKitLite正则表达式的使用的相关文章

27个提升效率的iOS开源库推荐

DZNEmptyDataSet(UI,空表格视图解算器) PDTSimpleCalendar(UI,drop-in日历组件) MagicalRecord(实施活跃记录模式的Core Data助手) Chameleon(UI,色彩框架) Alamofire(Swift 网络) TextFieldEffects (UI,自定义外观的文本区域) GPUImage(快速图片处理) iRate(获取用户评价) GameCenterManager(快速管理游戏中心) PKRevealController 2

10个有用的第三方iOS开源库

CocoaPods 地址:https://github.com/CocoaPods/CocoaPods 教程:http://www.raywenderlich.com/12139/introduction-to-cocoapods 描述:可以很方便的管理第三方库,清晰知道项目引用的库有哪些和它们的版本. CocoaAsyncSocket 地址:https://github.com/robbiehanson/CocoaAsyncSocket 简述: 处理TCP/UDP 链接的开源库. Appira

史上最全的iOS开源项目分类汇总

楼主转载的,并未亲自测试 Category/Util  sstoolkit 一套Category类型的库,附带很多自定义控件 功能不错-        BFKit 又一套Category类型的 Kit,还有几个工具类        APUtils 又一套Category类型的 Kit        QSKit 又一套Category类型的 Kit        iOS-Categories 又一套Category类型的 Kit        BlocksKit 将Block风格带入UIKit和F

iOS开发:GitHub上的40个iOS开源项目

 开发:GitHub上的40个iOS开源项目-"> 1. AFNetworking 在众多iOS开源项目中,AFNetworking可以称得上是最受开发者欢迎的库项目.AFNetworking是一个轻量级的iOS.Mac OS X网络通信类库,现在是GitHub上第三大Objective-C库.它建立在NSURLConnection.NSOperation等类库的基础上,让很多网络通信功能的实现变得十分简单,因此,许多iOS应用开发都会使用到它. 支持HTTP请求和基于REST的网络服务(

我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍

GitHub Android Libraries Top 100 简介 本文转载于:https://github.com/Freelander/Android_Data/blob/master/Android-Librarys-Top-100.md 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不相关的项目, 所以排名并

C++开源库,欢迎补充。

C++在"商业应用"方面,曾经是天下第一的开发语言,但这一桂冠已经被java抢走多年.因为当今商业应用程序类型,已经从桌面应用迅速转移成 Web应 用.当Java横行天下之后,MS又突然发力,搞出C#语言,有大片的曾经的C++程序员,以为C++要就此沉沦,未料,这三年来,C++的生命力突然被 严重地增强了.主力原因就是开源的软件.基础软件(比如并发原生支持,比如Android必定要推出原生的SDK).各种跨平台应用的出现.   开源C++库必须具有以下特点:必须是成熟的产品.跨平台的产

iOS 开源图形库 Core Plot 使用教程

本文讲的是iOS 开源图形库 Core Plot 使用教程, 注意 :本篇教程已被 Attila Hegedüs 更新,可适用于 iOS 9 和 Swift 2.2.原始教程出自教程组成员 Steve Baranski. 如果你曾经想在自己的 app 中引入图表或图形,那么你应该已经考虑过下面两种选项: 自己写. 通过使用 Core Graphics 或者 Quartz 这样的框架编写全部的绘制代码.然而,这显然要花费大量的功夫. 买一个! 购买一个像 ShinobiControls 这样的商业

站在巨人的肩膀上,C++开源库大全

程序员要站在巨人的肩膀上,C++拥有丰富的开源库,这里包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等.   标准库 C++ Standard Library:是一系列类和函数的集合,使用核心语言编写,也是C++ISO自身标准的一部分. Standard Template Library:标准模板库 C POSIX library : POSIX系统的C标准库规范 ISO C++ Standards Committee :C++标准委员会 框架 C++通用框架

使用开源库 MBProgressHUD 等待指示器

source https://github.com/jdg/MBProgressHUD MBProgressHUD is an iOS drop-in class that displays a translucent HUD with an indicator and/or labels while work is being done in a background thread. The HUD is meant as a replacement for the undocumented,