Home >Web Front-end >JS Tutorial >A brief analysis of regular expressions for matching numeric and alphabetic passwords

A brief analysis of regular expressions for matching numeric and alphabetic passwords

微波
微波Original
2017-06-28 13:30:441121browse

I was working on a project recently and encountered such a requirement. It is required to be composed of numbers and letters, and it must contain both numbers and letters, and the length must be between 8-16 digits. Let me share with you the implementation code. Please take a look.

The password for a user registration function has the following requirements: it must be composed of numbers and letters, and it must contain both numbers and letters, and the length must be Between 8-16 bits.

How to analyze requirements? Split! This is the general idea of ​​​​software design. So, the splitting requirements are as follows:

1, it cannot be all numbers

2, it cannot be all letters

3, it must be numbers or letters

As long as the above three requirements can be met at the same time, write it out as follows:

^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$

Comment it separately:

^ Match the beginning of a line

(?! [0-9]+$) Predict that the position is not followed by all numbers

(?![a-zA-Z]+$) Predict that the position is followed by not all letters

[0- 9A-Za-z] {8,16} consists of 8-16 digits or letters

$ Matches the end of the line position

Note: (?!xxxx) isRegular expression is a form of negative zero-width assertion, indicating that the character after the preset position is not xxxx.

The test examples 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"; // 长度不够
    System.out.println(value.matches(regex));
    value = "1111aaaa1111aaaaa"; // 太长
    System.out.println(value.matches(regex));
    value = "111111111"; // 纯数字
    System.out.println(value.matches(regex));
    value = "aaaaaaaaa"; // 纯字母
    System.out.println(value.matches(regex));
    value = "####@@@@#"; // 特殊字符
    System.out.println(value.matches(regex));
    value = "1111aaaa"; // 数字字母组合
    System.out.println(value.matches(regex));
    value = "aaaa1111"; // 数字字母组合
    System.out.println(value.matches(regex));
    value = "aa1111aa";  // 数字字母组合
    System.out.println(value.matches(regex));
    value = "11aaaa11";  // 数字字母组合
    System.out.println(value.matches(regex));
    value = "aa11aa11"; // 数字字母组合
    System.out.println(value.matches(regex));
  }
}

The above is a regular expression introduced by the editor to match numeric and letter passwords. I hope it will be helpful to everyone. If you If you have any questions, please leave me a message and I will reply to you in time.

The above is the detailed content of A brief analysis of regular expressions for matching numeric and alphabetic passwords. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn