From: Rob Biedenharn Date: 2008-04-10T03:23:10+09:00 Subject: Re: capitalizing words On Apr 9, 2008, at 2:04 PM, Peter Bailey wrote: > Rob Biedenharn wrote: >> On Apr 9, 2008, at 8:24 AM, Peter Bailey wrote: >>>> end >>>> >>>> Rob Biedenharn http://agileconsultingllc.com >>>> Rob@AgileConsultingLLC.com >>> >>> Thanks, Rob. This works beautifully, except that I need that last >>> in my output. It's being stripped with your code. I don't >>> see >>> why, because it's just your $3, isn't it? >>> -- >>> Posted via http://www.ruby-forum.com/. >> >> You said to Todd: >> The original text is just: >> THE QUICK BROWN FOX JUMPED OVER THE >> LAZY DOG.<\/emph>/ >> >> I assumed that the "<\/emph>/" part was a cut-n-paste of a regexp for >> the email (which is one reason that I change from // to %r{} >> construction of the Regexp so the / wouldn't have to be escaped. You >> may have to change the second group to (.*?) [reluctant match rather >> than greedy match] or adjust the third group to exactly match your >> input. >> >> -Rob >> >> Rob Biedenharn http://agileconsultingllc.com >> Rob@AgileConsultingLLC.com > > Rob, > So, here's my original file: > THE QUICK BROWN FOX JUMPED OVER THE > LAZY DOG. OK, change this to a regexp: 1. surround with the regexp literal bits %r{THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.}m 2. add the grouping ()'s %r{()(THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.)()}m 3. replace text with wildcards .* or .*? %r{()(.*?)()}m 4. (optional?) add anchor ^ %r{^()(.*?)()}m I'm assuming that is not the WHOLE file since the tags are not closed. It it quite likely that .* is slurping a lot more that you think so that's why I've change this to .*? which matches as little as possible while continuing to succeed. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com > Here's my code, from you: > 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. > The Quick Brown Fox Jumped Over The > Lazy Dog. > > Thanks, > Peter