From: Ammar Ali Date: 2010-11-21T09:11:49+09:00 Subject: Re: Partial GSUB match / replacement On Sun, Nov 21, 2010 at 1:04 AM, Shea Barton wrote: > I need to match a somewhat complicated match using gsub, then modify > part of it, leaving the rest intact > > for example, replacing > "url(http://www.google.com)" > with > "url(HTTP://WWW.GOOGLE.COM)" > > this is the closest answer I can come up with > gsub /(url\(['"]?)([^\)'"]+)(['"]?\))/, "\\1#{"\\2".capitalize}\\3" > or > gsub /(url\(['"]?)([^\)'"]+)(['"]?\))/, "\\1\\2.capitalize\\3" > but neither of these solutions work > > It doen't have to be with gsub, I just thought this would be the most > straightforward way > > ideas? You can use a block with gsub, where the back-references are accessible via the $1, $2, etc: >> "url(http://www.google.com)".gsub(/(url\(['"]?)([^\)'"]+)(['"]?\))/) {"#{$1}#{$2.upcase}#{$3}"} => "url(HTTP://WWW.GOOGLE.COM)" Note that I replaced #capitalize with #upcase to upcase all characters not just the first one. HTH, Ammar