Author:AXYZdong Automation Engineering Male A little bit of thinking , Have a little idea , A little bit rational !
Set a small goal , Try to be a habit ! Meet a better self in the most beautiful years !
Regular expressions , abbreviation regex , Is a description of the text .
When writing programs or web pages that handle strings , There is often a need to find strings that meet certain complex rules , Regular expressions are tools for describing these rules , let me put it another way Regular expressions are a tool , It defines the matching pattern of strings ( How to check whether a string has a part that matches a pattern, or extract or replace the part that matches a pattern from a string ).
for example ,\d Is a regular expression , Represents a digit character , Any one of them 0 To 9 The number of .
Symbol
explain
Example
explain
.
Match any character
b.t
Can match bat / but / b#t / b1t etc.
\w
Match the letter / Numbers / Underline
b\wt
Can match bat / b1t / b_t etc. But can't match b#t
\s
Matching blank character ( Include \r、\n、\t etc. )
love\syou
Can match love you
\d
Match the Numbers
\d\d
Can match 01 / 23 / 99 etc.
\b
Match the boundaries of words
\bThe\b
^
Matches the beginning of the string
^The
Can match The Starting string
$
Matches the end of the string
.exe$
Can match .exe a null-terminated string
\W
Match nonletter / Numbers / Underline
b\Wt
Can match b#t / [email protected] etc. But can't match but / b1t / b_t etc.
\S
Match non white space characters
love\Syou
Can match love#you etc. But can't match love you
\D
Match non numeric
\d\D
Can match 9a / 3# / 0F etc.
\B
Match non word boundaries
\Bio\B
[]
Matches any single character from the character set
aeiou
Can match any vowel character
^
Matches any single character that is not in the character set
^aeiou
Can match any non vowel character
matching 0 Times or times
\w*
matching 1 Times or times
\w+
?
matching 0 Time or 1 Time
\w?
{N}
matching N Time
\w{3}
{M,}
Match at least M Time
\w{3,}
{M,N}
Match at least M Times at most N Time
\w{3,6}
|
Branch
foo|bar
Can match foo perhaps bar
(?#)
notes
(exp)
matching exp And capture it into an automatically named group
(?<name>exp)
matching exp And capture a file named name In the group of
(?:exp)
matching exp But do not capture matching text
(?=exp)
matching exp Front position
\b\w+(?=ing)
Can match I'm dancing Medium danc
(?<=exp)
matching exp The back position
(?<=\bdanc)\w+\b
Can match I love dancing and reading The first of ing
(?!exp)
After matching, it's not exp The location of
(?<!exp)
Not before match exp The location of
*?
Repeat any number of times , But repeat as little as possible
a.b a.?b
Apply regular expressions to aabab, The former will match the entire string aabab, The latter will match aab and ab Two strings
+?
repeat 1 Times or times , But repeat as little as possible
??
repeat 0 Time or 1 Time , But repeat as little as possible
{M,N}?
repeat M To N Time , But repeat as little as possible
{M,}?
repeat M More than once , But repeat as little as possible
Regular expression test site : http://regexpal.com/
Use regular expressions to find phone numbers :
>>> import re >>> phoneNumRegex = re.compile (r'\d\d\d-\d\d\d-\d\d\d\d') # establish Regex object ( Use original string , Simplify writing ) >>> mo = phoneNumRegex.search ('My number is 515-345-7890') # utilize search Method to pass in the string you want to find >>> print('Phone number found :' + mo.group ()) # call March Object's group() Method , Returns the string that actually matches the text Phone number found :515-345-7890
reference
1:https://github.com/jackfrued/Python-100-Days
2:Python Programming fast : Automate tedious work / ( beautiful ) Svegart (A1 Sweigart) Writing ; Translated by Wang Haipeng . Beijing : People's post and Telecommunications Press ,2016.7
3:Python Chinese guide ; author : Wang Bingming , edition :v1.0
This sharing is here
A good book is worth reading a hundred times , Read it well and know what it means . Make learning a habit , Use knowledge to change fate , Let the blog witness the growth , Prove your efforts with your actions .
If any of the above is inaccurate , Welcome below Leave a message . Or you have a better idea , Welcome to exchange and study together ~~~