Smart way to perform a similar replacement (not simple enough for diff) in multiple files?

view story

http://unix.stackexchange.com – I have a bunch of files (45, so too many to edit them manually without going crazy) which all require the same changes (as seen in the three diffs at the end of this question). Most tools used for search-and-replace on the commandline only support line-by-line replacements so they won't work. Is there some commandline tool available which can do the job? Basically performing a multi-line regex search&replace with backreferences without shoving the whole regex into a single line (i.e. it should be read from a file or stdin) should be enough, but maybe there's an even better solution to do (HowTos)