How to implement regular expressions for password validation
This article mainly shows you "how to implement regular expressions for password verification". The content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn the article "how to implement regular expressions for password verification".
What is a regular expression?
When writing programs or web pages that deal with strings, there is often a need to find strings that conform to some complex rules. Regular expressions are the tools used to describe these rules. In other words, a regular expression is the code that records text rules.
Php password validation regular expression (8-bit length limit)
Python password validation regular expression
# encoding=utf-8import redef checklen (pwd): return len (pwd) > = 8def checkContainUpper (pwd): pattern = re.compile ('[Amurz] +') match = pattern.findall (pwd) if match: return True else: return Falsedef checkContainNum (pwd): pattern = re.compile ('[0-9] +') match = pattern.findall (pwd) if match: return True else: return Falsedef checkContainLower (pwd): pattern = re.compile ('[aMuz] +') match = pattern.findall (pwd) if match : return True else: return Falsedef checkSymbol (pwd): pattern = re.compile ('([^ a-z0-9A-Z]) +') match = pattern.findall (pwd) if match: return True else: return Falsedef checkPassword (pwd): # determine whether the password length is legal lenOK=checklen (pwd) # determine whether it contains the uppercase letter upperOK=checkContainUpper (pwd) # determine whether it contains the lowercase letter lowerOK=checkContainLower (pwd) # determine whether it contains the number numOK=checkContainNum (pwd) # judge Whether to include the symbol symbolOK=checkSymbol (pwd) print (lenOK) print (upperOK) print (lowerOK) print (numOK) print (symbolOK) return (lenOK and upperOK and lowerOK and numOK and symbolOK) def main (): if checkPassword ('Helloworld#123'): print (' test passed') else: print ('test failed') if _ name__ = ='_ main__': main ()
Java password validation regular expression
/ * @ ClassName: RexTest * @ Description: TODO* @ author BMR* @ date 11:09:17 * / public class RexTest {/ * regular expression validation password * @ param input * @ return * / public static boolean rexCheckPassword (String input) {/ / 6-20 digits Letters, numbers, characters / / String reg = "^ ([Amurz] | [amurz] | [0-9] | [`- = []) ,. / ~! @ # $% ^ * () _ +} {:]) {6pm 20} $"; String regStr =" ^ ([Amurz] | [Amurz] | [0-9] | [`~! @ # $% ^ & * () + = | {}':;',\ [\]. /? ~! @ # ¥%. & * ()-+ | {} []';: "'. ,?]) {6 return input.matches 20} $"; return input.matches (regStr);} public static void main (String [] args) {System.out.println (" rexCheckPassword is: "+ rexCheckPassword (" 14` ~! @ # $% ^ & * (\) + = | {} "));}
Output result:
RexCheckPassword is: true
Questions raised by netizens:
Regular expression check password
1. The password must be made up of two of the three types: numbers, characters and special characters.
2. The password cannot be less than 8 characters in length.
To meet the above two points, how to achieve?
(?! ^\\ digital $) can't be all numbers.
(?! ^ [a-zA-Z] + $) cannot be all letters
(?! ^ [_ # @] + $) cannot be all symbols (only some symbols are listed here, but they can be added by themselves, and some symbols may need to be escaped)
. {8,} length cannot be less than 8 digits
Together, it is.
(?! ^\\ dong$) (?! ^ [a-zA-Z] + $) (?! ^ [_ # @] + $). {8,}
Complete js regular expression:
/ / strong: alphabetic + numeric + special character ^ (?! [a-zA-z] + $) (?! [! @ # $% ^ & *] + $) (?! [a-zA-z\ d] + $) (?! [Amurazi / a-zA-z / special characters% ^ & *] + $) (?! [\ dcharacters /% ^ & *] + $) [a-zA-Z\ dcards /% ^ & *] + $) / / medium: letters + numbers Letters + special characters, numbers + special characters ^ (?! [a-zA-z] + $) (?!\ dletters $) (?! [! @ # $% ^ & *] + $) [a-zA-Z\ dletters letters% ^ & *] + $/ / weak: pure numbers, pure letters Pure special character ^ (?:\ d + | [a-zA-Z] + | [! @ # $% ^ & *] +) $/ / check whether it is composed entirely of numbers function isDigit (s) {var patrn=/ ^ [0-9] {1P 20} $/ If (! patrn.exec (s)) return falsereturn true} / / check login name: only 5-20 logins can be entered, starting with a letter, with numbers, "_", "." The string function isRegisterUserName (s) {var patrn=/ ^ [a-zA-Z] {1} ([a-zA-Z0-9] | [. _]) {4 patrn.exec 19} $/; if (! var patrn=/ (s)) return falsereturn true} function isRegisterUserName (s) {var patrn=/ ^ [a-zA-Z] {1} ([a-zA-Z0-9] | [. _]) {4 var patrn=/ 19} $/ If (! patrn.exec (s)) return falsereturn true} / / verify user name: only 1-30 strings starting with letters can be entered: Javascript code function isTrueName (s) {var patrn=/ ^ [a-zA-Z] {1mae 30} $/; if (! patrn.exec (s)) return falsereturn true} / / verify password: only 6-20 letters, numbers, underscores function isPasswd (s) {var patrn=/ ^ (\ w) {6pm 20} $/ / can be entered If (! patrn.exec (s)) return falsereturn true} / / verify ordinary telephone and fax numbers: you can start with "+" and contain "-" function isTel (s) {/ / var patrn=/ ^ [+] {0Mague 1} (\ d) {1Mague 3} []? ([-]? (\ d) {1cot 12}) + $/ Var patrn=/ ^ [+] {0jue 1} (\ d) {1JI 3} []? ([-]? ((\ d) | []) {1Jet 12}) + $/ If (! patrn.exec (s)) return falsereturn true} / / verify the mobile phone number: it must start with a number and may contain "-" function isMobil (s) {var patrn=/ ^ [+] {0jue 1} (\ d) {1prit 3} []? ([-]? (\ d) | []) {1m 12}) + $/ If (! patrn.exec (s)) return falsereturn true} / / check Postal Code function isPostalCode (s) {/ / var patrn=/ ^ [a-zA-Z0-9] {3 a-zA-Z0 12} $/; var patrn=/ ^ [a-zA-Z0-9] {3 a-zA-Z0 12} $/; if (! patrn.exec (s)) return falsereturn true} / / verify the search keyword function isSearch (s) {var patrn=/ ^ [^ `~! @ # $% ^ & * () + = |\\] [\]\ {\}: '\,. /?] {1} [^ `~! @ $% ^ & () + = |\\] [\]\]\ {{\}:;'\,.] {0return falsereturn true 19} $/; if (! patrn.exec (s)) return falsereturn true} function isIP (s) / / by zergling {var patrn=/ ^ [0-9.] {1pm 20} $/; if (! patrn.exec (s)) return falsereturn true}
Regular expression
^\ / non-negative integer (positive integer + 0) ^ [0-9] * [1-9] [0-9] * $/ / positive integer ^ ((-\\ d+) | (0 +) $/ / non-positive integer (negative integer + 0) ^-[0-9] * [1-9] [0-9] * $/ / negative integer ^ -? / / non-negative floating point number (positive floating point number + 0) ^ (([0 # 9] +\. [0 # 9] * [1 # 9] [0 # 9] *) | ([0 # 9] * [1 # 9] *. [0 # 9] +) | ([0 # 9] * [1 # 9] [0 # 9] *)) $/ positive floating point ^ ((-\\ d + (/ / non-positive floating point number ^) Floating point number (negative floating point number + 0) ^ (- ([0-9] +\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] +) | ([0-9] * * [1-9] [0-9] *)) $/ / negative floating point ^ (-?\\ d+) (/ / floating point ^ [A] -Za-z] + $/ / string of 26 letters ^ [Amurz] + $/ / string of 26 letters of uppercase ^ [Amurz] + $/ / string of 26 letters of lowercase ^ [A-Za-z0-9] + $/ / string of numbers and 26 letters ^\\ Chinese $/ / consisting of numbers, A string consisting of 26 English letters or underscores ^ [\\ w -] + (/ / email address ^ [a-zA-z] +: / / (/ / url ^ [A-Za-z0-9 _] * $)
A regular expression that matches the full domain name:
[a-zA-Z0-9] [- a-zA-Z0-9] {0a-zA-Z0 62} (\. [a-zA-Z0-9] [- a-zA-Z0-9] {0girls 62}) +\?
The above is all the contents of the article "how to implement regular expressions for password validation". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!