Java regular Check, passwords must consist of letters and numbers

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.