Some people try to do this kind of thing with regular expressions. It doesn't work; regular expressions can't handle context-free matching.
The right answer is Program Transformation Systems.
These are tools that parse source, build ASTs, and let one write code transformations in source syntax, very much as OP has shown.
While there is a ton of theory (see scholar.google.com with the term program transformation), building these tools is fairly difficult. Just the notion of parsing code fragments (as OP has hinted at) requires some pretty interesting twists in parsing machinery. I've been in this space since the 1980s, and seen the tools listed at Wikipedia built over the last 15-20 years; I don't see a lot more of them. The experience we have with building our system (see my bio) is some 50 man-years of PhD level software engineering, so I'm not surprised I don't see more of them.
These tools typically require a precise language front end to be useful, and this is where much of the work in actually using them goes after you have the basic transformation engines built. (See this discussion on parsing Java and C++; it applies equally to complex legacy languages like COBOL).
[A query in a comment wanted to know more where to learn more technical detail. If you want "first-cut" but deep technical detail, this survey is pretty good, but focuses mostly on "pure" transformation engines (operating only on abstract syntax trees).
I happen to believe that one needs considerably more than that; see my bio for a discussion of "Life After Parsing". You can get an older technical paper on our DMS system, and/or a more recent video overview of our system, including how it differs, in my Google Tech Talk.
In a comment on the original question, there was a request for "deep technical tour". See this for detailed discussion on how DMS handles rewrite rules; that page links to similarly deep discussions of related topics.