Python regular expression example usage
This article mainly explains the "python regular expression example usage", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "python regular expression example usage" bar!
Example 1: strong password detection
Topic: write a function that uses regular expressions to ensure that the incoming password string is a strong password. A strong password is defined as not less than 8 characters long, containing both uppercase and lowercase characters, and at least one digit. You may need to test the string with multiple regular expressions to ensure its strength.
Train of thought:
1. The program needs to write a Python function to test the string.
2. Regular expressions are needed to determine whether a string meets condition 1 (not less than 8 characters in length), condition 2 (both uppercase and lowercase characters), and condition 3 (at least one digit).
Implementation: this article provides three ways to write:
1 、
Import re
Def strongPassword (str): # defines regular expression functions
LengthRex1 = re.compile (r'[amurz] +') # create regular expression object 1
LengthRex2 = re.compile (r'[Amurz] +') # create regular expression object 2
LengthRex3 = re.compile (r'[0-9] +') # create regular expression object 3
If len (str)