[PYTHON] By language: Regular expressions for passwords

Introduction

Regular expression with alphanumeric symbols (case insensitive) and 8 or more characters, but for each language I will also summarize the optimized ones. However, there are some languages that I don't know much about, so please point out any mistakes.

Points to keep in mind

line

--In JavaScript, ^ $ represents the beginning and end, respectively, unless you specify multiline mode. \ A`` \ z does not exist in the first place. --In Ruby, ** multi-line mode ** by default, so ^ $ will match the beginning and end of each line, respectively. This makes no sense for regular expressions, so use \ A`` \ z to match the beginning and end. --In PHP, it's not as serious as Ruby, but since $ matches the end or the end ** from a newline **, use \ z without a newline. --The \ z in Ruby and PHP is equivalent to \ Z in Python.

Pattern modifier

--In JavaScript, Ruby, and PHP, you can simply add Case-Insensitive matching by adding ʻiafter the delimiter. --In Python, Case-Insensitive is declared by including(? I)` in the pattern. There is a way to specify it with another argument, but this is simpler. --Pattern modifiers cannot be used in HTML attributes.

Quantity specifier

--When using the longest match, limit the character type. --In Ruby and PHP, exclusive quantity specifiers are available. There is no loss in using it, so I will use it.

Escape character class

--In Ruby, you need to escape [ in the character class.

Various regular expressions

** (2015/06/23 am) ** Added the one that prioritizes execution speed. However, I personally think that it is a regular expression that does not need to sacrifice readability extremely, so I commented it out to make it less noticeable. Consider using it when you need to be concerned about execution speed.

** (2015/06/23 afternoon) ** If you're worried that . *? Matches an infinite length string in theory, you can be a little relieved just by setting it to. {0,99}? . This change wouldn't make it much less readable.

Regular expression with 8 to 100 single-byte alphanumeric characters

This is the simplest pattern. I'm a little worried when asked if this is all right in this era ...

JavaScript 


/^[a-z\d]{8,100}$/i

HTML Pattern Attribute


"^[a-zA-Z\d]{8,100}$"

Ruby


/\A[a-z\d]{8,100}+\z/i

PHP


'/\A[a-z\d]{8,100}+\z/i'

Python


'\A[a-z\d]{8,100}\Z(?i)'

Regular expression of 8 to 100 characters including one or more half-width alphanumeric characters

By using affirmative look-ahead, it is possible to make a statement that ** contains at least one character. Negative look-ahead like Verify whether half-width alphanumeric characters and half-width symbols are used in passwords in VBScript regular expressions It can be realized by using it, but even when the number of character types increases, affirmative look-ahead can be described more simply.

JavaScript 


/^(?=.*?[a-z])(?=.*?\d)[a-z\d]{8,100}$/i
# /^(?=\d{0,99}[a-z])(?=[a-z]{0,99}\d)[a-z\d]{8,100}$/i

HTML Pattern Attribute


"^(?=.*?[a-zA-Z])(?=.*?\d)[a-zA-Z\d]{8,100}$"
# "^(?=\d{0,99}[a-zA-Z])(?=[a-zA-Z]{0,99}\d)[a-zA-Z\d]{8,100}$"

Ruby


/\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{8,100}+\z/i
# /\A(?=\d{0,99}+[a-z])(?=[a-z]{0,99}+\d)[a-z\d]{8,100}+\z/i

PHP


'/\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{8,100}+\z/i'
# '/\A(?=\d{0,99}+[a-z])(?=[a-z]{0,99}+\d)[a-z\d]{8,100}+\z/i'

Python


'\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{8,100}\Z(?i)'
# '\A(?=\d{0,99}[a-z])(?=[a-z]{0,99}\d)[a-z\d]{8,100}\Z(?i)'

Regular expression of 8 to 100 characters including one or more half-width lowercase letters and uppercase letters

An increasing number of web services are requiring both case. Currently, I think this is the most ideal.

JavaScript 


/^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z\d]{8,100}$/
# /^(?=[A-Z\d]{0,99}[a-z])(?=[a-z\d]{0,99}[A-Z])(?=[a-zA-Z]{0,99}\d)[a-zA-Z\d]{8,100}$/

HTML Pattern Attribute


"^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z\d]{8,100}$"
# "^(?=[A-Z\d]{0,99}[a-z])(?=[a-z\d]{0,99}[A-Z])(?=[a-zA-Z]{0,99}\d)[a-zA-Z\d]{8,100}$"

Ruby


/\A(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z\d]{8,100}+\z/
# /\A(?=[A-Z\d]{0,99}+[a-z])(?=[a-z\d]{0,99}+[A-Z])(?=[a-zA-Z]{0,99}+\d)[a-zA-Z\d]{8,100}+\z/

PHP


'/\A(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z\d]{8,100}+\z/'
# '/\A(?=[A-Z\d]{0,99}+[a-z])(?=[a-z\d]{0,99}+[A-Z])(?=[a-zA-Z]{0,99}+\d)[a-zA-Z\d]{8,100}+\z/'

Python


'\A(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z\d]{8,100}\Z'
# '\A(?=[A-Z\d]{0,99}[a-z])(?=[a-z\d]{0,99}[A-Z])(?=[a-zA-Z]{0,99}\d)[a-zA-Z\d]{8,100}\Z'

Regular expression of 8 to 100 characters including one or more half-width alphanumeric characters and symbols

I wonder if this is still so popular ...

JavaScript 


/^(?=.*?[a-z])(?=.*?\d)(?=.*?[!-\/:-@[-`{-~])[!-~]{8,100}$/i
# /^(?=[!-@[-`{-~]{0,99}[a-z])(?=[!-\/:-~]{0,99}\d)(?=[a-z\d]{0,99}[!-\/:-@[-`{-~])[!-~]{8,100}$/i

