The password for a user registration function must consist of numbers and letters. It must contain both numbers and letters and be between 8 and 16 characters in length.
How to analyze requirements? Split! This is the general idea of software design. Therefore, the splitting requirement is as follows:
- Not all numbers
- Not all letters
- Must be a number or letter
As long as the above three requirements can be met at the same time, it is written as follows:
1 String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
Separate the following comments:
- ^: Match the starting position of a row
- (?! [0-9] + $): predicts that the position is not followed by all numbers.
- (?! [A-Za-Z] + $): it is predicted that the position is not followed by all letters.
- [0-9a-za-z] {8, 16}: it consists of 8-16 digits or the letter
- $: Match the end of a row
Note :(?! XXXX) is a form of assertion of a regular expression with a negative and Zero Width. It indicates that the pre-location is not a XXXX character.