In Python, is there a way to search, return matched string, and replace matched strings all at the same time? See example below:
a = "[fox] dog turtle [cat]"
Goal:
result1 = "fox" #(first match inside bracket)
result2 = "cat" #(second match inside bracket)
result3 = "dog turtle" #(remaining string after removing matched text inside brackets
What I have:
result1, result2 = re.findall('\[.*?\]', a)
result3 = re.sub('\[.*?\]', '', a)
It seems redundant and clunky to have to run re twice. Is there a more elegant way to achieve this?
