# Replacing text using regular expression before compilation without texteditor

I would like to replace some text (no commands) which are included in my tex-file with some certain string. This shall happen (if possible) without modifying the actual source file. Therefore, I don't want to use some bash/shell script or text-editor replacing function.

Is there any command/toolbox which does this? I have seen that there is an package called l3regex, unfortunately I don't know how to use the tex commands and I don't even know whether this a suitable package for this.

Sorry for being imprecise. Here an example.

\documentclass{article}
\theUltraCoolRegExpTool{oldRegexp}{newRegexp}
\begin{document}

\include{file1.tex}
\include{file2.tex}

This here is very long text which also has some formulas $a_{ij} = b*x^2$.

\end{document}


And here, I e.g. want to replace using a regular expression a_{ij} by X^{ij}. Or maybe some other text in the included files.

-
Is conditional compilation an option? By this, I mean, extra lines of document that will or will not be executed depending on the state of a variable. –  Steven B. Segletes Feb 7 '14 at 16:49
Please provide a small example document with an indication of the needed replacements. In particular where is the text? in headings? in verbatim sections? in one specific environment? the details will affect the possible answers. –  David Carlisle Feb 7 '14 at 16:49
@DavidCarlisle sorry, I edited the question. –  bonanza Feb 7 '14 at 16:56

l3regex can apply a regular expression to a supplied list of tokens but the harder part is to reliably apply any operation to text occurring freely in the document rather than text that is the argument to some command.

It is possible in simple cases but likely to be incompatible with packages used in real documents.

So, despite the fact that you say you don't want a bash script, I offer this bash script, it does not edit (or even save) the modified document, just pipes it into latex.

given

\documentclass{article}
%\theUltraCoolRegExpTool{oldRegexp}{newRegexp}
\begin{document}

%\include{file1.tex}
%\include{file2.tex}

This here is very long text which also has some formulas $a_{ij} = b*x^2$.

second para.

\end{document}


The command line

 sed -e "s/a_{ij}/x^{ij}/g" regex.tex  | pdflatex  --jobname=regex \\relax


Produces

-