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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:49:09+00:00 2026-05-30T07:49:09+00:00

There are many threads on find replace text for python but I think my

  • 0

There are many threads on find replace text for python but I think my question is different.

I have a bunch of java files with

System.out.println("some text here");

I am trying to write a python script which will replace them all with

if (logger.isInfoEnabled()) {
    logger.info("some text here");
}

To do this I have tried:

def findReplace(fileName, sourceText, replaceText):
    file = open(fileName, "r") #Opens the file in read-mode
    text = file.read() #Reads the file and assigns the value to a variable
    file.close() #Closes the file (read session)

    file = open(fileName, "w") #Opens the file again, this time in write-mode
    file.write(text.replace(sourceText, replaceText)) #replaces all instances of our keyword
    # and writes the whole output when done, wiping over the old contents of the file
    file.close() #Closes the file (write session)

and pass in:

filename=Myfile.java, sourceText='System.out.println', replaceText='if (logger.isInfoEnabled()) { \n' \logger.info'

However, I am struggling to the get the closing ‘ in the replace. It needs to wrap out around the same output string that is already there. Any tips?

Thanks.

  • 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-30T07:49:10+00:00Added an answer on May 30, 2026 at 7:49 am
    import re
    
    sourceText = 'System\.out\.println\(("[^"]+")\);'
    
    replaceText = \
    r'''if (logger.isInfoEnabled()) {
        logger.info(\1);
    }'''
    
    re.sub(sourceText, replaceText, open(fileName).read())
    

    This isn’t perfect — it will only work if the string doesn’t contain any escaped quote marks i.e. \" — but hopefully it should do the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand there are many threads like mine but I have been trying to
I have searched many threads so far but cant seem to find a solution.
I have a black box of code that contains many threads. There is no
I am now debugging a program that utilizes many different threads. There is an
I know there are many similar threads out there pertaining to this but I
Is there any way to find that how many threads are waiting on semaphore?
I have a Python test suite that creates and deletes many temporary files. Under
Is there any way to check how many threads are waiting for a synchronized
Firstly I know that there are many question and solutions to correct thread marshalling
There are many ASP.MVC blog post bits and pieces scattered over different web sites,

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.