Regular expression implementation and or non-relationship

Source: Internet
Author: User
Write a regular expression that requires the matching string to satisfy both condition A and condition B we can write two sub-expressions Patterna and Patternb, respectively, check condition A and condition B. Then combine it into a new expression (? =patterna) Patternb
For example, when you modify a user's password on Windows Server, the following requirements are generally required: 6 to 256 characters include at least three of the following four classes of characters: number/lowercase/uppercase/Other characters other rules that cannot be verified with regular expressions (such as Minimum password modification time) We can use the following regular expression to verify the legality of the password (where the blue and green parts are Patterna and Patternb): (? =^.{ 6,255}$) ((? =.*\d) (? =.*[a-z]) (? =.*[a-z]) | (? =.*\d) (? =.*[^a-za-z0-9]) (? =.*[a-z]) | (? =.*[^a-za-z0-9]) (? =.*[a-z]) (? =.*[a-z]) | (? =.*\d) (? =.*[a-z]) (? =.*[^a-za-z0-9])) ^.*
Write a regular expression that requires the matching string to satisfy the condition a, but not satisfy the condition B we can first write two sub-expressions Patterna and Patternb, respectively, check condition A and condition B. Then combine into a new expression (?! PATTERNB) Patterna For example, Windows server's machine name generally requires the following requirements (simplified than Microsoft's official requirements): 2 to 15 characters. Can only include: numbers/lowercase letters/uppercase letters/minus signs cannot all be numbers we can use the following regular expression to verify the legality of the machine name (where the blue part is PATTERNB, the green part is Patterna): ^ (?! ^\d*$) ([a-za-z0-9-]{2,15}) $
Write a regular expression that requires matching strings or satisfying condition A or satisfying condition B we can first write two sub-expressions Patterna and Patternb, respectively, check condition A and condition B. And then combine it into a new expression (patterna| PATTERNB)
For example, in a non-mandatory phone number field, the user can enter a valid phone number or leave it blank we can use the following regular expression to verify the legality of the machine name (where the blue part is PATTERNB, the green part is Patterna): (^$|\d{7,16})
Note: The line start character (^), the line terminator ($), and the non-greedy flag (?). The use here is very important.

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.