I'm trying to parse some text using regex and would like a combination of strings to only register as one match if the combination appears, but for any substring to be captured if only that substring appears. For example, I want either foo bar or either individual string, such that I get:
text = 'foo bar bar foo'
In: re.findall(some_pattern, text)
Out: ['foo bar', 'bar', 'foo']
Using some_pattern = re.compile(r'foo|bar) returns ['foo', 'bar', 'bar', 'foo']. But I can't begin to think of any other patterns that would make this work. How can I capture this?