Regular Expression matching (Tool class) and iOS matching tool class in ios

Source: Internet
Author: User
Tags email account

Regular Expression matching (Tool class) and iOS matching tool class in ios
Regular Expression

A regular expression is a logical formula for string operations. It uses predefined characters and combinations of these specific characters to form a "rule string ",

This "rule string" is used to express a filtering logic for strings. A regular expression is a tool used to describe these rules, or a regular expression is the code that records text rules.

During development, we often need to find strings that comply with certain complex rules,

For example, data verification: determines whether the user input is legal (for example, when the user is registered, the QQ number, phone number, and email address meet the requirements)

Next let's take a look at some common characters of Regular Expression matching:

I. Common wildcard characters

. Match any character except linefeed

\ W matches letters, numbers, underscores, and Chinese characters

\ S matches any blank space character (space, tab \ t, press enter \ r \ n)

\ D matching number

^ Match the start of a string

$ End of matching string

\ B matches the start or end of a word

Ii. Commonly Used assignees

\ W matches any character that is not a letter, number, underline, or Chinese Character

\ S matches any character that is not a blank character

\ D matches any non-numeric characters

\ B matching is not the start or end position of a word

[^ X] matches any character except x

[^ Aeiou] matches any character except aeiou.

Iii. Set

[Xyz] match any character in xyz

[^ Xyz] match any character not in xyz

[A-z] match any character from lowercase letters a to lowercase letters z

[^ A-z] match any character not from lowercase letters a to lowercase letters z

4. Common delimiters

* Repeated zero or more times

+ Repeat once or more times

? Zero or one repetition

{N} repeated n times

{N,} repeat n times or more times

{N, m} repeat n to m times

5. Greedy and lazy mode (Greedy mode by default; question mark directly after quantifiers, that is, non-Greedy Mode)

*? Repeat any time, but as few as possible

+? Repeat once or more times, but as few as possible

?? Repeat zero or once, but as few as possible

{N, m }? Repeat n to m times, but as few as possible

{N ,}? Repeated more than n times, but as few as possible

 

The above are the characters that need to be used for regular expression matching rules.

The following are some regular matches that I often use in development and are collected at the same time. For convenience, I encapsulated them into a tool class and provided class methods to facilitate the call and return matching results.

Some of the above matching characters may not be clear to some friends, so we don't need to talk nonsense. I should understand it after reading the code several times.

