How to implement regular expressions that match numeric and alphabetic passwords
This article mainly introduces how to achieve regular expressions that match numeric and alphabetic passwords. It is very detailed and has a certain reference value. Friends who are interested must read it!
The password for a user registration function has the following requirements: it consists of numbers and letters, and it should contain both numbers and letters, and the length should be between 8 and 16 digits.
How to analyze the requirements? Break it up! This is the general idea of software design. Therefore, the split requirements are as follows:
1, it can't be all numbers.
2, it can't be all letters
3, must be a number or letter
As long as you can meet the above three requirements at the same time, write as follows:
^ (?! [0-9] + $) (?! [a-zA-Z] + $) [0-9A-Za-z] {8jue 16} $
Let's comment separately:
^ matches the beginning of a line
(?! [0-9] + $) predict that the position is not all followed by numbers
(?! [a-zA-Z] + $) predicts that the position is not all followed by letters.
[0-9A-Za-z] {8001 16} consists of 8-16 digits or this letter
$match line end position
Note: (?! xxxx) is a form of negative zero width assertion of a regular expression that indicates that the position is not preceded by a xxxx character.
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] {8regex 16} $"; String value = "aaa"; / / not long enough System.out.println (value.matches (regex)); value = "1111aaaa1111aaaaa"; / / too long System.out.println (value.matches (regex)) Value = "111111111"; / / Numeric 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" / / numeric alphanumeric combination System.out.println (value.matches (regex)); value = "aaaa1111"; / / numeric alphanumeric combination System.out.println (value.matches (regex)); value = "aa1111aa"; / / numeric letter combination System.out.println (value.matches (regex)); value = "11aaaa11"; / / numeric letter combination System.out.println (value.matches (regex)) Value = "aa11aa11"; / / numeric and alphanumeric combination System.out.println (value.matches (regex));}} this is all about the article "how to implement regular expressions that match numeric and alphabetic passwords". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!