Use Mathematica to edit TeX file

I have a large (>300 page) latex project split across multiple .tex files that I have decided I want to change some notation around in. A minimal working example would be a main file main.tex that reads

\documentclass{book} \usepackage{amsmath} \newcommand{\funct}[2]{f \left( #1 , #2 \right)} \begin{document} \include{ch1} \end{document}

with ch1 reading

\chapter{Chapter 1} words words words $ \funct{a}{b}$ words word $ \funct { \sum_{k=1}^\infty \frac{1}{k^2}} {\lim_{n\to \infty} n }$

and so forth. Now, I’ve decided that I would like to globally reverse the inputs of \funct. Changing main.tex to \newcommand{\funct}[2]{f \left( #2 , #1 \right)} would do the exact right thing, except that this would be really psychedelic and problematic for any future editing. As such, I need to go through and reverse ever instance of this macro being used in the individual tex files, i.e., replace \funct{a}{b} with \funct{b}{a}. The problem is that there are…hundreds?…of instances of this macro being used among the various tex files, not to mention that the individual inputs to this macro are often several lines worth of latex. Basically, going through by hand would be an absolute nightmare and necessitate several days worth of hunting down typos.

As such, I’ve had the thought of trying to use some of Mathematica’s string pattern matching to do this reordering automatically. After making ALL THE BACKUPS, I’ve gotten as far as:

ch1=Import[<file path for ch1.tex>, "String"] StringCases[ch1, "\funct{"~~_~~"}{"~~_~~"}"]

Before getting hopelessly stuck. In particular, Mathematica doesn’t seem to like all the \ characters, not to mention the issues of inconsistent spacing between braces, nor dealing with matched sets of nested open/close braces (e.g., the second \funct { ....} {...} above).

Any suggestions? Or is this going to become a quagmire, and I would be better off just brewing a pot of coffee and getting busy with the old ctrl-x ctrl-v routine?