Learning to develop regular expressions for iOS

Source: Internet
Author: User

Regular expression Notes

One small step of the day, knowing that I am making progress, is my little goal every day.
This is my first blog, in the spare part, I will try to leave a bit of footprints.
I am an iOS developer, for iOS I am just a rookie, willing to leave my experience here to help more friends to study together, have any questions, are welcome to give advice and criticism.

This is a regular expression foundation and provides some of the regular expression formats that are commonly used today. 1. What is a regular expression

Regular expressions provide a pattern to search for matches in a given pattern, which allows you to do something more useful, such as validating fields, phone numbers, email addresses, checking user input, performing more advanced text operations, and more.

2. Individual study notes

1. Line locator: "^" means the beginning of the line, "$" means the end of the line 2. The word delimiter (\b.\b)    \btm\b   matches the word containing the TM 3. Character class []: Match TM is case insensitive: [tt][mm]4. Select Character |:t|tm|m     start with T or T, followed by a letter M or M5. Hyphen-:[a-za-z]     matches one letter 6. Exclude characters [^]:    [^a-za-z]    matches except letters and underscores 7. Qualifier  ?:  Matches the preceding character 0 or 1 times. Colou?r  can match colour and color         +: Matches the preceding character one or more times. Go+gle  can match gogle ... Goo...gle         *: matches the preceding character 0 or more times.       {n}:  matches the preceding character n times   go{2}gle   matches Google      {n,}: matches the preceding character at least n times     {n,m}: matches the preceding character at least n times, Up to M times 8. The dot character (.): Matches a word, the first letter is R, the third character is S, and the last letter is T.   ^r.s.*t$9. Escape character (\): [0-9]{1,3} (\.[ 0-9]{1,3}) {3}10. Backslash   \d: Any decimal digit equivalent to [0-9]           \d: Any non-decimal digit           \s: Any one of the whitespace characters (\f\n\r\t)           \w: Any one word character, equivalent to [a-za-z0-9_]

3. Attach regular expression code used by iOS

//mailbox verification  
+ (BOOL) Validateemail: (NSString *) email 

NSString *emailregex = @ "[A-z0-9a-z. _%+-][email protected][a-za-z0-9.-]+\\. [A-za-z] {2,4} ";  
Nspredicate *emailtest = [Nspredicate predicatewithformat:@" Self MATCHES%@ ", emailregex]; 
return [Emailtest evaluatewithobject:email]; 

//mobile number verification  
+ (BOOL) Validatemobile: ( NSString *) mobile 

//Phone number starts with 13, 15,18,17, eight \d numeric characters  
NSString *phoneregex = @ "^1 (3[0-9]|4[ 57]|5[0-35-9]|7[01678]|8[0-9]) \\d{8}$ ";  
Nspredicate *phonetest = [Nspredicate predicatewithformat:@" self MATCHES%@ ", phoneregex]; 
return [Phonetest evaluatewithobject:mobile]; 
}

Car Grade Verification
+ (BOOL) Validatecarno: (NSString *) Carno
{
NSString *carregex = @ "^[\u4e00-\u9fa5]{1}[a-za-z]{1}[a-za-z_0-9]{4}[a-za-z_0-9_\u4e00-\u9fa5]$";
Nspredicate *cartest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Carregex];
NSLog (@ "Cartest is%@", cartest);
return [Cartest Evaluatewithobject:carno];
}

Models
+ (BOOL) Validatecartype: (NSString *) Cartype
{
NSString *cartyperegex = @ "^[\u4e00-\u9fff]+$";
Nspredicate *cartest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Cartyperegex];
return [Cartest Evaluatewithobject:cartype];
}

User name
+ (BOOL) Validateusername: (NSString *) name
{
NSString *usernameregex = @ "^[a-za-z0-9]{6,20}+$";
Nspredicate *usernamepredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Usernameregex];
BOOL B = [Usernamepredicate evaluatewithobject:name];
return B;
}

Password
+ (BOOL) ValidatePassword: (NSString *) PassWord
{
NSString *passwordregex = @ "^[a-za-z0-9]{6,20}+$";
Nspredicate *passwordpredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Passwordregex];
return [Passwordpredicate Evaluatewithobject:password];
}

Nickname
+ (BOOL) Validatenickname: (NSString *) nickname
{
NSString *nicknameregex = @ "^[\u4e00-\u9fa5]{4,8}$";
Nspredicate *passwordpredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Nicknameregex];
return [Passwordpredicate Evaluatewithobject:nickname];
}

ID number
+ (BOOL) Validateidentitycard: (NSString *) Identitycard
{
BOOL Flag;
if (identitycard.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 = @ "^ (\\d{14}|\\d{17}) (\\d|[ XX]) $ ";
Nspredicate *identitycardpredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Regex2];
return [Identitycardpredicate Evaluatewithobject:identitycard];
}

4. Reference links

Regular expression Upgrade

Learning to develop regular expressions for iOS

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.