Article From:https://www.cnblogs.com/neilyoung22/p/9218262.html
 1 # A regular expression is a way to match a string
 2 # String itself has matching methods. Why do we need to introduce regular expressions? Because the original string matching does not come out of the original method.
 3 # A regular match is used to make a fuzzy match
 4 
 5 s = "alex want to drink"
 6 print(s.split(" ")) # By intimidation
 7 print(s.find("a"))  # -1
 8 
 9 
10 import re
11 # 。 Fuzzy matches a character or digit or space, but cannot match the line break symbol.
12 # ^  Head matching
13 # $  End match
14 # *  Match the front 0 to more characters: 0 is also a 0, or no return an empty character.
15 # +  Match the previous 1 to multiple characters
16 # ? The characters in front of the characters that match the first 0 to 1 times may not be, or only one.
17 # {} Specifies the number of times to match the {5} or the character before the {1,5} matches 5 times'aaaaaab'.
18 #    (The default is greedy matching, according to the most or least.
19 
20 # findall() Method completely matches the Hello return list
21 pp = re.findall("h...o","asdadasdashelloasdasdasd")
22 pp2 = re.findall("^h...o","asdadasdashelloasdasdasd")   #Unsuccessful match
23 pp3 = re.findall("h*","asdashehllohhhhhhasdas")   #The matches were unsuccessful [, ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',' ',' '.''H'].'H'.
24 print(pp3)
 1 import re
 2 #Two character character
 3 # [ ] A character set matches a given range of elements or relationships.
 4 # [ ] Cancel the special function of the meta character, such as * is regarded as a common character x instead of multiple characters, but ^ - these characters are exceptions!
 5 # [^ ] ^In [] is the meaning of taking the opposite!
 6 # [^4,5] The meaning of this is 4 or 5.
 7 # \ The following meta characters will cancel their special functions, but follow the ordinary individual characters to achieve special functions. \w represents a letter.
 8 # As shown below:
 9 # \d  Matches any decimal number; it is equivalent to class [0-9].
10 # \D Matches any non numeric character; it is equivalent to class [^0-9].
11 # \s  Matches any blank character; it is equivalent to class [\t\n\r\f\v].
12 # \S Matches any non blank character; it is equivalent to class [^ \t\n\r\f\v].
13 # \w Matches any alphanumeric character; it is equivalent to class [a-zA-Z0-9_].
14 # \W Matches any non alphanumeric character; it is equivalent to class [^a-zA-Z0-9_].
15 # \b  Matches a special character border, such as space, &, and so on.
16 # \\ In the special character regular expression, the escape symbol will go through the interpreter of python, then the Python interpreter needs to explain again: \ \ \ \ 4 characters.
17 
18 # () The rules in parentheses match as a whole.
19 # | The meaning of a pipe symbol.
20 
21 #Method two: x=re.search () finds a return and returns an object to get the value of this object. Then f.group () gets the value it finds.
22 
23 a=re.findall('a[b,v]c','abc')
24 a=re.findall('[a,z]','abc')     # This will return 3 results to a B C
25 print(a)
26 c = re.findall(r"I\b","hello,I am hero I am OK")    # Pay attention to the transfer symbol
27 print(c)
28 
29 # \\ The use of transfer symbols should be understood because the regular expressions need to be passed through the Python interpreter after they escape.
30 # Way 1:
31 n = re.search(r'\\c','asdasd\cf')
32 print(n.group())
33 # Mode two:
34 v = re.search('\\\\c','asdasd\cf')
35 print(v.group())
36 
37 #()With the way of use
38 #()Using the upgraded version of the complex version, see the following example regularization and value based on the group name.
39 print(re.search("(as)|3","as3").group())      #as  Pay attention to only one value
40 # Advanced Edition
41 # <> Is a regular form in a regular expression
42 ret=re.search('(?P<id>\d{2})/(?P<name>\w{3})','23/com')
43 print(ret.group())#23/com
44 print(ret.group('id'))#23
 1 # The method of regular expression
 2 
 3 re.findall()
 4 re.search().group()
 5 re.match()              #It only returns at the beginning of the string matching and returns an object successfully. The match fails to return to none and returns only one object.
 6 re.split()              #The focus is to make mistakes here.
 7 re.sub()                #The way that the replace replacement is very good
 8 re.compile()            #Compile rules into one object to save code usage.
 9 
10 
11 # This is grouped according to K first, then grouped ['a','d','dl','op'] again according to s's grouping of K.
12 print(re.split('[k,s]','asdkdlsop'))
13 # Notice where mistakes are made. S will have a null character segmented at the beginning of segmentation.
14 print(re.split('[k,s]','sasdkdlsop'))
15 print(re.sub('a..x','s..b','asdalexppm'))       #asds..bppm
16 
17 #re.compile() Use
18 
19 pp=re.compile('/.com')
20 ret=pp.findall('asdadd.com')
21 print(ret)
1 import re
2 #Iterator objects
3 ret = re.finditer("\d","asdasda2adsd")
4 #print(next(ret))
5 print(next(ret).group())

 

Link of this Article: The re module of the python module

Leave a Reply

Your email address will not be published. Required fields are marked *