From: Rob Biedenharn Date: 2008-04-10T03:46:53+09:00 Subject: Re: capitalizing words On Apr 9, 2008, at 2:13 PM, Jens Wille wrote: > hi peter! > > Peter Bailey [2008-04-09 20:04]: >> Dir.chdir("C:/users/pb4072/documents") do |d| file = >> File.read("test1.txt") output = >> file.gsub(%r{^(> 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 >> >> Here's what I get. It works great, but, I don't understand why >> the $3 text is simply blown away. > because it's reset when you're doing that gsub on $2. the capture > variables only refer to the *last* match. so you have to capture > them into local variables first (can't think of a better way right > now). > > cheers > jens > > -- > Jens Wille, Dipl.-Bibl. (FH) > prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre > Kunsthistorisches Institut der Universität zu Köln > Albertus-Magnus-Platz, D-50923 Köln > Tel.: +49 (0)221 470-6668, E-Mail: jens.wille@uni-koeln.de > http://www.prometheus-bildarchiv.de/ Ah yes! Good catch, Jens. Peter, you only *need* to capture $3, but it would make sense to get them all: head, content, tail = $1, $2, $3 "#{head}#{content.gsub(%r{\b\w+\b}){|w|w.capitalize}}#{tail}" -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com