I’m using python and regex to try to extract the optional middle of a string.
>>> re.search(r'(.*?)(HELLO|BYE)?(.*?END)', r'qweHELLOsdfsEND').groups()
('', None, 'qweHELLOsdfsEND') #what I want is ('qwe', 'HELLO', 'sdfsEND')
>>> re.search(r'(.*?)(HELLO|BYE)?(.*?END)', r'qweBLAHsdfsEND').groups()
('', None, 'qweBLAHsdfsEND') #when the middle doesn't match. this is OK
How can I extract the optional middle?
Note: This is my first post.
Your regex fails because the first part is happy with matching the empty string, the second part fails (which is OK since it’s optional), so the third part captures all. Solution: Make the first part match anything up to
HELLOorEND:Is that acceptable?
Explanation: