First, regular expression
The regular expression (regular expression) describes a pattern of string matching that can be used to check whether a string contains a seed string, replace a matched substring, or remove a substring from a string that matches a certain condition. The regular expression has two parts parameter, one is the match rule pattern, the other is to match the data source, Data_source, needs to import the module Re,import re when using the regular expression
import rem = Re.match (" abc ", " abcdef " ) # where ABC is pattern,abcdef for Data_source,re.match (pattern, Data_source) method, # from DA Ta_source to the left of the match, if the match on the return of an object and stop the match, that can only match once, matching does not return to the none print (m) if m: Span style= "COLOR: #0000ff" >print (M.group ()) # Obj.group () method to see what matches to
<_sre. Sre_match object; Span= (0, 3), match= ' abc ' >
Abc
But the above method of pattern writing fixed, can only match ABC is not flexible, and can only match once
Importrem1= Re.findall ('[0-9]{0,10}','146567abc576def')#where [0-9]{0,10} is pattern,[0-9] represents a matched number of 0-9, #{0,10} represents the type defined above can be matched for the same 0-10, can write a number to indicate the number of matches #146567abc576def is the Data_source,re.match (Pattern,data_source) method, #match the Data_source matching rules to all of the sub-characters to return a list, #{0,10} exists 0, the returned list will appear empty, usually starting from 1, for example {1,10}M2 = Re.findall ('[0-9]{1,10}','146567abc576def') M3= Re.findall ('[a-za-z]{1,10}','146567abc576def')#[A-za-z] represents a match for all letters, case can beM4 = Re.findall ('.*','146567abc576def')#where. The delegate can match all characters except the newline character, * represents 0 to more functions similar to {0,10}M5 = Re.findall ('.+','146567abc576def')#where + represents 1 to more features similar to {1,10}M6 = Re.search ('\d+','146567abc576def')#Re.search () method that matches from the left of the data_source until a match is found and then ends, #or find the data_source have not found the end, where the \d representative matches any number, similar to [0-9], #The \d representative matches any non-numeric, \s represents any whitespace character, and \s matches any non-null character . #\w matches alphanumeric, \w matches non-alphanumericM7 = Re.search ('^\d+','146567abc576def')#where ^ represents the data_source from the first character of the request to match the rule, otherwise can not find Nonetype, #$ represents the last character from data_source to match the ruleM8 = Re.sub ('\d+','-','146567abc576def')#Re.sub (pattern,str,data_source,count=) method, starting from the left of Data_source, #Replace the matched substring with the set Str,count=num to specify the number of replacements, replace all by defaultifM1orM2orM3orM4orM5orM6orM7orM8:#print (M.group ()) # Re.findall No Obj.group () method, use print (obj) to view the matched content directly Print(M1)Print(m2)Print(m3)Print(M4)Print(M5)Print(M6.group ())#to view the substring that is matched in Re.search () Print(M7.group ())Print(M8)
[' 146567 ', ', ', ', ', ' 576 ', ', ', ', ', ']
[' 146567 ', ' 576 ']
[' abc ', ' Def ']
[' 146567abc576def ', ']
[' 146567abc576def ']
146567
146567
-abc-def
Python Learning (iii)