I have code that I thought would work, but it does not. In particular, how can I handle the case where there is only a begin or end phrase within the text. See my code below. Thanks!
import re
def extract(text, begin, end):
result1 = re.search(begin, text)
if result1 is None:
index1 = " "
else:
index1 = text.find(begin) + len(begin)
result2 = re.search(end, text)
if result2 is None:
index2 = " "
else:
index2 = text.find(end)
return text[index1:index2]
print(extract("Eat an <apple> each day", "<", ">"))
print(extract("Oh [/b] no", "[b]", "[/b]"))
#First case works as expected and prints "apple". I am expecting "Oh" to print for the second case, but it's not returning anything. Why not and how do I fix it?

