This is a little trick I just applied and thought was cool enough to write down.
Let’s say you want to replace a name that is used throughout a project. Due to various
conventions/restrictions in use the name might appear in several forms like:
MY_COOL_NAME, my-cool-name, my_cool_name etc.
In Emacs you can invoke regexp replace across an entire project by invoking
project-query-replace-regexp, by default bound to C-x p r. This will first prompt
for the regexp to search for, then what to replace it with.
For the search regexp we can put: my\([_-]\)cool\1name.
This allows either underscore or hyphen as a separator. Notice we use \1 as the
second separator. This is a “backreference” and simply refers to whatever was captured
in the first group, in this case \([_-]\).
We can then us the same backreference in the replacement, so we can put: new\1name.
After pressing enter again emacs will then cycle through every replacement in every file
in the project allowing you to either apply it, with y or skip it, with n. If you
wish to make the changes across an entire file unconditionally, press !. If you wish
to skip an entire file, press N. You can also press ? to see the other options.
Notice Emacs does what you (probably) want when it comes to case. We didn’t type the
search in upper case, but it will match MY_COOL_NAME and replace it with NEW_NAME.
Similarly, if there were a My-Cool-Name, it would replace it with New-Name
automatically.