ImportRest='Asdfasxxixxdafqewxxlovexxsadawexxyouxxas'# .#Point match any character except line breaka0 = Re.findall ('xx.', ST)#Print (A0)#[' XXI ', ' xxd ', ' XXL ', ' xxs ', ' xxy ', ' xxa ']A1= Re.findall ('xx..', ST)#Print (A1)#[' xxix ', ' xxlo ', ' xxsa ', ' xxyo ', ' Xxas ']# *#star matches one or more of the preceding charactersB0 = Re.findall ('x*', ST)#print (B0)#[', ', ', ', ' ', ', ', ' xx ', ' ', ' xx ', ' ', ' ', ' ', ', ' ', ' ', ' xx ', ' ', ' ', ' ', ' ' xx ', ', ', ', ', ', ' xx ', ', ', ', ' xx ', ', ', ', '#? #question mark matches one of the preceding characters 0 or 1 timesC0 = Re.findall ('x?', ST)#print (C0)#[', ', ', ', ', ', ' ', ', ' x ', ' X ', ', ' x ', ' X ', ', ', ', ', ', ', ', ' x ', ' X ', ', ', ', ', ', ' x ', ' X ', ', ', ', ' ', ', ' ', ' ', ' x ', ' X ', ', ', ' ', ' x ', ' x ', ' , ', ', '#greedy algorithm (Point Star) as many matches as possible--. *#match string as long as possibleA = Re.findall ('xx.*xx', ST)#print (a)#Run Results#[' Xxixxdafqewxxlovexxsadawexxyouxx ']#Non-greedy algorithm (Point star question mark) less food and more meals--. *?#Return to listb = Re.findall ('xx.*?xx', ST)#print (b)#Run Results#[' Xxixx ', ' Xxlovexx ', ' xxyouxx ']#Non-greedy algorithm (Point star question mark) less food and more meals--. *?#Return to listc = Re.findall ('xx (. *?) XX', ST)#print (c)#Run Results#[' I ', ' love ', ' You ']#Re. S matches line breakSS=" "Asdfasxxixxdafqewxxlovexxsadawexxyouxxas" "D= Re.findall ('xx (. *?) XX', SS) e= Re.findall ('xx (. *?) XX', Ss,re. S)Print("no re. S:%s\n has re. s:%s"%(d,e))#Run Results#no re. s:[' i ', ' Sadawe ']#there is re. s:[' i ', ' love\n ', ' You ']
Greedy algorithm, non-greedy algorithm
Python regular expression 01--greedy algorithm and non-greedy algorithm findall ()