) Use and comparison of the three regular expressions in iOS, ios Regular Expressions

Source: Internet
Author: User

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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.