Regular expressions are often used to match keywords, and the basic syntax is described below.
"Basic Syntax"
① brackets indicate that one can be satisfied, for example [ABC], this position can be any of a, B, C.
② in brackets, you can pass-a range of connections, such as a-Z; multiple ranges do not require any delimiters, for example [A-za-z]
③ indicates that the number of repetitions with {x}, such as [A-z]{2}, is 2 consecutive times, and the range of repetitions can be {x, y}.
④\\d represents a number.
⑤ Regular expressions default to greedy matches, such as [a-z]{2,4}, if a string like Abcde2ab is present, ABCD satisfies a maximum length of 4, so it is a string, E is the second, and AB is the third.
The ⑥ wildcard is. (dot), which represents any character that is unexpected except for a newline character.
⑦? represents 0 or one of the preceding characters, + represents at least one, * represents 0 or more.
For example, zo*,* represents o can be 0 or more o, which means z, zoo.
⑧ with what begins with ^, with what end with $.
⑨OC special characters in a string are escaped with \.
For example [] is a special expression in the regular, \[is the ordinary ' [' character, and OC has a special meaning, need to be re-escaped, so with \\[expression ' ['.
⑩ indicates that the range of Chinese is \\u4e00-\\u9fa5.
Multiple matching criteria are used in parallel |.
When matching, be sure to pay attention to the problem of greedy matching, or you may get an error.
The following is an introduction to the regular expression object that comes with OC.
"The use of Nsregularexpression"
The object is created first, and then the Nstextcheckingresult array is obtained through the matching pattern, from which the object can be retrieved to the range of the matched characters.
The following code shows the scope of finding main and if from a string.
NSString *code = @ "MAINJIAOIFIFSIIFNELSETIFAJOMAINISKKL"; NSString *pattern1 = @ "Main|if"; Nsregularexpression *regex = [[Nsregularexpression alloc] initwithpattern:pattern1 options:0 Error:nil]; Nsarray *results = [Regex matchesinstring:code options:0 range:nsmakerange (0, Code.length)]; For (Nstextcheckingresult *result in results) { NSLog (@ "%@", Nsstringfromrange (Result.range)); }
It is troublesome to use the object of the system to match, the following is a powerful third-party library Regexkitlite.
"Integrated Regexkitlite"
① first download regexkitlite from GitHub.
② import Files REGEXKITLITE.M and. h into the project.
③ because the library is older and does not support arc, the REGEXKITLITE.M should be added to the compilation tag-fno-objc-arc for partial arc suppression.
④ add Dynamic Library Libicucore.dylib.
Note that both ③ and ④ are set in build phases, as shown in:
Through the above steps to complete the integration, the following describes the common methods of the library.
The library is a classification of nsstring, so strings can call methods directly, and common methods have matching and segmentation.
① match: Pass in the pattern of matching patterns, you can get the matching string and range.
[Str enumeratestringsmatchedbyregex:pattern usingblock:^ (Nsinteger capturecount, NSString *const __unsafe_unretained *capturedstrings, const nsrange *capturedranges, volatile BOOL *const stop) { Nsrange range = *capturedranges; NSLog (@ "%@%@", *capturedstrings,nsstringfromrange (range)); }];
② Segmentation: Sometimes there is a need to get all the matches to the string outside of the content, that is, the use of regular content to split strings, you can get the string and range of matching strings.
[Str enumeratestringsseparatedbyregex:pattern usingblock:^ (Nsinteger capturecount, NSString *const __unsafe_ unretained *capturedstrings, const nsrange *capturedranges, volatile BOOL *const stop) { Nsrange range = *capturedrang es; NSLog (@ "%@%@", *capturedstrings,nsstringfromrange (range)); }];
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(110) The basic use of regular expressions and the use of Regexkitlite