0

About regular expression (re.findall problem)

import re pattern = r"egg(spam)*" string = "sinfhweggdifndheggspamdjdneggspamspamdhdn" #first print(re.findall(pattern,string)) #why the output is ['','spam','spam']??? #second n = re.finditer(pattern,string) for i in n: print(i.group()) #third n = re.finditer(pattern,string) print(list(map(lambda x: x.group(),n))) #second and third will give all that match, but why do i have to reassign n = re.finditer(pattern,string) in order to let the third has output, otherwise it will give empty[]. Somebody please help, thank you :) https://code.sololearn.com/coHHj7zSJ2Vh/?ref=app

30th May 2018, 11:56 AM
Ghight
1 Resposta
+ 1
#first - Your pattern matches 3 substrings since the pattern says "find all instances of 'egg' with 0 or more instances of 'spam' afterward". - The return value is the content of the capturing group, in this case the word 'spam', for each match. - Repeated instances of a capturing group replace each other, which is why you get one 'spam' for the last match instead of two. #third You have to recreate the iterator for the third one to print anything because all items had been iterated over in the for loop. You can see the behavior if you add a 'break' statement in the for loop after the print statement and don't recreate the iterator after #third. The for loop will print the first match, and the map call will print the other two.
8th Jun 2018, 12:24 AM
1_coder