Replace text in files

Posted on 2011-10-05
Last Modified: 2012-05-12
I've got the below code to replace text between two tags in an XML file, the script seems to open the file, and debugging code shows its finding the tags correctly, however, its not actually replacing the text (or writing back to the file)

What I'm looking to do, is replace anything between <ConnectionString> and </ConnectionString>

Can someone lend a hand please?


def ChangeOnFile(file, regex_to_find, text_to_put_in_place)
    text= file, 'w+'){|f| f << text.gsub(regex_to_find,text_to_put_in_place)}

  puts "Start #{}"
  puts "I'm in #{Dir.pwd}"
  folders = Dir.glob("**/")
  for fldr in folders
    puts "Searching #{fldr}"
    Dir.chdir("#{fldr}") do
    Dir["*.xml"].each do |tempfile|
    puts "Process File #{tempfile}"
    #build the connection string
    replace = "My New Text"
    #update the file
    ChangeOnFile(tempfile, /<ConnectionString>\d+<\/ConnectionString>/, replace)
rescue Exception => e
  puts "Error: #{e}"
end #begin
Question by:Andrew Doades
    LVL 34

    Accepted Solution

    Disclaimer: I don't know anything about ruby, just regular expressions.

    The pattern:
    requires an integer number of at least digit between <ConnectionString> and <\/ConnectionString>

    If as you say you want to replace anything between those tags, then you should be probably using pattern:

    I'm assuming the ? works to make the .* non-greedy in the ruby regex engine - it doesn't work in all languages, but I'd guess it probably will in this case.
    LVL 34

    Expert Comment

    by:Terry Woods
    Confirmed via that my pattern works... hope that solves your problem!
    LVL 10

    Author Closing Comment

    by:Andrew Doades
    Thank You! Tested and confirmed as working!


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Article by: narshlob
    If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
    In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below:…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now