IOS: detailed Regular expressions

Source: Internet
Author: User

1, Introduction:

In the project, regular use is common, such as login account and password (mobile number, mailbox, etc.). The method used is predicate object filtering:nspredicate.

2. What is a regular expression:

Regular expressions, also known as formal representations, are a logical formula for manipulating strings. A regular expression can detect whether a given string conforms to our defined logic, or we can get the specific part we want from a string. It can quickly achieve complex control of strings in a very simple way.

3. Syntax:

First, the special symbols ' ^ ' and ' $ '. Their role is to indicate the beginning and end of a string, respectively. Eg: "^one": denotes all strings starting with "one" ("One Cat", "one123″, ); similar to:-(BOOL) Hasprefix: (NSString *) astring; " A dog$ ": the string that ends with" a dog "(" It is a dog ", ); similar to:-(BOOL) Hassuffix: (NSString *) astring; " ^apple$ ": Indicates that the start and end are" apple "strings, this is the only ~;" Banana ": represents any string that contains" banana ". A new method similar to IOS8-(BOOL) containsstring: (NSString *) astring, searching for substrings. ' * ', ' + ' and '? ' These three symbols represent the number of occurrences of one or n characters. They mean "no or more" ([0,+∞] rounding), "one or more" ([1,+∞] rounding), "No or Once" ([0,1] rounding). Here are a few examples: "ab*": a string that has an a followed by 0 or several B ("a", "AB", "ABBB",...... "ab+": Indicates that a string has a followed by at least one B or more ("AB", "ABBB",...... ); "Ab?" : Indicates that a string has a followed by 0 or a B ("a", "AB"); "a?b+$": indicates that at the end of the string there are 0 or one a followed by one or several B ("B", "AB", "BB", "ABB",...... )。 can be enclosed in curly braces ({}), indicating a specific range of duplicates. For example "Ab{4}": Indicates that a string has a followed by 4 B ("abbbb"); "Ab{1,}": Indicates that a string has a followed by at least 1 B ("AB", "ABB", "ABBB",......) ; "ab{3,4}": Indicates that a string has a followed by 3 to 4 B ("abbb", "abbbb"). Then, "*" can be used {0,} to indicate, "+" can be used {1,}, "?" You can use {0,1} to note: There can be no lower bound, but there is no limit! For example, "ab{,5}" is the wrong wording "| "Represent" or "Action:" A|b ": Indicates that a string has" a "or" B ";" (A|BCD) EF ": means" AEF "or" Bcdef ";" (a|b) *c ": A String" a "" B "is followed by a" C ", and the square brackets" [] "means the parenthesesAmong the numerous characters in the 1-n, select the syntax-conforming character in parentheses as the result, for example "[AB]": Indicates that a string has a "a" or "B" (equivalent to "a|b"); "[A-d]": Indicates that a string contains a lowercase ' a ' to ' d ' of one (equivalent to "a|b|c|d "[ABCD]"); "^[a-za-z]": denotes a string that begins with a letter; "[0-9]a": A number preceded by a digit; "[a-za-z0-9]$": A string that ends with a letter or number. “.” Match any single character except \ r \ n: "A.[a-z]": Indicates that a string has a "a" followed by an arbitrary character and a lowercase letter; "^. {5}$ ": represents any 1 string of length 5;" \num "where num is a positive integer. Indicates that the characters before "\num" appear the same number, for example "(.) \1″: Represents two consecutive identical characters. "10\{1,2\}": Represents a number 1 followed by 1 or 2 0 ("10″," 100″). "0\{3,\}" indicates a number of at least 3 consecutive 0 ("000", "0000", ... )。 Use ' ^ ' in square brackets to denote unwanted characters, ' ^ ' should be the first in square brackets. "@[^a-za-z][email protected]" means that no letters should appear in two "@". It is also commonly used: "\d" matches a numeric character. equivalent to [0-9]. "\d" matches a non-numeric character. equivalent to [^0-9]. "\w" matches any word character that includes an underscore. Equivalent to "[a-za-z0-9_]". "\w" matches any non-word character. Equivalent to "[^a-za-z0-9_]". Write regular expressions in iOS, encounter escape characters, add a "\", for example: Full character: @ "^\\d\+$"

4. Regular expressions are commonly used as follows: (email, phone number, ID, password, nickname)

Mailbox + (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{//Mobile phone number starts with 13, 15, 18, eight \d numeric characters nsstring *phoneregex = @ "^ ((13[0-9] )| (15[^4,\\d]) | (18[0,0-9]))    \\d{8}$ ";    Nspredicate *phonetest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Phoneregex];  return [Phonetest evaluatewithobject:mobile];} Vehicle 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];} Model + (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, I organized a regular expression of the class, for everyone to use, listed the regular expressions commonly used, of course, you can also expand (custom bar)

Github:https://github.com/xiayuanquan/xyqregexpattern

Support Cocoapods:pod ' Xyqregexpattern ', ' ~> 2.0.0 '

IOS: detailed Regular expressions

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.