4

I'm trying to parse and remove any \command (\textit, etc...) from each line loaded (from .tex file or other commands from lilypond files as [\clef, \key, \time]).

How could I do that?

What I've tried

import re
f = open('example.tex')
lines = f.readlines()
f.close()

pattern = '^\\*([a-z]|[0-9])' # this is the wrong regex!!
clean = []
for line in lines:
    remove = re.match(pattern, line)
    if remove:
        clean.append(remove.group())

print(clean)

Example

Input

#!/usr/bin/latex

\item More things
\subitem Anything

Expected output

More things
Anything

2 Answers 2

2

You could use a simple regex substitution using this pattern ^\\[^\s]*:

Sample code in python:

import re
p = re.compile(r"^\\[^\s]*", re.MULTILINE)

str = '''
\item More things
\subitem Anything
'''

subst = ""

print re.sub(p, subst, str)

The result would be:

More things
Anything
Sign up to request clarification or add additional context in comments.

Comments

0

This will work:

'\\\w+\s'

It searches for the backslash, then for one or more characters, and a space.

1 Comment

Hi, Thanks for answer. I also tried using with '^\\\w+\s' but didn't work as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.