Common Regular Expressions _ regular expressions for iOS development

Source: Internet
Author: User
Tags lowercase uppercase letter

A regular expression is a tool used for text matching, and its syntax is elegant and concise. In development, finding, contrasting, and matching strings is a commonplace business, with regular expressions we describe these services as some requirements rules to make our code more beautiful and useful. For example, to verify that the password length that the user enters satisfies the length of the 6~18 bit, the most common way for a novice to authenticate is to determine the length of the password entered

Return (textField.text.length >= 6 && TextField.text.leng <= 18);

Although there is no problem with this way of judging, the validation above is short and short for matching characters of regular expressions

^. {6,18}$

Also, most of the landline phones are 0 area code-eight-digit format, so the regular expression matches the following

^0\\d{2}\-?\\d{8}$

For example, for password strength certification. Today's passwords should include one or more uppercase and lowercase letters, and the matching of this regular expression is

(^.*[a-z]+.*[a-z]+.*$|^.*[a-z]+.*[a-z]+.*$)

Expression-character parsing

If you're a novice developer, you probably don't understand the regular expression above. So let's first look at the first matching condition, matching the 6-18-bit password length. According to the requirements of the topic, you can easily judge {6,18} to represent 6-18 bits. where {n} matches characters repeat n times; {n} matches repeated n or more times; {n,m} matches repeated n to M.

Then the reader to ^,. and $ three symbols may be unclear so. ^ and $ belong to special symbols that represent the beginning of a matching string, which represents the end of a matching string. Because we sometimes need to match some small fragment strings in a long string (for example, to match the left-empty emoticons in a long string in a text mix), the proper use of these two symbols can reduce the number of matches and improve execution efficiency.

So the combination of these characters represents meaning, you can draw. A symbol represents any character (except for a newline character). It can also be concluded that this special symbol for {6,18} represents the number of digits represented by the previous symbol. Combined to say ^. {6,18}$ matches a 6-to 18-bit length string so it can be used to verify password length

Of course,. The character matching rate is too high, more times we need to match the numbers or letters, even the exact numbers and letters. In addition to the. character, there are \d used to represent any single digit, \w represents any letter or number, or any number directly using 0-9 to represent the specific number. and a special symbol? indicates that the previous character is 0 or 1.

So the above ^0\\d{2}\-?\\d{8}$ matches with the number 0, followed by two digits, followed by-number and eight digits, and then the ending string, whether 010-88888888 or 02098989898, matches correctly.

The last expression may be the most complex expression, based on the above partial symbol parsing. We can put

^.*[a-z]+.*[a-z]+.*$|^.*[a-z]+.*[a-z]+.*$

Split into ^.*[a-z]+.*[a-z]+.*$ and ^.*[a-z]+.*[a-z]+.*$.

These two separate expressions, while the middle of the | not hard to guess is logical OR. We'll split the previous one to identify the matching rule for this expression step by step. Here's how I split the expression: I divide the character of the expression into two categories: value expression and cosmetic expression. The so-called value expression means that the symbol represents a value, just as \d represents a number,. Represents any newline character. A cosmetic expression is used to modify a value to achieve a condition, such as {2}, which means that the previous value repeats two times, * indicates that the previous value is repeated 0 or more times. According to this method, ^.*[a-z]+.*[a-z]+.*$ can be split into parts: ^$,. *, [a-z]+, a-z]+.

^$ will not say more.

. * Here's what we're going to say. * Indicates that the previous value symbol repeats any number of times.

[a-z]+-Represents a closed set of all values from the left value to the right value; [] the value in square brackets must be a subset of the middle set of parentheses, and note that there can be more than one set in parentheses, such as [a-z0-9a-z] to match any one of the uppercase and lowercase letters or numbers; + with * a bit like, but + Represents a duplicate value of at least one.

In conjunction with the above parsing, ^.*[a-z]+.*[a-z]+.*$ represents an arbitrary number of characters, followed by an uppercase letter, followed by an arbitrary number of characters and a lowercase letter, followed by any number of digits, letters, or characters. While ^.*[a-z]+.*[a-z]+.*$ represents the position of any number of characters in front of the lowercase letters before uppercase letters, two matching matches ensure that the string includes at least one lowercase letter and one upper case.

PS: One thing to note is that the expression shown above \d These special symbols in our code more than one \, because \ itself is an escape symbol, in order to ensure that the expression can be a normal match, we have to give \ an escape, so it becomes \. Basically all the symbolic characters need to be escaped.

Syntax/Character Description table

Value expression

. Match any character except the line feed

\w characters that match letters or numbers

\w matches any character that is not a letter or number

\s matches any whitespace (spaces, tabs, line breaks)

\s matches any character that is not whitespace

\d matches any number

\d matches any number of non-numeric characters

\b A character that matches the end or beginning of a word

\b matches any character that is not the end or beginning of a word

[^x] matches any character that is not X. If [^[a-z]] matches any character that is not a lowercase letter

^ matches the beginning of a string

$ match End of string

Cosmetic expression

* Match repeat any number of times

+ Match repeat number of times above

? Match once or 0 times

{n} match repeat n times

{N,} match repeat n times or more than n times

{n,m} match repeat at least n times Max M times

In addition to the characters listed above, there are other regular expressions that are more difficult to apply, such as position designation, but the above characters are sufficient for normal use. Want to know more knowledge can be the mother of Google.

Code Practice

We simply explained the meaning of each character in the regular expression and how it should be used in iOS development. For developers who intentionally use regular rules to match, my advice is to encapsulate them into category methods, one at a time, and multiple calls. The following is illustrated with my own encapsulated code. These methods are implemented by extending the Uitextfield method:

@interface Uitextfield (lxdvalidate)
/*! Determines whether the text box is empty (non-regular expression).
-(BOOL) IsEmpty;
/*! To determine whether the mailbox is correct * *
-(BOOL) Validateemail;
/*! Determine if the verification code is correct.
-(BOOL) Validateauthen;
/*! Determine if the password format is correct.
-(BOOL) ValidatePassword;
/*! Determine if the phone number is correct.
-(BOOL) Validatephonenumber;
/*! Write your own regular incoming for judgment
-(BOOL) Validatewithregexp: (NSString *) RegExp;
@end

Method implementation file:

#import "Uitextfield+lxdvalidate.h" @implementation Uitextfield (lxdvalidate)-(BOOL) IsEmpty {return self.text.length
== ; }-(BOOL) Validateemail {return [self validatewithregexp: @] ^[a-za-z-]{,}@[a-z-a-z]{,}\\.[
a-za-z]{,}$ "];  }-(bool) Validateauthen {return [self validatewithregexp: @ "^\\d{,}$"];}-(bool) ValidatePassword {NSString * length =    @ "^\\w{,}$";   Length NSString * number = @ "^\\w*\\d+\\w*$";   Digital NSString * lower = @ "^\\w*[a-z]+\\w*$";  Lowercase letter NSString * Upper = @ "^\\w*[a-z]+\\w*$"; Capital letter return [self validatewithregexp:length] && [self validatewithregexp:number] && [self validatewit
Hregexp:lower] && [self validatewithregexp:upper]; }-(bool) Validatephonenumber {NSString * reg = @ "^\\d{}$"; return [self validatewithregexp:reg];}-(BOOL) Validatewith REGEXP: (NSString *) REGEXP {nspredicate * predicate = [nspredicate Predicatewithformat: @ "SELF matches%@", REGEXP]; retu
RN [predicate evaluateWithObject:self.text];
 } @end

The above content is small series to introduce about iOS development commonly used regular expression, I hope you like.

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.