some special characters such as %, #, \, {, } and delimits inline math environments with $ … $). What I need is to escape all special chars that are not already escaped and are not inside $ … $.
For example, for the provided entry "#s{o}me\%thing $ \text{a} $", I need to return "\#s\{o\}me\%thing $ \text{a} $".
I'm using re.sub to do that, and this way I can ignore the escaped ones, but I don't know how to ignore $ … $ environments. My current code is:
re.sub(r'([^\\]?)((?:%|#|{|}|#|&))', r"\1\\\2", string)
Any suggestions??
Edit: I wrote this on Stack Overflow with more details.
I'm not sure if regex is the best tool for your use case
What could I use?
Depending on what you're trying to do, a real parser might be better
Обсуждают сегодня