Basically, you cannot use a single capturing group followed by a repetition marker +
or *
to capture an arbitrary number of sequences.
Maybe you should capture the sequence of integers and then split it, like this:
import re
FOO = """neighbors= {5 7 9 11 13 14 15 16 17 }"""
COMPILE = re.compile('(neighbors\s*=\s*\{\s*((\d+\s*)+)\})') # added a capturing group
match = re.findall(COMPILE, FOO)
print match[0][1].split()
which prints:
['5', '7', '9', '11', '13', '14', '15', '16', '17']
Additionally, maybe you don't need to findall
but only to match
, like this:
import re
FOO = """neighbors= {5 7 9 11 13 14 15 16 17 }"""
COMPILE = re.compile('(neighbors\s*=\s*\{\s*((\d+\s*)+)\})')
match = re.match(COMPILE, FOO)
if match:
print match.group(2).split()
else:
print 'input does not match regex'
You can also take a look at the top answers of the following similar questions:
python regex for repeating string
Python RegEx multiple groups