Wish you all a happy new year in advance , This article should be the last article before the Spring Festival ~
A regular expression is a special sequence of characters , It can help you easily check whether a string matches a certain pattern ,Python
Mainly through re
Modules use regular expressions .
This article does not introduce the syntax of regular expressions itself , Just introduce Python
Using regular expressions .
Use re.search
function , Scan the entire string and return the first successful match .
re.search(pattern, string, flags=0)
The meaning of each parameter is as follows :
pattern
: Enter the regular expressionstring
: String to match .flags
: Sign a , Used to control how regular expressions are matched , Such as : Is it case sensitive , Multi line matching and so on .
If re.search
Method matching succeeded , Then return a matching object ; Otherwise return to None
. Let's illustrate with a simple example :
import re result = re.search("bitpy", "www.bitpy.com") print(result)
The output is as follows :
<re.Match object; span=(4, 9), match='bitpy'>
In the previous section, we got the matching result object ( namely re.MatchObject
object ), The corresponding information can be extracted according to the actual needs
import re result = re.search("bitpy", "www.bitpy.com") print(result.span())
The output is as follows :
(4, 9)
besides , also start()
Function returns the starting position of the match ;end()
Function returns the position where the match ends .
If you need to extract key substrings , You can add parentheses to regular expressions , Then the matching result is passed group
Function extraction .
import re result = re.search(r"(.*)\.(.*)\.(.*)", "www.bitpy.com") if result: print(result.groups()) print(result.group(0)) print(result.group(1)) print(result.group(2)) print(result.group(3))
Note the second line , Precede quotation marks with letters
r
, Indicates that this string is a regular expression .
The output is as follows :
('www', 'bitpy', 'com') www.bitpy.com www bitpy com
re.match
Try to match a pattern... From the beginning of the string , If the match is successful , return re.MatchObject
object ; Otherwise return to none
.
re.match(pattern, string, flags=0)
Illustrate with a simple example :
import re result_1 = re.match(r"bitpy", "bitpy.com") result_2 = re.match(r"(.*).com", "bitpy.com") if result_1: print(result_1.span()) if result_2: print(result_2.groups())
Be careful ,
re.match
And back againre.MatchObject
object , therefore , It can also be done throughspan
、group
Wait for the function to obtain the corresponding information .
The output is as follows :
(0, 5) ('bitpy',)
Regular expressions can contain optional flag modifiers to control the matching pattern . The modifier is specified as an optional flag . Multiple flags can be passed by biting OR(|)
They specify . Such as re.I | re.M
Is set to I
and M
sign :
Modifier
describe
re.I
Make match match case insensitive
re.L
Do localization identification (locale-aware
) matching
re.M
Multi-line matching , influence ^
and $
re.S
send . Match all characters including line breaks
re.U
according to Unicode Character set parsing characters . This sign affects \w
, \W
, \b
, \B
.
re.X
This flag allows you to write regular expressions more easily by giving you a more flexible format .
Welcome to follow me 【Python Learning from actual combat 】, Learn a little every day , Make a little progress every day .