Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:22:08+00:00 2026-06-14T11:22:08+00:00

I want to use a python regexp to remove the comments in a LaTeX

  • 0

I want to use a python regexp to remove the comments in a LaTeX file. In LaTeX a comment starts by “%”. But if the % character is escaped (“\%”) then its not a comment, its the symbol percent.

This task is just one among many regexp that I apply on my LaTeX text. I store all these reg exp in a list of dicts.

The problem I face is that the regexp I use for pruning the comments does not work (because I do not know how to specify the character set ‘not backslash’). The backslash in the character set escapes the closing ‘]’ and the regexp is incorrect.

My code:

regexps=[]
regexps.append({r'left':'%.*', 'right':r''}) # this strips all the comments, but messes up with the percent characters (\%)
regexps.append({r'left':'[^\]%.*', 'right':r''}) # this is incorrect (escapes the closing "]" )
return applyRegexps(latexText, regexps)


def applyRegexps(text, listRegExp):
    """ Applies successively many regexps to a text"""
    if testMode:
        print str(listRegExp)
    # apply all the regexps in the list
    for element in listRegExp:
        left = element['left']
        right = element['right']
        r=re.compile(left)
        text=r.sub(right,text)
    return text

Any help will be much appreciated. Thanks!

Gilles

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:22:09+00:00Added an answer on June 14, 2026 at 11:22 am

    Simply double the backslash, but do use a raw string literal to avoid having to double them again:

    regexps.append({'left':r'[^\\]%.*', 'right':r''})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to use Python to open a .csv file like
I want to use the google maps hand cursor in Python but I don't
I want to use this regular expression in Python: <(?:[^]*[']*|'[^']*'[']*|[^'>])+> (from RegEx match open
I want to use Python to access MFC document files generically? Can CArchive be
I want to use some Python libraries to replace MATLAB. How could I import
I use python -m SimpleHTTPServer when I want to run my web application locally,
I want to use some features of python like as Tuples and Sets in
I am implementing caching for my Python application and I want to use memcached.
I'd like to create a Word document using Python, however, I want to re-use
I'm using Python and I want to use regular expressions to check if something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.