I'm struggling to find a solution, I'm trying to make a simple app in Perspective that can take FactoryTrash ME window exports and do a search and replace. I have a lookup table that is the current address and what the new address should be.
Here is a short example:
import re
oldAddress = "::[PLC]N22:21"
newAddress = "[PLC]N22[21]"
data = '<connection name="Value" expression="{::[PLC]N22:211} * .1"/>'
data = data.replace(oldAddress, newAddress)
print data
To have regex see square brackets, you have to escape them with \.
However, help me understand. Your oldAddress ends with 21, not 211, so it would not replace the last 1 in the data string because its not part of the searched string. Don't you want your oldAddress to be ::[PLC]N22:211?
I tried escaping with backslashes and I couldn't get it to work, but perhaps I was overlooking something.
In my example, nothing should have been replaced, {::[PLC]N22:211} should have remained as-is. My point was using replace would still replace the text, which is why I wanted to use boundaries.
I'll be honest, this app is really simple and it's just something I want to use for conversion projects. So, not important, IMO, to be best practice, so for right now I just added back in the curly brackets, then do the replace. I should have mentioned this is replacing direct tags which are always wrapped in curly brackets and will include a shortcut (the stuff with square brackets).