I don’t know of any way to accomplish that within Sublime, unless there is a plugin that allows you to do it. Regular expressions don’t have the kind of conditional logic in them that would allow you to formulate a replacement that selects the thing to replace based on what it matched.
Having said that, I don’t know if the regex engine that Sublime uses is more advanced than what I’m used to, so I might be talking out of my butt there.
Assuming I’m not, the only way to do that would be to do multiple regex finds and replaces in a row (perhaps using a Macro so that you could do it in one pass?).
If you’re on a Mac/Linux machine, this is exactly the sort of job for sed
, which basically allows you to perform several operations at once. For example:
sed -e "s/WORD_1/A/g" -e "s/WORD_2/B/g" inputFile.txt > outputFile.txt
which means:
Process the file inputFile.txt
line by line, and replace every occurrence of WORD_1 with the text A (the g
means every match on the line and not just the first one found). Then, replace every occurrence of WORD_2 with the text B. Place the transformed text in the file outputFile.txt
Also important to keep in mind that in cases like this, the second operation happens after the first one, so if the first one has replacement text that maches what the second one is looking for, it might not do what you expect it to.