I want to use sed to rename variable names (identifiers). I want to do it for c++ however for other languages it will be similar. Say we have a code sample like that here: example.cpp
int hi;
int bye;
...//a lot of code with many occurences of n
Assumed for any reason I want to rename hi in hello. The problem is hi can occur as a part of other words. In C++ a valid identifiers has the following receipt :[[[:alpha:]]_]+[[[:alnum:]]_]
(Putting extended characters like ä
or 龍
aside. I do not know if alnum includes these but if they are no problem expect extended punctuation characters maybe but who uses them)
There must be a character not pertaining to this expression next to a valid identifier to distinguish it from other identifiers. So before and behind n
an [[[:alnum:]]_]
is not allowed while any other character may. Another problem are string in ""
. This all only works if strings are always on-liners. Then we must check for odd occurences of unescpaped " and it may be a mathematical issue if we can do this with regular expressions however I did not come to this point trying this the first time without string recognising:
sed -i -e 'hi/\([^[[:alnum:]]_]\)hello\([^[[:alnum:]]_]\)/\1r\2/g' example.cpp
It doesnt changed anything