re module -----> Regular Expressions

#_author: Star 
#date: 2019/11/8
.. 1 # [] characters
Import Re
RET = the re.findall ( 'A [C, D, E] B', 'AEB')
Print (RET) #
RET1 = the re.findall ( '[AZ]', 'AXB')
Print (RET1) # [ 'a', 'X', 'B']
# [] characters ------> special function cancel metacharacters \ ^ -
RET2 the re.findall = ( '[m, *]', 'axmb *')
Print (RET2) # [ 'm', '*']
RET3 the re.findall = ( '[m ,,]', 'axmb *,')
Print (RET3) # [ 'm', ',']
# (. 1) -
ret4 the re.findall = ( '[1-9a-zA-the Z]', 'axmb55623GHGSJCSH,')
Print ( ret4) # [ 'a', 'x', 'm', 'b', '5', '5', '6', '2', '3', 'G', 'H', 'G ',' S ',' J ','C ',' S ',' H ']
ret5 the re.findall = (' [1-9, AZ, AZ] ',' axmb55623GHGSJCSH, ')
Print (ret5) # [' A ',' X ',' m ',' b ',' 5 ',' 5 ',' 6 ',' 2 ',' 3 ',' G ',' H ',' G ',' S ',' J ',' C ', 'S', 'H', ',']
# (2) ^ inside [] means negated
= the re.findall ret6 ( '[^ F]', 'kdfjdhferw')
Print (ret6) # [ 'K', 'D', 'J', 'D', 'H', 'E', 'R & lt' , 'W']
ret7 the re.findall = ( '[3,8 ^]', 'kd3fjdhfe8rw')
Print (ret7) # [ 'K', 'D', 'F', 'J', 'D', 'H', 'F', 'E', 'R & lt', 'W']
# (. 3) \


#. 3 |. pipeline breaks

. # 2 \ backslash
# backslash character removed with special features membered
# Common backslash characters with special functions implemented (as 2-1,2-18)
# (2-1) \ D ---> matches any decimal number, equivalent to the class [0,9]
ret8 = the re.findall ( '\. 11 D {}', 'gdsdhjsdjsjshd2347652987178628912678')
Print (ret8) # [ '23,476,529,871', '78,628,912,678']
# (2-2) \ D ---> matches any non-numeric characters, equivalent to the class [^ 0,9]
ret9 the re.findall = ( '\. 11 D {}', 'gdsdhjsdjsjshdokjiygdhy2347652987178628912678 ')
Print (ret9) # [' gdsdhjsdjsj ',' shdokjiygdh ']
# (2-3) \ S ---> matches any whitespace character, equivalent to the class [DSSD]
ret10 the re.findall = (' \ SStar ' , 'dgsh star')
Print (ret10) # [ 'Star']
# (2-4) \ S ---> matches any non-blank character, equivalent to the class [^ DSSD]
ret11 the re.findall = ( '\ S', 'dgshstar')
Print (ret11) # [ 'D', 'G', 'S', 'H', 'S', 'T', 'A', 'R & lt']
ret11_ the re.findall = ( '\ SStar', ' dgshstar ')
Print (ret11 _) # [' HSTAR ']
# (2-5) \ W ---> matches any alphanumeric character, equivalent to the class [A-zA-Z0-9]
ret12 the re.findall = (' \ wstar ',' dgshstar ')
Print (ret12) # [' HSTAR ']
ret13 the re.findall = (' \ wstar ',' dgsh Star ')
Print (ret13) # []
# (2-5) \ W- -> matches any non-alphanumeric character, equivalent to the class [^ A-zA-Z0-9]
ret14 the re.findall = ( '\ W is', 'S $ D] S @ -d_m')
Print (ret14) # [ '$', ']', '@', '-']
ret15 = Re.findAll ( '\ W is [$, #]', 'SG $ H # S $ #')
Print (ret15) # [ '$ #']
# (2-6) \ B ---> matches a special character a boundary
ret16 = re.findall (r'I \ b ' ,' hello I am a DI, CT ')
Print (ret16) # [ 'the I', 'the I']
ret17 the re.findall = (R & lt '\ bI', 'Hello% the ICT the I AM A D')
Print (ret17) # [ 'the I', 'the I']
Print ( '--------------------------------------------')
# matched result meets a first condition
ret18 the re.search = ( 'ST', 'ihsdjwstghubustbjc')
Print (ret18) # <re.match Object; span = (. 6,. 8), match = 'ST'>
Print ( ret18.group ()) ST #

# # membered backslash character with special features removed
RE1 = the re.search ( 'A \.', 'a.hg'). Group ()
Print (RE1) # A.
RE2 the re.search = ( 'a \ +', 'Hg + a') Group ().
Print (RE2) # + a
# first phenomenon
# rosy, there are two cases: one of the top two because (1) with four \\\\ two (2) R & lt '\\'
RE3 the re.findall = ( '\\', 'ugdhsdhs \ C')
Print (RE3) # [ '\\']
RE4 = the re.findall (R & lt '\\', 'ugdhsdhs \ C')
Print (RE4) # [ '\\']
# second phenomenon
re5=re.search('\bblue','blue')
print(re5)# None
re5_=re.search(r'\bblue','blue')
print(re5_)# <re.Match object; span=(0, 4), match='blue'>
print(re5_.group())# blue
print('---------------------------')
# 4. ()----->做分组 | 管道符 ---->或(靠前)
print(re.search('(as)+','ggubeasas').group())# asas
print(re.search('go|8','go').group())# go
print(re.search('go|8','8go').group())# 8

print('---------------------------')
re1=re.search('(?P<id>\d{3})/(?P<name>\w{3})','dwdwdwdw56yy456/ppp')
print(re1.group())# 456/ppp
print(re1.group('id')) # 456
print(re1.group('name'))# ppp





Guess you like

Origin www.cnblogs.com/startl/p/11818931.html