iOS Regular expressions

Source: Internet
Author: User
Tags uppercase letter

A regular expression is a tool for text matching, and its syntax is elegant and concise. In development, finding, comparing, and matching strings is a commonplace business, and we describe them as certain requirements rules through regular expressions to make our code more beautiful and useful. For example, to verify that the length of the password entered by the user satisfies the length of the 6~18 bit, the most common authentication method for beginners 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 matching character of the above validation change regular expression appears to be short and concise

^. {6,18}$

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

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

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

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



Expression-character parsing

If you are a novice developer, you probably don't understand the above regular expression. So let's look at the first match condition and match the 6-18-bit password length. According to the requirements of the topic, it is easy to determine that {6,18} represents 6-18 bits. where the {n} match character repeats n times, {n,} matches repeats n or more times, and {n,m} matches repeats n to M times.

Then the reader to ^,. and $ three symbols may be unclear so. ^ and $ are special symbols that represent the beginning of a matching string, which represents the end of a matching string. Since we sometimes need to match some small fragment strings in a long string, such as an emoticon that matches a long string in a text-in-picture mash, using these two symbols appropriately can reduce the number of matches and improve execution efficiency.

Then the combination of these characters represents the meaning that can be drawn. The symbol represents any character (except the newline character). It is also possible to conclude that the special symbol {6,18} represents the number of digits represented by the previous symbol. Combine to say ^. {6,18}$ matches a string of 6 to 18 bits in length, so it can be used to verify the password length



Of course, the match rate of the characters is too high, and more often we need to match the exact numbers and letters to numbers or letters. In addition to the. Character, there is also a \d used to represent any single number, \w means any letter or number, or directly using any number 0-9 to represent a specific number. and a special symbol? indicates that the previous character is 0 or 1.

So the above ^0\\d{2}\-?\\d{8}$ matches the number 0, followed by two digits, followed by a eight number, then the end of the string, whether 010-88888888, or 02098989898 can be correctly matched.



The last expression may be the most complex expression, based on the parsing of some of the symbols above. 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 one is not difficult to guess is logical OR. We will split the previous one to identify the matching rules 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 non-newline character. A decorated expression is used to modify a value to achieve a condition, such as {2} indicating that the previous value repeats two times, * indicating that the previous value repeats 0 or more times. According to this method, the ^.*[a-z]+.*[a-z]+.*$ can be split into parts: ^$,. *, [a-z]+, [a-z]+].

^$ will not say more.

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

[a-z]+-Represents a closed set of values formed from the left-hand value to the value on the right, and [] the value in square brackets must be a subset of the middle set of parentheses, note that there can be multiple sets in parentheses, such as [a-z0-9a-z] to match any uppercase or lowercase letter or number; + Represents a duplicate value of at least one.

In conjunction with the above parsing, the ^.*[a-z]+.*[a-z]+.*$ means to start with any number of characters followed by an uppercase letter, with any number of characters followed by an uppercase letter, and a lowercase letter, followed by any number of numbers, letters, or characters. The ^.*[a-z]+.*[a-z]+.*$ represents the position of any number of characters in the lowercase letters before the uppercase letters, and the two combinations match to 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 \, this is because \ itself is an escape symbol, in order to ensure that the expression can be properly matched, we have to give \ One escape, so it becomes \ \. Basically all symbol characters need to be escaped.





Syntax/Character Description table

Value expression

. Match any character except line break

\w characters that match letters or numbers

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

\s matches any white space character (space, tab, line feed)

\s matches any character that is not a white letter

\d matches any number

\d matches any non-numeric character

\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. Any character that matches a non-lowercase letter, such as [^[a-z]]

^ matches the beginning of the string

$ matches the end of a string

Modified expression

* Match repeat any number of times

+ Repeat the number of times more than once

? Match once or 0 times

{n} match repeats n times

{n,} matches repeat n or n more times

{n,m} match repeats at least N times up to M times

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



Code Real

Above, we simply explain what each character in the regular expression represents, and how it should be used in iOS development. For developers who intend to use regular rules to match, my advice is to encapsulate it into a class method, one package at a time, and multiple invocations. The following is a description of 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;
/*! Determine if 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 judgment * *
-(BOOL) Validatewithregexp: (NSString *) RegExp;
@end


Method implementation file:


#import "Uitextfield+lxdvalidate.h"
@implementation Uitextfield (lxdvalidate)
-(BOOL) IsEmpty
{
return self.text.length = = 0;
}
-(BOOL) Validateemail
{
return [self Validatewithregexp: @ "^[a-za-z0-9]{4,}@[a-z0-9a-z]{2,}\\.[ a-za-z]{2,}$ "];
}
-(BOOL) Validateauthen
{
return [self Validatewithregexp: @ "^\\d{5,6}$"];
}
-(BOOL) ValidatePassword
{
NSString * length = @ "^\\w{6,18}$"; Length
NSString * Number = @ "^\\w*\\d+\\w*$"; Digital
NSString * lower = @ "^\\w*[a-z]+\\w*$"; lowercase letters
NSString * Upper = @ "^\\w*[a-z]+\\w*$"; Capital
return [self validatewithregexp:length] && [self validatewithregexp:number] && [self Validatewithregexp:lower] && [self validatewithregexp:upper];
}
-(BOOL) Validatephonenumber
{
NSString * reg = @ "^1\\d{10}$";
return [self validatewithregexp:reg];
}
-(BOOL) Validatewithregexp: (NSString *) REGEXP
{
nspredicate * predicate = [nspredicate Predicatewithformat: @ "Self MATCHES%@", REGEXP];
return [predicate evaluateWithObject:self.text];
}
@end

iOS Regular expressions

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.