On Apr 8, 2008, at 2:53 PM, Peter Bailey wrote: > Hi, > I need to capitalize the words in a string I find in XML files. > > The string that's in (.*) below is what I need to change. I just > want to > capitalize the first letter of each word in the string. > > I'm trying this, in a test: > > Dir.chdir("C:/users/pb4072/documents") > file = File.read("test1.txt") > file.gsub(/^<row><entry><text><emph face="b">(.*)<\/emph>/) do | > match| > array = $1.split > array.each do |word| > word.capitalize! > end > newfile = File.open("c:/users/pb4072/documents/test1.txt", "w") { |f| > f.print array } > end > > And, I'm getting this: > > #(.*)<\/emph>theQuickBrownFoxJumpedOverTheLazyDog. > > I want this: > > <row><entry><text><emph face="b">The Quick Brown Fox Jumped Over The > Lazy Dog.<\/emph>/ > > > Thanks, > Peter Dir.chdir("C:/users/pb4072/documents") do |d| file = File.read("test1.txt") output = file.gsub(%r{^(<row><entry><text><emph face="b">)(.*)(</ emph>)}m) do |match| "#{$1}#{$2.gsub(%r{\b\w+\b}){|w|w.capitalize}}#{$3}" end File.open("test1.txt", "w") { |f| f.write output } end Note the use of three capture groups to get the unchanged initial and final parts as well as the middle part that is altered. The %r{\b\w+ \b} is a Regexp that matches words, \b is a word-boundary and \w is a word-character (short for [a-zA-Z0-9_]). Your use of String#capitalize! returns nil if no change is made. -Rob Rob Biedenharn http://agileconsultingllc.com Rob / AgileConsultingLLC.com