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

  • Home
  • SEARCH
  • 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 6024727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:11:51+00:00 2026-05-23T04:11:51+00:00

I have been using python with regex to clean up a text file. I

  • 0

I have been using python with regex to clean up a text file. I have been using the following method and it has generally been working:

mystring = compiledRegex.sub("replacement",mystring)

The string in question is an entire text file that includes many embedded newlines. Some of the compiled regex’s cover multiple lines using the re.DOTALL option. If the last character in the compiled regex is a \n the above command will substitute all matches of the regex except the match that ends with the final newline at the end of the string. In fact, I have had several other no doubt related problems dealing with newlines and multiple newlines when they appear at the very end of the string. Can anyone give me a pointer as to what is going on here? Thanks in advance.

  • 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-05-23T04:11:52+00:00Added an answer on May 23, 2026 at 4:11 am

    My bet is that your file does not end with a newline…

    >>> content = open('foo').read()
    >>> print content
    TOTAL:.?C2
    abcTOTAL:AC2
    defTOTAL:C2
    >>> content
    'TOTAL:.?C2\nabcTOTAL:AC2\ndefTOTAL:C2'
    

    …so the last line does not match the regex:

    >>> regex = re.compile('TOTAL:.*?C2\n', re.DOTALL)
    >>> regex.sub("XXX", content)
    'XXXabcXXXdefTOTAL:C2'
    

    If that is the case, the solution is simple: just match either a newline or the end of the file (with $):

    >>> regex = re.compile('TOTAL:.*?C2(\n|$)', re.DOTALL)
    >>> regex.sub("XXX", content)
    'XXXabcXXXdefXXX'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently been working with Python using Komodo Edit and other simpler editors
I have been trying to make a case for using Python at my work.
I've been using python for years, but I have little experience with python web
I am trying to talk to a device using python. I have been handed
We have been using Scrum for around 9 months and it has largely been
I have been using Python for about a year now, coming from a mostly
I have had to change from python 2.7 to 2.6. I've been using a
I have developed some internal tools at work using Python. I have been using
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been using PHP and JavaScript for building my dad's website. He wants

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.