Today in the project need to judge the user entered the length of the user name 8-16-bit and contains both numbers and letters, the online search for the use of regular expressions, and then refer to this blog, the perfect solution to the problem. Record it:
The password has the following requirements: It consists of numbers and letters, and contains both numbers and letters, and the length is between 8-16 bits.
How do I analyze requirements? Split it! This is the general idea of software design. The split requirements are as follows:
1, not all numbers.
2, not all letters.
3, must be a number or a letter
As long as the above 3 requirements can be satisfied, write to the following:
1 |
^(?![0-9]+$)(?![a-za-z]+$)[0-9a-za-z]{8, -}$ |
Separate to annotate:
^ matches the beginning of a line
(?! [0-9]+$] predict that the position is not all numbers behind
(?! [a-za-z]+$] predict that the position is not all letters behind
[0-9a-za-z] {8,16} consists of 8-16 digits or this letter
$ Match Line End position
Note: (?! XXXX) is the negative 0-wide assertion of a regular expression of a form that identifies the pre-position after it is not the XXXX character.
Code that is used in iOS:
* * @author KaKa, 15-07-31 14:07:02 * * To Determine whether the password entered by the user is compliant with the code requirements: 1. Length greater than 8 bits 2. The password must contain both the number and the letter */+ ( BOOL) Judgepasswordlegal: (NSString *) pass{ BOOL result = false; if ([Pass length] >= 8) { //judgment length is greater than 8 digits and then determine if both numbers and characters are included NSString * regex = @ "^ (?! [0-9]+$) (?! [a-za-z]+$] [0-9a-za-z]{8,16}$]; Nspredicate *pred = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex]; result = [pred evaluatewithobject:pass]; } return result;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Using regular expressions in iOS to determine the input is 8-16-bit and contains both numbers and letters