As I gradually update the source code of my LaTeX documents to conform to new conventions, I find myself in need of a search/replace utility that is (or can be made) aware of LaTeX math mode. Often I need to replace some patterns in math mode, but not in text mode.

For example, my own stubborn TeX trickery once made me turn . into a math-active alias for \cdot. It made my math code a lot more readable at the time. Now that I'm using XeLaTeX and unicode input, I want to replace those occurrences with a proper ·.

Using regular expressions in a conservative way, I've been able to catch a lot of them, but now and then I still run into some stragglers. If only I could automatically replace all .'s in math mode, it would save me a lot of tedious effort. And there are still two or three more of such cases.

Math mode in my documents is relatively predictable. Always delimited by \[...\], $...$ or one of a small number of custom environments. I also occasionally use \text. But it doesn't get more complicated than that.

Is there a utility or script I could use to make this task easier? I work in Linux, so there should be plenty of possibilities, but I'm not really a hero with Perl et al.