python
python
Out of context: Reply #2
- Started
- Last post
- 2 Responses
- jasonistaken0
Easiest way to accomplish this is with a regex replace with a callback method.
re.sub allows you to pass a function as the replacement paramter.
Here's a simple example from the manual:>>> def dashrepl(matchobj):
... if matchobj.group(0) == '-': return ' '
... else: return '-'
>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
'pro--gram files'You should be able to figure out the rest.