rereplace help
-
I’m trying to automate some of my coding tasks using pythonscript and running into an issue I think regex should be able to solve. I want to replace things that are specifically surrounded by certain characters while preserving the middle part.
so for example:
" & Placeholder &"
becomes:
{Placeholder}
I thought that named capture variables would be able to do this but I am getting an empty {} trying to use them in this situation with the code:
editor.rereplace(r" & \b(?<Placeholder_Variable>[a-zA-Z0-9._]*)\b & “,r”{(?{Placeholder_Variable})}")
-
You have several things wrong in your regular expression.
Try this instead:
editor.rereplace(r'" & (?<Placeholder_Variable>\w+) &"', r'{$+{Placeholder_Variable}}')
EDIT: Oops, the first version of this posting used Python named capture group stuff; I’ve adjusted to use Boost equivalents.
-
@Alan-Kilborn I had to re-add ‘.*’ to the middle section, but I got it working now and can use variables the way I want now, thanks!!