-RegularTool. h
1 // 2 // RegularTool. h 3 // regular expression match 4 // 5 // Created by Arvin on 15/12/3. 6 // Copyright©2015 Arvin. All rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 @ interface RegularTool: NSObject12 13 /*! 14 * match fixed phone number 15*16 * @ param number the fixed phone number 17*18 * @ return returns the matching result 19 */20 + (BOOL) matchTelephoneNumber :( NSString *) number; 21 22 /*! 23 * match mobile phone number 24*25 * @ param number 26*27 * return matching result 28 */29 + (BOOL) matchMobilephoneNumber :( NSString *) number; 30 31 /*! 32 * match 3-15 characters in Chinese or English (User Name) 33*34 * @ param username the string to be matched 35*36 * @ return returns the matching result 37 */38 + (BOOL) matchUsername :( NSString *) username; 39 40 /*! 41 * match a combination of 6-18 digits and letters (password) 42*43 * @ param password the string to be matched 44*45 * @ return returns the matched result 46 */47 + (BOOL) matchPassword :( NSString *) password; 48 49 /*! 50 * matching email account 51*52 * @ param email the email account 53*54 * @ return returns the matching result 55 */56 + (BOOL) matchEmail :( NSString *) email; 57 58 /*! 59 * matching ID card number 60*61 * @ param idCard ID card number 62*63 * @ return the matching result 64 */65 + (BOOL) matchUserIdCard :( NSString *) idCard; 66 67 /*! 68 * match URL string 69*70 * @ param urlStr the URL string 71*72 * @ return returns the matching result 73 */74 + (BOOL) matchillstr :( NSString *) urlStr; 75 76 /*! 77 * matching ¥: price string 78*79 * @ param priceStr contains the price string 80*81 * @ return returns the matching result 82 */83 + (BOOL) matchPriceStr :( NSString *) priceStr; 84 85 @ end
-RegularTool. m
1 // 2 // RegularTool. m 3 // regular expression match 4 // 5 // Created by Arvin on 15/12/3. 6 // Copyright©2015 Arvin. all rights reserved. 7 // 8 9 # import "RegularTool. h "10 11 @ implementation RegularTool 12 13 # pragma mark-match fixed phone number 14 + (BOOL) matchTelephoneNumber :( NSString *) number {15 NSString * pattern = @ "^ (\ d {3, 4}-) \ d {7, 8} $"; 16 return [RegularTool matchString: number withPattern: pattern]; 17} 18 19 # pragma mark-matching mobile phone number 20 + (BOOL) matchMobilephoneNumber :( NSString *) number {21 NSString * pat Tern = @ "^ (0 | 86 )? 1 ([358] [0-9] | 7 [678] | 4 [57]) \ d {8} $ "; 22 return [RegularTool matchString: number withPattern: pattern]; 23} 24 25 # pragma mark-Match 3-15 characters in Chinese or English (username) 26 + (BOOL) matchUsername :( NSString *) username {27 NSString * pattern = @ "^ [a-zA-Z one-region] {3, 15} $"; 28 return [RegularTool matchString: username withPattern: pattern]; 29} 30 31 # pragma mark-match 6-18 digits and a combination of letters (password) 32 + (BOOL) matchPassword :( NSString *) password {3 3 NSString * pattern = @ "^ (?! [0-9] + $ )(?! [A-zA-Z] + $) [a-zA-Z0-9] {6, 18} $ "; 34 return [RegularTool matchString: password withPattern: pattern]; 35} 36 37 # pragma mark-matched email account 38 + (BOOL) matchEmail :( NSString *) email {39 NSString * pattern = 40 @ "^ [a-z0-9] + ([\\. _ \-] * [a-z0-9]) * @ ([a-z0-9] + [-a-z0-9] * [a-z0-9] + \\.) {} [a-z0-9] + $ "; 41 return [RegularTool matchString: email withPattern: pattern]; 42} 43 44 # pragma mark-match ID number 45 + (BOOL) matchU SerIdCard :( NSString *) idCard {46 NSString * pattern = 47 @ "(^ [1-9] \ d {7} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} $) | (^ [1-9] \ d {5} [1-9] \ d {3} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) (\ d {4 }) | \ d {3} [Xx]) $) "; 48 return [RegularTool matchString: idCard withPattern: pattern]; 49} 50 51 # pragma mark-match URL string 52 + (BOOL) matchillstr :( NSString *) urlStr {53 NSString * pattern = @ "^ [0-9A-Za-z] {} $ "; 54 Return [RegularTool matchString: urlStr withPattern: pattern]; 55} 56 57 # pragma mark-match ¥: price string 58 + (BOOL) matchPriceStr :( NSString *) priceStr {59 NSError * error = nil; 60 NSRegularExpression * regular = [NSRegularExpression regularExpressionWithPattern: @ "¥ (\ d + (?: \. \ D + )?) "Options: 1 <0 error: & error]; 61 if (! Error) {62 NSArray <NSTextCheckingResult *> * result = [regular matchesInString: priceStr options: NSMatchingWithoutAnchoringBounds range: NSMakeRange (0, priceStr. length)]; 63 if (result) {64 for (NSTextCheckingResult * checkingRes in result) {65 if (checkingRes. range. location = NSNotFound) {66 continue; 67} 68 NSLog (@ "% @", [priceStr substringWithRange: checkingRes. range]); 69 // NSLog (@ "% @", [priceStr SubstringWithRange: [res rangeAtIndex: 1]); 70} 71} 72 return YES; 73} 74 NSLog (@ "matching failed, Error: % @", error ); 75 return NO; 76} 77 78 79 /*! 80 * Regular Expression matching 81*82 * @ param str matching string 83 * @ param pattern matching rule 84*85 * @ return matching result 86 */87 + (BOOL) matchString :( NSString *) str withPattern :( NSString *) pattern {88 NSError * error = nil; 89 NSRegularExpression * regular = [NSRegularExpression regularExpressionWithPattern: pattern options: Alert error: & error]; 90 if (! Error) {91 NSTextCheckingResult * result = [regular firstMatchInString: str options: 0 range: NSMakeRange (0, str. length)]; 92 if (result) {93 NSLog (@ "matched successfully, Result: % @", [str substringWithRange: result. range]); 94 return YES; 95} 96} 97 NSLog (@ "failed to match, Error: % @", error); 98 return NO; 99} 100 101 @ end

END! You are welcome to leave a message and learn together...

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.