溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

[IOS]開(kāi)源庫(kù)RegexKitLite正則表達(dá)式的使用

發(fā)布時(shí)間:2020-04-06 07:21:36 來(lái)源:網(wǎng)絡(luò) 閱讀:592 作者:蓬萊仙羽 欄目:移動(dòng)開(kāi)發(fā)

1.去RegexKitLite下載類(lèi)庫(kù),解壓出來(lái)會(huì)有一個(gè)例子包及2個(gè)文件,其實(shí)用到的就這2個(gè)文件,添加到工程中。

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

友情提醒:一般人導(dǎo)入RegexKitLite編譯報(bào)錯(cuò),正是因?yàn)闆](méi)有導(dǎo)入這個(gè)類(lèi)庫(kù),加上這個(gè)就OK了

3.現(xiàn)在所有的nsstring對(duì)象就可以調(diào)用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用到的正則表達(dá)式和wiki上的略有區(qū)別。

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的例子。

下面給出常用的一些正則表達(dá)式(其實(shí)就是RegexKitLite官網(wǎng)上的,怕同鞋偷情不看)

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

[IOS]開(kāi)源庫(kù)RegexKitLite正則表達(dá)式的使用

[IOS]開(kāi)源庫(kù)RegexKitLite正則表達(dá)式的使用

[IOS]開(kāi)源庫(kù)RegexKitLite正則表達(dá)式的使用

[IOS]開(kāi)源庫(kù)RegexKitLite正則表達(dá)式的使用[IOS]開(kāi)源庫(kù)RegexKitLite正則表達(dá)式的使用

同時(shí)需要注意的是轉(zhuǎn)義字符哦~~在safari上復(fù)制會(huì)直接轉(zhuǎn)換(網(wǎng)站蠻人性化的)

同時(shí)也提供了轉(zhuǎn)換工具,safari測(cè)試支持,可能下載的時(shí)候有點(diǎn)慢,耐心等待,鏈接

[IOS]開(kāi)源庫(kù)RegexKitLite正則表達(dá)式的使用

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI