1 #Regular expressions are methods used to match strings2 #The string itself has a matching method, why introduce a regular expression? Because the original string does not match the primitive method3 #a regular match is used to make a fuzzy match.4 5s ="Alex want to drink"6 Print(S.split (" "))#to intimidate7 Print(S.find ("a"))#-18 9 Ten ImportRe One #. Blur matches a character or a number or a space, but does not match the line break symbol A #^ beginning of match - #$ end Match - #* Match front 0 to multiple characters Note: 0 is also a 0 is not also return a null character the #+ match front 1 to more characters - #? Characters that match the preceding 0 to 1 characters may not be there, or there is only one - #{} Specifies the number of occurrences of the range {5} or characters in front of {1,5} to match 5 times ' Aaaaaab ' characters that can be taken - #(the default is greedy match, by the most, or by the least) + - #the FindAll () method exactly matches the Hello return list +pp = Re.findall ("H...O","ASDADASDASHELLOASDASDASD") APP2 = Re.findall ("^H...O","ASDADASDASHELLOASDASDASD")#Match not successful atPP3 = Re.findall ("h*","Asdashehllohhhhhhasdas")#match unsuccessful [' ', ' ', ' ', ' ', ' ', ' h ', ' ', ' h ', ' ', ' ', ' ', ' hhhhhh ', ', ', ', ', ' - Print(PP3)
1 ImportRe2 #two meta characters3 #[] A character set that matches the relationship of one element within a given range4 #[] The special function of canceling meta-characters such as * is treated as a normal character x and does not match multiple characters but ^ \-these characters are the exception! 5 #[^] ^ in [] is to take the meaning of the inverse! 6 #[^4,5] here means except for 4 or and except 57 #\ followed by the meta-character will cancel its special function, but followed by ordinary individual characters can be implemented special functions \w represents a letter8 #is as follows:9 #\d matches any decimal number; it is equivalent to class [0-9]. Ten #\d matches any non-numeric character; it is equivalent to class [^0-9]. One #\s matches any whitespace character; it is equivalent to class [\t\n\r\f\v]. A #\s matches any non-whitespace character; it is equivalent to class [^ \t\n\r\f\v]. - #\w matches any alphanumeric character; it is equivalent to class [a-za-z0-9_]. - #\w matches any non-alphanumeric character; it is equivalent to a class [^a-za-z0-9_] the #\b Matches a special character boundary, such as a space, &,#, etc. - #\ \ Special Word regular the escape symbol in the expression \ \ will go through the Python interpreter, then the Python interpreter needs to explain again that \\\\ becomes 4 characters - - #() The rules within parentheses as a whole to match the + #| The meaning of a pipe symbol or - + #method Two: X=re.search () find a return, return is an object to get the value of this object F.group () Gets the value found A atA=re.findall ('A[b,v]c','ABC') -A=re.findall ('[A,z]','ABC')#This will return 3 results for a b c, respectively . - Print(a) -c = Re.findall (r"i\b","hello,i am Hero I am OK")#Note Escape symbols - Print(c) - in #to understand the use of the transfer symbol, you need to go through the Python interpreter and then escape it once the regular expression is escaped. - #Way One: ton = Re.search (r'\\c','ASDASD\CF') + Print(N.group ()) - #Way Two: thev = re.search ('\\\\c','ASDASD\CF') * Print(V.group ()) $ Panax Notoginseng #() and | The way to use - #() and | The use of the upgraded version of the complex version see the following example regular and value based on the group name the Print(Re.search ("(AS) |3","AS3"). Group ())#As note there is only one value + #Premium Edition A #<> is a fixed format in regular expressions theRet=re.search ('(? P<ID>\D{2})/(? P<NAME>\W{3})','23/com') + Print(Ret.group ())#23/com - Print(Ret.group ('ID'))# at
1 #methods of regular Expressions2 3 Re.findall ()4 Re.search (). Group ()5Re.match ()#only when the string starts matching matches successfully returns an object, and the match does not return only one object if none is returned successfully6Re.split ()#the point here is to make a mistake .7Re.sub ()#equivalent to replacing replace with a good method8Re.compile ()#compile the rules into an object to invoke, saving the amount of code used9 Ten One #This is grouped by K first and then grouped in accordance with S-K grouping again [' A ', ' d ', ' DL ', ' op '] A Print(Re.split ('[K,s]','Asdkdlsop')) - #Note the error-prone place where s at the beginning of the split will have a blank character being split - Print(Re.split ('[K,s]','Sasdkdlsop')) the Print(Re.sub ('A.. X','S.. b','asdalexppm'))#ASDs. BPPM - - #use of Re.compile () - +Pp=re.compile ('/.com') -Ret=pp.findall ('asdadd.com') + Print(ret)
The re module of the Python module