You can do:
>>> import re
>>> def repl(match):
... start, end = match.groups()
... return ','.join(str(i) for i in range(int(start), int(end)+1))
...
>>> re.sub(r'(\d+)-(\d+)', repl, "235:2,4,6-9,12,14-19;240:3,5-9,10;245:4,9,10-15,18")
'235:2,4,6,7,8,9,12,14,15,16,17,18,19;240:3,5,6,7,8,9,10;245:4,9,10,11,12,13,14,15,18'
This uses the fact that the repl argument to re.sub can be a callable that takes as argument the match and returns the replacing string.
The expand(s) function would then be:
import re
def repl(match):
start, end = match.groups()
return ','.join(str(i) for i in range(int(start), int(end)+1))
def expand(s):
return re.sub('(\d+)-(\d+)', repl, s)