# Removing latex commands using Python “re” module

Recently I had to sanitize lines in a .tex file where a `\textcolor` command had been used.
The command was being used the following way: `{\textcolor{some_color}{text to color}}`.

The main problem was that the command could have appeared any number of times in a line, so I couldn’t apply the command a set number of times.
Also, given any color could have been used, a simple “blind replace” was clearly not a good weapon in this case.

I therefore resorted to applying a reg ex recursively until the line was cleaned of any `\textcolor` command.

In a nutshell:

```def discolor(line):
regex = re.compile('(.*?){\textcolor\{.*?\}(\{.*?\})\}(.*)')
while True:
try:
line = ''.join(re.search(regex, line).groups())
except AttributeError:
return line```

The key part here is that we match not only the text inside the `\textcolor` command, but also what comes before and after (the two `(.*?)` blocks). We return them all until there are any left: when that happens, accessing `.groups()` will raise an `AttributeError`, which we catch and use as sentinel to know when to return.