0
Help me with this code.
#Can anyone explain this code? import re pattern =r"spam" print(list(re.finditer(pattern, "eggspamsausagespam"))) for i in re.finditer(pattern,"eggspamsausagespam"): print(i.group()) Output [< re.Match object ; span=(3,7),match='spam'>,re.Match object; span=(14,18),match='spam'>] spam spam
1 Odpowiedź
+ 1
1. import re // imports the regular expression module
2. pattern = r"spam" // declares a variable called "pattern" with the row string "spam" as value
3. Now it is better to split the code
- re.finditer(pattern, "eggspamsausagespam") //returns an iterator, which is actually a Match object, if the pattern is matched
- list(...) // puts each matched result in a list
- print(...) //prints the list
4. for in re.finditer(pattern, "eggspamsausagespam") //iterates through each matched result of the iterator
print(i.group()) //prints each group found
I hope you better understand the code now