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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:22:17+00:00 2026-06-15T10:22:17+00:00

I want to search for a substring in a string and replace with another

  • 0

I want to search for a substring in a string and replace with another pattern.
The search string is in between curly braces.

Example string

line = $lang['MY_KEY'] = '{search-string} wird in Analyse berücksichtigt';

I tried this code:

re.sub('([a-zA-Z0-9\[\]\$\' ].*{).*(}[a-zA-Z\.,; ].*)','\1replace-string\2',line)

And also:

re.sub('(.*{).*(}.*)', '\1replace-string\2', line)

I am getting some junk characters along with the replace-string in the output.

EDIT

I have one more question.
There are multiple search strings in curly braces. I want to replace each substring with different patterns. How can I do it?

  • 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-15T10:22:18+00:00Added an answer on June 15, 2026 at 10:22 am

    As you have backslashes prefix the patterns and replacements with an r :-

    print re.sub(r'([a-zA-Z0-9\[\]\$\' ].*{).*(}[a-zA-Z\.,; ].*)',r'\1replace-string\2',line)
    
    print re.sub(r'(.*{).*(}.*)', r'\1replace-string\2', line)
    

    EDIT: replace every second occurrence using a replacement function

    import re
    def repl(matchobj):
        global counter
        counter = counter+1
        if counter == 2: 
            return '{replace-string}' 
        else: 
            return matchobj.group(0)
    
    counter = 0
    line = "$lang['MY_KEY'] = '{search-string} wird in Analyse {search-string} bercksichtigt'";
    print re.sub(r'{.*?}',repl,line)
    

    EDIT: Use a dictionary to store the replacements

    import re
    def repl(matchobj):
        global counter
        replacementFor = {'{search-string1}': '{replace-string1}',
                          '{search-string2}': '{replace-string2}',
                          '{search-string3}': '{replace-string3}'}
    
        counter = counter+1
        if counter == 2: 
            return replacementFor.get(matchobj.group(0), matchobj.group(0))
        else: 
            return matchobj.group(0)
    
    counter = 0
    line = "$lang['MY_KEY'] = '{search-string} wird in Analyse {search-string1} bercksichtigt'";
    print re.sub(r'{.*?}',repl,line)
    
    counter = 0
    line = "$lang['MY_KEY'] = '{search-string} wird in Analyse {search-string3} bercksichtigt'";
    print re.sub(r'{.*?}',repl,line)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to search and replace substring in string which starts and ends with
I have a string s and I want to search for the substring of
I want to search two item (name=string and location=json). this search is (one input
I want to search a text in a table without knowing its attributes. Example
I want to set a anchor tag in a provided string. It search in
Possible Duplicate: How to see if a substring exists inside another string in Java
I need to perform a regular expression search for a string x in another
I want to write a procedure (function) that checks if a string contains another
I have a string with certain pattern. I need to search for the pattern
I am using jQuery.autocomplete(1.02) on my search box and I want exact string and

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.