I am trying to grab a string that is surrounded by “! and replace the first “!” with a “!_”.
For example:
str(!test!).strip() -> str(!_test!).strip()
Here is the code I have so far:
print re.sub(r'!.*?!','!_', 'str(!test!).strip()')
With this code I grab too much and the result is:
str(!_).strip()
Any thoughts on how to zero in on the first “!”. Or alternatively, is there a way to grab the string in the “!!” and then add “!_”+”!” arround that string?
You need to group the second half of the “word” with parenthesis and use the group in your substitute string
\g<1>.