Use and comparison of the three regular expressions in iOS, ios Regular Expressions
1. match with NSPredicate (predicate) for example match valid mailbox: NSString * email = @ "nijino_saki@163.com"; NSString * regex = @ "[A-Z0-9a-z. _ % +-] + @ [A-Za-z0-9. -] + \\. [A-Za-z] {2, 4} "; NSPredicate * predicate = [NSPredicate predicateWithFormat: @" self matches % @ ", regex]; BOOL isValid = [predicate evaluatewithob: email]; predicate matching is flexible, but requires knowledge about the predicate. 2. use rangeOfString: option: to directly find NSString * searchText = @ "// Do any additional setup after loading the view, typically from a nib. "; nsange range = [searchText rangeOfString :@"(?: [^,]) * \. "Options: NSRegularExpressionSearch]; if (range. location! = NSNotFound) {NSLog (@ "% @", [searchText substringWithRange: range]);} The NSRegularExpressionSearch parameter set in options indicates regular expression matching, returns the position of the first matching result. 3. use the regular expression class NSString * searchText = @ "// Do any additional setup after loading the view, typically from a nib. "; NSError * error = NULL; NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern :@"(?: [^,]) * \. "Options: NSRegularExpressionCaseInsensitive error: & error]; NSTextCheckingResult * result = [regex firstMatchInString: searchText options: 0 range: NSMakeRange (0, [searchText length])]; if (result) {NSLog (@ "% @ \ n", [searchText substringWithRange: result. range]);} Multiple matching results are returned using the system's regular expression class (NSRegularExpression. Conclusion: For the first matching method, you need to learn NSPredicate Writing Method and Apple technical documents. If you only care about the first matching result, the second matching method is concise. If you need to match multiple results, matching Multiple times at the same time, the third method is more efficient.