This is sort of a follow-up to Python regex - Replace single quotes and brackets thread.
The task:
Sample input strings:
RSQ(nam
You can indeed use the regex module and repeated captures. The main interest is that you can check the structure of the matched string:
import regex
regO = regex.compile(r'''
\w+ \( (?: name\['([^']*)'] (?: ,[ ] | (?=\)) ) )* \)
''', regex.VERBOSE);
regO.sub(lambda m: 'XYZ(' + (', '.join(m.captures(1))) + ')', s)
(Note that you can replace "name"
by \w+
or anything you want without problems.)