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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:15:36+00:00 2026-05-13T06:15:36+00:00

I’m using Python to write a regular expression for replacing parts of the string

  • 0

I’m using Python to write a regular expression for replacing parts of the string with a XML node.

The source string looks like:

Hello
REPLACE(str1) this is to replace
REPLACE(str2) this is to replace

And the result string should be like:

Hello
<replace name="str1"> this is to replace </replace>
<replace name="str2"> this is to replace </replace>

Can anyone help me?

  • 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-13T06:15:36+00:00Added an answer on May 13, 2026 at 6:15 am

    What makes your problem a little bit tricky is that you want to match inside of a multiline string. You need to use the re.MULTILINE flag to make that work.

    Then, you need to match some groups inside your source string, and use those groups in the final output. Here is code that works to solve your problem:

    import re
    
    
    s_pat = "^\s*REPLACE\(([^)]+)\)(.*)$"
    pat = re.compile(s_pat, re.MULTILINE)
    
    s_input = """\
    Hello
    REPLACE(str1) this is to replace
    REPLACE(str2) this is to replace"""
    
    
    def mksub(m):
        return '<replace name="%s">%s</replace>' % m.groups()
    
    
    s_output = re.sub(pat, mksub, s_input)
    

    The only tricky part is the regular expression pattern. Let’s look at it in detail.

    ^ matches the start of a string. With re.MULTILINE, this matches the start of a line within a multiline string; in other words, it matches right after a newline in the string.

    \s* matches optional whitespace.

    REPLACE matches the literal string “REPLACE”.

    \( matches the literal string “(“.

    ( begins a “match group”.

    [^)] means “match any character but a “)”.

    + means “match one or more of the preceding pattern.

    ) closes a “match group”.

    \) matches the literal string “)”

    (.*) is another match group containing “.*”.

    $ matches the end of a string. With re.MULTILINE, this matches the end of a line within a multiline string; in other words, it matches a newline character in the string.

    . matches any character, and * means to match zero or more of the preceding pattern. Thus .* matches anything, up to the end of the line.

    So, our pattern has two “match groups”. When you run re.sub() it will make a “match object” which will be passed to mksub(). The match object has a method, .groups(), that returns the matched substrings as a tuple, and that gets substituted in to make the replacement text.

    EDIT: You actually don’t need to use a replacement function. You can put the special string \1 inside the replacement text, and it will be replaced by the contents of match group 1. (Match groups count from 1; the special match group 0 corresponds the the entire string matched by the pattern.) The only tricky part of the \1 string is that \ is special in strings. In a normal string, to get a \, you need to put two backslashes in a row, like so: "\\1" But you can use a Python “raw string” to conveniently write the replacement pattern. Doing so you get this:

    import re

    s_pat = "^\s*REPLACE\(([^)]+)\)(.*)$"
    pat = re.compile(s_pat, re.MULTILINE)
    
    s_repl = r'<replace name="\1">\2</replace>'
    
    s_input = """\
    Hello
    REPLACE(str1) this is to replace
    REPLACE(str2) this is to replace"""
    
    
    s_output = re.sub(pat, s_repl, s_input)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 273k
  • Answers 273k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Seeing as this meta tag changes the way the document… May 13, 2026 at 2:14 pm
  • Editorial Team
    Editorial Team added an answer I think, this article explains a lot about the problem… May 13, 2026 at 2:14 pm
  • Editorial Team
    Editorial Team added an answer Adding a touch of object orientation here makes things simpler.… May 13, 2026 at 2:14 pm

Related Questions

I want use html5's new tag to play a wav file (currently only supported
In order to apply a triggered animation to all ToolTip s in my app,
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I've got a string that has curly quotes in it. I'd like to replace

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.