Question:
I use the search functionre.finditer() and get span as 1 tupleI’m trying a function that can handle any input but I can’t seem to get it to work
re.finditer
, then you need to cut-and-paste the string yourself. You also want to reverse the matches, because the indices for later matches will get messed up with earlier edits (“cutting the branch you are sitting on”)."Help"
instead of "help"
, and not specify the re.I
flag. Or you could search for (?<=help.*)help
with re.I
(a “help” that is preceded by another “help”). Or you could use re.sub
with a replacer function and a nonlocal counter, so that you only replace the second match. Or you could use re.finditer
to find a list of matches, extract the second one, and then perform the cut-and-paste from the first example.‘extract the second one’ this is exactly my problem. How can i extract it from group of finditer?
If you have better answer, please add a comment about this, thank you!