First, the Python regular introduction
Python's re module, allowing Python to support extended regular
Extended regular Character set ("." " [ABC] "(ABC)" "A|b"), Anchor Point ("$" "^" "\<\>"), modifier character ("*" "?") "+" "{}") do not introduce here
The functions in the second and re modules are mainly three matches (match, search, findall), a substitution (sub), a split (split)
1,match (apttern,string,flags=0)
Flag: Flags, used to control match matching, are not normally specified, default 0
Match is used to match the specified content from the starting position based on the model to the string, and if there is no match to the content, it returns none if the match to the content returns an Matchobject object
Use the group () and Groups () methods to access matching content
import restring = " 1234erqwe123 " obj =re.match ( \d+ ' , string) # Returns an object print print (Obj.group ()) # # <_sre. Sre_match object; Span= (0, 4), match= ' 1234 '; 1234
Import restring="@$1234erqwe123"obj1=re.match ('([^0-9]+) (\d +)', String'print(Obj1.group ()) #Group () returns a string Print (Obj1.groups ()) # groups () returns a tuple # @$1234 # (' @$ ', ' 1234 ')
2,search (Pattern, string, flags=0)
search and match are a distinction that matches the pattern that is not at the beginning of theline, but also the data that is matched through group () and groups ().
string="#$1234erqwe123"obj=re.search ('(\d+) ([^0-9]+) ] ', string' Print (obj.group ())print(obj.groups ())# 1234erqwe#(' 1234 ', ' erqwe ')
3,findall (pattern,string,flags=0)
Match and search matches only one pattern , and if you want to match all of the required pattern in a string , use FindAll ()
The FindAll function returns a list
Import restring="#$1234erqwe123"obj=re.findall ('\d+ ' , String) #FindAll function returns a list of print(obj)#[' 1234 ', ' 123 ']
4,sub (pattern, replaced part, string,n=0)
The sub is used to replace the specified string, which is richer than the string method replace
Importrestring="1234erqwe123adfa123"new_string=re.sub ('\d+','**', string)#returns the replaced copy, the original string unchangedNew_string2=re.sub ('\d+','**', string,2)#can be specified to replace the first n pattern, followed by a non-replacementPrint(String)Print(new_string)Print(new_string2)#1234erqwe123adfa123#**erqwe**adfa**#**erqwe**adfa123
5,split (Pattern,string,n)
Split is used to cut a string by a specified match, n means to divide by the first n pattern, and if n is empty, to return a list by all pattern cuts.
Split function is richer than string method Split function
Importrestring="1234erqwe123adfa123"new_string=re.split ('\d+', string,1)#Previous 1 pattern is a separatorNew_string1=re.split ('\d+', string,2)#Previous 2 pattern is a separatorNew_string2=re.split ('(\d+)', string,1)#If the pattern is enclosed in parentheses, the contents of the parentheses returnNew_string3=re.split ('(\d+)', string,2)#Print(new_string)Print(new_string1)Print(new_string2)Print(NEW_STRING3)#[' ', ' erqwe123adfa123 ']#[' ', ' erqwe ', ' adfa123 ']#[' ', ' 1234 ', ' erqwe123adfa123 ']#[', ' 1234 ', ' erqwe ', ' 123 ', ' adfa123 ']
Python Basics---Regular