I have a text file (verilog) that contains certain string sequences (escaped identifiers) that I want to modify. In the example below, I want to find any group starting with '\' and ending with ' ' (any printable character can be in between). After finding a group that matches this criteria, I want to replace all non-alphanumeric characters with alphanumeric ones (I don't really care what alphanumeric they get replaced with).
In[1]: here i$ \$0me text to \m*dify
Out[1]: here i$ aame text to madify
I have no problem finding the groups that need replacing using regex. However, if I just use re.findAll(), I no longer have the location of the words in the string to reconstruct the string after modifying the matched groups.
Is there a way to preserve the location of the words in the string while modifying each match separately?
Note: I previously asked a very similar question here, but I oversimplified my example. I thought editing my existing question would make the existing comments and answers confusing to future readers.
re.subyet? I think it's capable of taking your modified text and reconstructing the string on its own, without any additional effort on your part. And you can pass a callable for thereplparameter ofsub, so you can execute arbitrary code on each match rather than just replacing it with something static.re.sub, and the same is applicable here.