I am trying to write a regex to replace the whole square bracket with the first choice. For example, [ choice A | choice B ] I want to replace the previous square bracket as a whole with choice A. However, when I have more than one of these brackets:
[ choice A | choice B ] and choose between [ choice D | choice F ]
in the same line, all the brackets get replaced by choice A. I know it's because I am selecting [0] in my code, but I don't know how to replace each bracket with its respective choice; namely, choice A and choice D
import re
line = "[ choice A | choice B ] and choose between [ choice D | choice F ]"
x = re.findall( r"\[(.*?)\|", line)[0].strip()
line = re.sub(r"\[(.*?)\]", x,line)
print(line)