I have a string:
"foo hello world baz 33"
The part between foo
and baz
will be some number of space separated words (one or more). I want to match this string with an re that will group out each of those words:
>>> re.match(r'foo (<some re here>) baz (\d+)', "foo hello world baz 33").groups()
('hello', 'world', '33')
The re should be flexible so that it will work in case there are no words around it:
>>> re.match(r'(<some re here>)', "hello world").groups()
('hello', 'world')
I'm trying variations with ([\w+\s])+
, but I'm not able to capture a dynamically determined number of groups. Is this possible?