Use of OC Regular Expressions and OC Regular Expressions
Generally, the use of regular expressions in OC is two steps.
1. Create a regular expression object
2. Use a regular expression to test the corresponding string
For example
NSString * checkString = @ "a34sd231"; // 1. create a regular expression. [0-9]: indicates the NSString * pattern = @ "[0-9]" set of characters from '0' to '9'. // 1.1 set the regular expression to the OC rule NSRegularExpression * regular = [[NSRegularExpression alloc] initWithPattern: pattern options: NSRegularExpressionCaseInsensitive error: nil]; // 2. use the rule test string to obtain the matching result NSArray * results = [regular matchesInString: checkString options: 0 range: NSMakeRange (0, checkString. length)]; NSLog (@ "% ld", results. count );
Obtain the print result. The number of characters analyzed from checkString is 5.
Therefore, we can draw a conclusion that the function of a regular expression is to mix multiple strings into one expression.
Here we can understand the meaning of the regular expression. Next we will introduce some common Regular Expression characters.
/* []: Find an internal character [a-zA-Z0-9] ==> stands for character or number \ d ==> stands for number {2 }: two {2, 4}: two to four //? + * ^ $ .? : Represents 0 or 1 +: represents at least 1 *: represents 0 or more ^: represents... $: indicates that... end.: any character except line breaks */
// Represents a number character
NSString * pattern = @ "\ d ";
// Two to five consecutive numbers
NSString * pattern = @ "\ d {2, 5 }";
// The regular expression of the qq account
NSString * pattern = @ "^ [1-9] \ d {4, 10 }";
// Regular Expression in phone number format
NSString * pattern = @ "^ (13 [0-9]) | (15 [3-5]) | (18 [07-9]) \ d {8} $"
// Regular Expression of the mailbox
NSString * pattern = @ "^. * @... + \. [a-zA-Z] {2, 4} $"
In this way, you can set a regular expression based on the matching rules to determine whether the string is of the correct type.
If there is a long string, you must match the expression, url, @ (CALL), or # topic #.
Multiple regular expressions are required for determination.
// The string NSString * str = @ "# today's news # [Laugh] http://asd.fdfs. 2ee/aas/1e @ sdf [test] # Are you sure # @ rain Li 23: @ Zhang San [Snoop] m123m "; // expression Regular Expression // \ u4e00-\ u9fa5 stands for the unicode Character NSString * emopattern = @ "\ [[a-zA-Z \ u4e00-\ u9fa5] + \] "; // @ regular expression NSString * atpattern = @ "@ [0-9a-zA-Z \ u4e00-\ u9fa5] + ";//#... # Regular Expression NSString * toppattern = @ "# [0-9a-zA-Z \ u4e00-\ u9fa5] + #"; // url Regular Expression NSString * urlpattern = @ "\ B ([\ w-] + ://? | Www [.]) [^ \ s () <>] + (?: \ ([\ W \ d] + \) | ([^ [: punct:] \ s] | /)))"; // set the general Regular Expression NSString * pattern = [NSString stringWithFormat: @ "% @ | % @ |%@|%@", emopattern, atpattern, toppattern, urlpattern]; // set the OC rule NSRegularExpression * regular = [[NSRegularExpression alloc] initWithPattern: pattern options: NSRegularExpressionCaseInsensitive error: nil] According to the regular expression; // obtain the matching result NSArray * results = [regular matchesInString: str options: 0 range: NSMakeRange (0, str. length)]; // NSLog (@ "% @", results); // traverses the result for (NSTextCheckingResult * result in results) {NSLog (@ "% @", NSStringFromRange (result. range), [str substringWithRange: result. range]);}
Obtain results
With the above demonstration, you can roughly write the regular expression used by the system method.