1

For example:

import re
FOO = """neighbors= {5 7 9 11 13 14 15 16 17 }"""
COMPILE = re.compile('(neighbors\s*=\s*\{\s*(\d+\s*)+\})')
match = re.findall(COMPILE, FOO)
print match[0]

(the code is from here: find a repetitive expression using python regular expression )

This will only capture the last group, i.e. "17 ". What if I want to capture each of these groups, i.e. "5", "7", "9", ...?

Community
  • 1
  • 1
qed
  • 22,298
  • 21
  • 125
  • 196

2 Answers2

3

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

Community
  • 1
  • 1
wap26
  • 2,180
  • 1
  • 17
  • 32
2

I suggest using a simpler regex to match all numbers, and using this in with re.findall:

import re
s = 'neighbors= {5 7 9 11 13 14 15 16 17 }'
r = re.compile('(\d+)')
print re.findall(r,s)

which outputs

Out[5]: ['5', '7', '9', '11', '13', '14', '15', '16', '17']
mbatchkarov
  • 15,487
  • 9
  • 60
  • 79