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 7504773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:41:03+00:00 2026-05-29T21:41:03+00:00

I have multiple line string that I’d like to replace, but don’t understand why

  • 0

I have multiple line string that I’d like to replace, but don’t understand why it’s not working. For some reason, a period in the string stops the matching for the regular expression.

My string:

s = """
[some_previous_text]
<start>
one_period .
<end>
[some_text_after]
"""

What I’d like to end up with:

s = """
[some_previous_text]
foo
[some_text_after]
"""

What I initially tried, but it doesn’t match anything:

>>> import re
>>> s = "<start>\none_period .\n<end>"
>>> print re.sub("<start>[^.]*<end>", "foo", s)
<start>
one_period .
<end>

However, when I took the period out, it worked fine:

>>> import re
>>> s = "<start>\nno_period\n<end>"
>>> print re.sub("<start>[^.]*<end>", "foo", s)
foo

Also, when I put an <end> tag before the period, it matched the first <end> tag:

>>> import re
>>> s = "<start>\n<end>\none_period .\n<end>"
>>> print re.sub("<start>[^.]*<end>", "foo", s)
foo
one_period .
<end>

So what’s going on here? Why does the period stop the [^.]* matching?

EDIT:

SOLVED

I mistakenly thought that the carat ^ was for new-line matching. What I needed was a re.DOTALL flag (as indicated by Amber). Here’s the expression I’m now using:

>>> import re
>>> s = "<start>\none_period .\n<end>"
>>> print re.sub("<start>.*<end>", "foo", s, flags=re.DOTALL)
foo
  • 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-29T21:41:05+00:00Added an answer on May 29, 2026 at 9:41 pm

    Why wouldn’t it? [^.] is “the set of all characters that is not a .” and thus doesn’t match periods.


    Perhaps you instead meant to just put .* (any number of any characters) instead of [^.]*?

    For matching across newlines, specify re.DOTALL:

    re.sub("<start>.*<end>", "foo", s, flags=re.DOTALL)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that contains multiple parameters delimited by #, like this :
I have multiple classes that all derive from a base class, now some of
I have made a program that continuously monitors a log file. But I don't
I have a really long string of text that I would like to update
Let's say I have a string that has multiple carriage returns in it, i.e:
I have multiple projects in a couple of different workspaces. However, it seems like
I have multiple threads (C# application running on IIS) running that all need to
I have multiple logos of various companies in various formats that needs to be
I have a String that I'm passing to log4j to have it logged to
I have a text file that contains a data like ID Name Path IsTrue

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.