re module (regular expression)

1  import re
 2  
3  print (re.findall( ' s...t ' , ' rhjioestastgih ' )) #One . Represents matching a character 4 5 # * Matching 0 to infinite times (0,+00) means not Match at least 0, but a character before * can not match 6 #Example # ['sk'] 7 print (re.findall( ' skt* ' , ' skeriuhgioerg ' ))
 8 9 # + match 1 time to infinity Times (1,+00) means that it does not match at least 1, but all characters before + must match 10 #  Example#[] 11 print (re.findall(
 
 
 
  
 
 
 'skt+','skeriuhgioerg'))
12 
13 print(re.findall('stc*','uwhdefoistttttt'))     #['st']
14 print(re.findall('stc+','uwhdefoistttttt'))   #[]
15 
16 print(re.findall('st{0,1}','eriufhstfkjh'))
17 print(re.findall('stc*','uwhdefoistttttt'))     #['st']
18 print(re.findall('stc+','uwhdefoistttttt'))   #[]
19 
20 ret=re.findall('abc?','abccc')#[0,1]
21 print(ret)#['abc']
22 
23 print(re.findall('stt*?','stttttttttt' ))     #Lazy matching ['st'] 
24  print (re.findall( ' stt +? ' , ' stttttttttt ' ))     #Lazy matching ['stt'] 
25  
26  print (re.findall( ' skt? ' , ' iuhskuirh ' ))    # ?[0,1] The previous one can not match, only one match if there is a match #['sk'] 
27  print (re.findall( ' skt? ' , ' iuhsktttuirh ' ))     # ['skt '] 
28  
29  
30ret=re.findall( ' abc{1,4} ' , ' abccccccc ' )   # The last digit of {x,y} can be matched at least x times and at most y times 
31  print (ret) # ['abccc'] Greedy matching 
32  
33  print (re.findall( ' [0-9] ' , ' qq5ds5sg1r56ee2g ' ))        # [0-9] means any number up to 0123456789, or 
34  print (re.findall( ' [0 -9]* ' , ' qq55g876h5 ' ))
 35  print (re.findall( ' [az]' , ' qqa ' ))
 36  print (re.findall( ' [AZ]* ' , ' qqSOIGNIOSDF ' ))
 37  print (re.findall( ' [^AZ]* ' , ' qqS1O23IGNIOSDF ' ))       # [^] Indicates that non-reversed does not contain 
38  
39  # # The escape character of meta-characters \ 
40  # # The backslash is followed by meta-characters to remove special functions, such as \. 
41  # # The backslash is followed by ordinary characters to realize special functions, such as \ d 
42  # # 
43  # # \d matches any decimal number; it is equivalent to the class [0-9]. 
44  ## \D matches any non-numeric character; it is equivalent to the class [^0-9]. 
45  # # \s matches any whitespace character; it is equivalent to the class [ \t\n\r\f\v]. 
46  # # \S matches any non-whitespace character; it is equivalent to the class [^ \t\n\r\f\v]. 
47  # # \w matches any alphanumeric character; it is equivalent to the class [a-zA-Z0-9_]. 
48  # # \W matches any non-alphanumeric character; it is equivalent to the class [^a-zA-Z0-9_] 
49  # # \b matches a special character boundary such as space, &, #, etc.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325164835&siteId=291194637