1 #-*-coding:utf8-*-2 #Import re library file3 ImportRe4 fromReImportfindall,search,s5Secret_code ='HADKFALIFEXXIXXFASDJIFJA134XXLOVEXX23345SDFXXYOUXX8DFSE'6 7 #use an example to match any character, except for line break \ n8 #a = ' xy123 '9 #B = Re.findall (' x ... ', a)Ten #C = Re.findall (' x.. ', a) One #Print B A #Print C - - #* Use an example to match the previous character 0 or unlimited times the #a = ' xyxy123xx456xxx789 ' - #B = Re.findall (' x* ', a) - #Print B - #[' x ', ' ', ' x ', ' , ', ', ', ', ' xx ', ' ', ' ', ', ' xxx ', ', ', ', '] + - #use an example to match a previous character 0 or 1 times + #a = ' xy123 ' A #B = Re.findall (' x? ', a) at #Print B - - #the use of #.* for example greedy algorithm - #B = Re.findall (' xx.*xx ', Secret_code) - #Print Secret_code - #Print B in ## #.*? Examples of Use - #C = re.findall (' xx.*?xx ', Secret_code) to #Print C + - the ##使用括号与不使用括号的差别 * #d = Re.findall (' xx (. *?) xx ', Secret_code) $ #Print DPanax Notoginseng #For each in D: - #Print each the #Re. S match includes line break + #s = "' Sdfxxhello A #xxfsdfxxworldxxasdf "" the # + #d = Re.findall (' xx (. *?) xx ', S,re. S) #,re. S - #Print D $ $ #Compare the difference between findall and search - #s2 = ' ASDFXXIXX123XXLOVEXXDFD ' - #f = re.search (' xx (. *?) Xx123xx (. *?) xx ', S2). Group (2) the #Print F - #F2 = Re.findall (' xx (. *?) Xx123xx (. *?) xx ', s2)Wuyi #Print F2[0][1] the - #Replace the sub with an example Wu #s = ' 123rrrrr123 ' - #output = Re.sub (' 123 (. *?) 123 ', ' 123%d123 '%789,s) About #Print Output $ - #demonstrating the different import methods - #info = findall (' xx (. *?) xx ', secret_code,s) - #For each in info: A #Print each + the #do not use compile superfluous - #pattern = ' xx (. *?) XX ' $ #New_pattern = Re.compile (pattern,re. S) the #output = Re.findall (New_pattern,secret_code) the #Re1 = Re.findall (' xx (. *?) xx ', Secret_code,re. S) the #Print Output the #Print Re1 - in #Match Numbers the #a = ' Asdfasf1234567fasd555fas ' the ##b = Re.findall (' \d+ ', a) #[' 1234567 ', ' 555 '] About #B = Re.findall (' (\d+) ', a) the #Print B
Python-----Regular Expression Exercises