A regular expression that matches numbers and letter ciphersDecember 14, 2011 | Filed under: Regular expression and tagged with: password , regular expression , 0 wide assertion
A user registration function password has the following requirements: consists of numbers and letters, and must contain 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:
^(?! [0-9]+$) (?! [a-za-z]+$] [0-9a-za-z]{8,16}$
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.
The test cases are as follows:
public class Test {public static void main (string[] args) throws Exception {String regex = "^ (?! [0-9]+$) (?! [a-za-z]+$] [0-9a-za-z]{8,16}$]; String value = "AAA"; Length Not enough System.out.println (Value.matches (regex)); value = "1111AAAA1111AAAAA"; Too long System.out.println (value.matches (regex)); value = "111111111"; Pure Digital System.out.println (value.matches (regex)); value = "AAAAAAAAA"; Pure Letter System.out.println (value.matches (regex)); value = "####@@@@#"; Special character System.out.println (Value.matches (regex)); value = "1111AAAA"; Alphanumeric Combination System.out.println (value.matches (regex)); value = "aaaa1111"; Alphanumeric Combination System.out.println (value.matches (regex)); value = "AA1111AA";//alphanumeric combination System.out.println (Value.matches ( regex); value = "11AAAA11";//alphanumeric combination System.out.println (value.matches (regex)); value = "Aa11aa11"; Alphanumeric Combination System.out.println (value.matches (regex));}}
Java regular Check, passwords must consist of letters and numbers