Common Regular Expressions for IOS development

Source: Internet
Author: User

Common Regular Expressions for IOS development

A regular expression is a tool used for text matching. Its syntax is elegant and concise. In development, it is common to search, compare, and match strings. Through regular expressions, we describe these businesses into certain requirements and rules to make our code more beautiful and practical. For example, we want to verify whether the password length entered by the user is 6 ~ 18-bit length. The most common Verification Method for new users is to determine the length of the entered password.

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

Although there is no problem with this method of determination, the above verification for the regular expression matching character is short and concise

^. {6, 18} $

Also, most of the landlines are in the format of "0"-"8-digit", so the regular expression matching is as follows:

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

For example, password strength authentication. The current password should include one or more upper-case letters and lower-case letters.

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

Expression Character Parsing

If you are a novice developer, you may not understand the regular expression above. First, let's look at the first matching condition to match the length of the 6-18-bit password. According to the requirements of the question, we can easily determine that {6, 18} represents 6-18 digits. Among them, {n} matches the characters for n times; {n,} matches the characters for n times or more; {n, m} matches the number of times between n and m.

The readers may not be clear about the symbols ^,., and $. ^ And $ are special symbols. The former indicates matching the start of a string, and the latter indicates matching the end of a string. Because we sometimes need to match certain small fragment strings in a long string (for example, matching empty emoticon characters in a long string in text-and-image mixing ), using these two symbols can reduce the number of matching times and improve execution efficiency.

Based on the meanings of these characters, we can conclude that the. symbol represents any character (except the line break ). The special symbol {6, 18} represents the number of digits represented by the previous symbol. In combination, ^. {6, 18} $ matches a string of 6 to 18 characters, so it can be used to verify the password length.

Of course, the matching rate of. characters is too high. More often we need to match numbers or letters, or even accurate numbers and letters. In addition to the. character, \ d is also used to represent any single number. \ w represents any letter or number, or any number that uses 0-9 to represent a specific number. And special symbols? It indicates that the first character is 0 or 1.

So the above ^ 0 \ d {2 }\-? \ D {8} $ matches the string that starts with a number 0, followed by two numbers, followed by a-number and eight digits, and then ends with a string, whether it is 010-88888888, or 02098989898 can be matched correctly.

The last expression may be the most complex expression, which is parsed based on some of the above symbols. We can

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

Split into ^. * [A-Z] +. * [a-z] +. * $ with ^. * [a-z] +. * [A-Z] +. * $

The two independent expressions, while the Middle | is not hard to guess logical or. We will split the previous one to identify the matching rules of this expression step by step. Here we will teach you how to split expressions: I divide expressions into two types: Value expression and modifier expression. The so-called value expression means that this symbol represents a value, just as \ d represents a number and. represents any non-linefeed character. The modifier expression is used to modify a value to reach a certain condition. For example, {2} indicates that the previous value is repeated twice, and * 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] +.

^ $.

. * Here we will introduce * to indicate the number of times that the previous value symbol repeats at any time.

[A-Z] +-indicates a closed set of all values formed from the value on the left to the value on the right; [] the value represented by square brackets must be a subset of the Set in the middle of the brackets, note that there can be multiple sets in the brackets, for example, [A-Z0-9a-z] indicates matching any one of the upper and lower case letters or numbers; + a bit like, but + indicates at least one duplicate value.

Combined with the above parsing, then ^. * [A-Z] +. * [a-z] +. * $ indicates starting with any number of characters, followed by an upper-case letter, followed by any number of characters and a lower-case letter, followed by any number of numbers, letters, or characters. ^. * [A-z] +. * [A-Z] +. * $ indicates the position of lowercase letters in front of any number of characters. The combination of the two matches ensures that the string contains at least one lowercase letter and one uppercase letter.

Ps: One thing to note is that when the above expression is parsed, the special symbols \ d are added to our code. This is because \ itself is an escape symbol, to ensure normal expression matching, we need to escape \ once, so it becomes \\. Basically, all symbolic characters must be escaped.

Syntax/Character Description

Value expression

. Match any character except linefeed

\ W matches letters or numbers

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

\ S matches any blank space characters (spaces, tabs, line breaks)

\ S matches any character that is not a blank character

\ D match any number

\ D matches any non-numeric characters

\ B matches the end or start of a word

\ B matches any character that is not the end or start of a word

[^ X] matches any non-x characters. For example, [^ [a-z] matches any character of a non-lowercase letter.

^ Match the start of a string

$ Match the end of a string

Modify expression

* Match any number of times

+ Repeated times

? Match once or zero times

{N} matches multiple times

{N,} matches for n or more times

{N, m} matches at least n times and at most m times

In addition to the characters listed above, there are other regular expressions that are difficult to use, such as specifying the position, but the above characters are enough for our normal use. For more information, visit Niang Google.

Code practice

The above is just a simple explanation of the meaning of each character in the regular expression, so how to use it in iOS development. For developers who intend to use regular expression rules to match, I suggest encapsulating them into class methods, encapsulating them once and calling them multiple times. The following code is used to describe it. These methods are implemented by extending the UITextField method:

@ Interface UITextField (LXDValidate)
/*! Determines whether the text box is empty (non-regular expression )*/
-(BOOL) isEmpty;
/*! Check whether the email address is correct */
-(BOOL) validateEmail;
/*! Determine whether the verification code is correct */
-(BOOL) validateAuthen;
/*! Determine whether the password format is correct */
-(BOOL) validatePassword;
/*! Determine whether the mobile phone number is correct */
-(BOOL) validatePhoneNumber;
/*! Write regular expressions on your own for input 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 * $ "; // number NSString * lower = @ "^ \ w * [a-z] + \ w * $ "; // lowercase letter NSString * upper = @ "^ \ w * [A-Z] + \ w * $"; // uppercase letter return [self validateWithRegExp: length] & [self validateWithRegExp: number] & [self validateWithRegExp: lower] & [self validateWithRegExp: upper];}-(BOOL) validatePhoneNumber {NSString * reg = @ "^ \ d {}$"; return [self validateWithRegExp: reg] ;}- (BOOL) validateWithRegExp: (NSString *) regExp {NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "self matches % @", regExp]; return [predicate evaluateWithObject: self. text] ;}@ end

The above content is a regular expression that is commonly used in IOS development. I hope you will like it.

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.