in reply to Search and replace.. ..a bit different

Sorry, but it's entirely unclear to me how your input transforms to your output under those variables. It looks like you're trying to cook up a wiring control or some such from a netlist, but I'm not familiar with the format.

Some general comments that may help:

All this stuff is by use of the PSI::ESP module. I hope I've guessed right. If not, please post a reply with clarifications.