While traditional regex engines remember and return only the last match, some advanced libs provide captures
property which holds all matches for the given group. There's a library called regex
for python that does that, among other nice things:
import regex
string = "input-ports 6012, 6017, 6016"
m = regex.match("input-ports(?:\s(\d{4}),?)+", string)
print m.captures(1) # ['6012', '6017', '6016']
If you can't use this library, the only workaround is to use findall
and replace the repetition with a single group with lookaheads. This is not always possible, but your example is easy:
import re
string = "input-ports 6012, 6017, 6016"
m = re.findall("(?<=\s)\d{4}(?=,|$)", string)
print m # ['6012', '6017', '6016']