Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Python regular expression example usage

Shulou Source: shulou.com Published: 2022-06-02 05:14:53 10月18日 Update

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)

Tags: Character string regular expression password instance function parameter input method password number at the same time uppercase object lowercase idea condition format length Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Xiaomi Linux vpn Docker