python regular expression remove matching brackets file

python, regex

Solution

You can't match an undetermined level of nested brackets with the re module since it doesn't support recursion. To solve that, you can use the new regex module:

import regex

c = r'\red{here is \underline{underlined} text}'

c = regex.sub(r'\\red({((?>[^{}]+|(?1))*)})', r'\2', c)

Where `(?1)` is a recursive call to the capture group 1.

Problem

I have a Latex file where a lot of text is marked with `\red{}`, but there may also be brackets inside the `\red{}`, like `\red{here is \underline{underlined} text}`. I want to remove the red color and after some googling I wrote this python script: ``` import os, re, sys #Start program in terminal with #python RedRemover.py filename #sys.argv[1] then has the value filename ifn = sys.argv[1] #Open file and read it f = open(ifn, "r") c = f.read() #The whole file content is now stored in the string c #Remove occurences of \red{...} in c c=re.sub(r'\\red\{(?:[^\}|]*\|)?([^\}|]*)\}', r'\1', c) #Write c into new file Nf=open("RedRemoved_"+ifn,"w") Nf.write(c) f.close() Nf.close() ``` But this will convert \red{here is \underline{underlined} text} to here is \underline{underlined text} which is not what I want. I want here is \underline{underlined} text

Original source