One 、 Overview of regular expressions
Regular expressions are special sequences of characters , Often used for retrieval 、 Replace a pattern ( Having certain rules ) Some of the text
Registered app, Set the username 、 There are rules for setting passwords
Regular expressions can be used in almost all programming languages , Regular expressions have their own independent syntax and processing engine . For some operations of string, if the native string can complete , That is to use the original , Because native processing is more efficient than regular expressions
Native ones can't be handled or are rather verbose , You can use regular expressions
Python Provides modules for processing expressions , The module is called re
Two 、 Usage scenarios of regular expressions
Inspection operation : Like username 、 password 、 cell-phone number 、 Verification of mailbox, etc
Search query : To query the content in the specified format in a piece of text , Such as extracting the mobile phone number
3、 ... and 、 Regular grammar
explain :
If the character to be matched is a special character in a regular expression , Then you can use \ Escapes , For example, if you want to match the decimal point, you can write . That's all right. , Because write directly . Will match any character ; Empathy , To match parentheses, you must write ( and ), Otherwise, parentheses are treated as groupings in regular expressions .
Four 、Python Support for regular expressions
Python Provides re Module to support regular expression related operations , Here is re Core functions in modules .
explain :
As mentioned above re These functions in the module , Regular expression objects can also be used in practical development (Pattern object ) Instead of using these functions , If a regular expression needs to be used repeatedly , Then go through compile Function to compile regular expressions and create regular expression objects is undoubtedly a wiser choice .
# One 、 Find websites ##( One