From: Jano Svitok Date: 2007-08-14T04:08:58+09:00 Subject: Re: confused by back refs in gsub On 8/13/07, Peter Bailey wrote: > Can someone tell me why, in my code below, I'm getting part of the > original search in my substitution in my result, when, I'm not asking > for it, or at least, I don't think I'm asking for it. > > Thanks, > Peter > > > Original line: > Normandy Group LLC > > My Code: > xmlfile.gsub!(/(.*)<\/registrantName>/, > '\&') > I've tried "\1" instead of "\&," too. Same result. I've also tried > putting in "?" marks to make it non-greedy. Same result. > > Yields: > Normandy Group > LLC > > What I want: > Normandy Group LLC(/SUB.HEAD4> This works for me (I've used \1): require 'test/unit' class TestGsub < Test::Unit::TestCase def test_replace line = "Normandy Group LLC" line.gsub!(/(.*)<\/registrantName>/,'\1') assert_equal(line, 'Normandy Group LLC') end end Note that you have (/SUB.HEAD4> instead of (the parenthesis)