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

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

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官网上的,怕同鞋偷情不看)

Character Description
\a Match a BELL, \u0007
\A Match 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.
\B Match if the current position is not a word boundary.
\cx Match a Control-x character.
\d Match any character with the Unicode General Category of Nd (Number, Decimal Digit).
\D Match any character that is not a decimal digit.
\e Match an ESCAPE, \u001B.
\E Terminates a \Q…\E quoted sequence.
\f Match a FORM FEED, \u000C.
\G Match if the current position is at the end of the previous match.
\n Match 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.
\Q Quotes all following characters until \E.
\r Match a CARRIAGE RETURN, \u000D.
\s Match a white space character. White space is defined as [\t\n\f\r\p{Z}].
\S Match a non-white space character.
\t Match a HORIZONTAL TABULATION, \u0009.
\uhhhh Match the character with the hex value hhhh.
\Uhhhhhhhh Match the character with the hex value hhhhhhhh. Exactly eight hex digits must be provided, even though the largest Unicode code point is \U0010ffff.
\w Match a word character. Word characters are [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}].
\W Match a non-word character.
\x{h…} Match the character with hex value hhhh. From one to six hex digits may be supplied.
\xhh Match the character with two digit hex value hh.
\X Match a Grapheme Cluster.
\Z Match if the current position is at the end of input, but before the final line terminator, if one exists.
\z Match if the current position is at the end of input.
\n Back 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 * ? + [ ( ) { } ^ $ | \ . /
OperatorsOperator Description
| 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

屏幕快照 2010-04-10 上午11.00.34

屏幕快照 2010-04-10 上午11.00.48

屏幕快照 2010-04-10 上午11.01.11

屏幕快照 2010-04-10 上午11.01.20屏幕快照 2010-04-10 上午11.01.28

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

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

屏幕快照 2010-04-10 上午11.17.04

相关文章
|
5月前
|
Python
Python 内置正则表达式库re的使用
正则表达式是记录文本规则的代码,用于查找和处理符合特定规则的字符串。在Python中,常通过原生字符串`r&#39;string&#39;`表示。使用`re.compile()`创建正则对象,便于多次使用。匹配字符串有`match()`(从开头匹配)、`search()`(搜索首个匹配)和`findall()`(找所有匹配)。替换字符串用`sub()`,分割字符串则用`split()`。
62 3
|
5月前
|
Python Windows
【Python进阶必备】一文掌握re库:实战正则表达式
【Python进阶必备】一文掌握re库:实战正则表达式
92 0
|
3月前
|
存储 C++ 容器
C++一分钟之-正则表达式库(regex)
【7月更文挑战第7天】C++从C++11开始支持正则表达式,通过`&lt;regex&gt;`库提供功能。本文涵盖基本概念如`std::regex`、`std::smatch`,以及`regex_search`和`regex_match`的使用。常见问题包括大小写敏感性、特殊字符转义、贪婪与非贪婪匹配和捕获组。提供的代码示例展示了如何进行匹配、不区分大小写的匹配、特殊字符匹配、贪婪与非贪婪匹配和捕获组的使用。理解并练习正则表达式能提升文本处理效率。
45 0
|
4月前
|
Python
python re 正则表达式库的使用
python re 正则表达式库的使用
35 0
|
5月前
|
iOS开发
iOS使用.framework类型的静态库
iOS使用.framework类型的静态库
39 1
|
5月前
|
开发工具 iOS开发
iOS制作.framework静态库
iOS制作.framework静态库
44 1
|
5月前
|
iOS开发 Perl
iOS使用.a类型的静态库
iOS使用.a类型的静态库
39 1
|
5月前
|
开发工具 iOS开发
iOS制作.a类型的静态库
iOS制作.a类型的静态库
35 1
|
5月前
|
开发工具 iOS开发 Perl
iOS使用SDK静态库
iOS使用SDK静态库
51 0
|
5月前
|
移动开发 网络协议 小程序
基于开源IM即时通讯框架MobileIMSDK:RainbowChat-iOS端v9.0版已发布
MobileIMSDK 是一套专门为移动端开发的开源IM即时通讯框架,超轻量级、高度提炼,一套API优雅支持 UDP 、TCP 、WebSocket 三种协议,支持 iOS、Android、H5、标准Java、小程序、Uniapp,服务端基于Netty编写。
121 0