HTML Pattern Attribute


"^(?=.*?[a-zA-Z])(?=.*?\d)(?=.*?[!-/:-@[-`{-~])[!-~]{8,100}$"
# "^(?=[!-@[-`{-~]{0,99}[a-zA-Z])(?=[!-/:-~]{0,99}\d)(?=[a-z\d]{0,99}[!-/:-@[-`{-~])[!-~]{8,100}$"

Ruby


/\A(?=.*?[a-z])(?=.*?\d)(?=.*?[!-\/:-@\[-`{-~])[!-~]{8,100}+\z/i
/\A(?=.*?[a-z])(?=.*?\d)(?=.*?[!-~&&[^a-z\d]])[!-~]{8,100}+\z/i
%r#\A(?=.*?[a-z])(?=.*?\d)(?=.*?[!-/:-@\[-`{-~])[!-~]{8,100}+\z#i
# /\A(?=[!-@\[-`{-~]{0,99}+[a-z])(?=[!-\/:-~]{0,99}+\d)(?=[a-z\d]{0,99}+[!-\/:-@\[-`{-~])[!-~]{8,100}+\z/i
# %r#\A(?=[!-@\[-`{-~]{0,99}+[a-z])(?=[!-/:-~]{0,99}+\d)(?=[a-z\d]{0,99}+[!-/:-@\[-`{-~])[!-~]{8,100}+\z#i

PHP


'/\A(?=.*?[a-z])(?=.*?\d)(?=.*?[!-\/:-@[-`{-~])[!-~]{8,100}+\z/i'
'#\A(?=.*?[a-z])(?=.*?\d)(?=.*?[!-/:-@[-`{-~])[!-~]{8,100}+\z#i'
# '/\A(?=[!-@[-`{-~]{0,99}+[a-z])(?=[!-\/:-~]{0,99}+\d)(?=[a-z\d]{0,99}+[!-\/:-@[-`{-~])[!-~]{8,100}+\z/i'
# '#\A(?=[!-@[-`{-~]{0,99}+[a-z])(?=[!-/:-~]{0,99}+\d)(?=[a-z\d]{0,99}+[!-/:-@[-`{-~])[!-~]{8,100}+\z#i'

Python


'\A(?=.*?[a-z])(?=.*?\d)(?=.*?[!-/:-@[-`{-~])[!-~]{8,100}\Z(?i)'
# '\A(?=[!-@[-`{-~]{0,99}[a-z])(?=[!-/:-~]{0,99}\d)(?=[a-z\d]{0,99}[!-/:-@[-`{-~])[!-~]{8,100}\Z(?i)'

Recommended Posts

By language: Regular expressions for passwords
[Language processing 100 knocks 2020] Chapter 3: Regular expressions
3.6 Text Normalization 3.7 Regular Expressions for Tokenizing Text
100 Language Processing Knock 2020 Chapter 3: Regular Expressions
[Python] Regular Expressions Regular Expressions
100 Language Processing Knock Regular Expressions Learned in Chapter 3
Python pandas: Search for DataFrame using regular expressions
100 natural language processing knocks Chapter 3 Regular expressions (first half)
Faker summary by language
100 natural language processing knocks Chapter 3 Regular expressions (second half)
Compare how to write processing for lists by language
Language prediction model by TensorFlow
Use regular expressions in C
Use regular expressions in Python
Extract numbers with regular expressions
About Python and regular expressions