# Confused by back refs in gsub

**URL:** <https://www.ruby-forum.com/t/confused-by-back-refs-in-gsub/111958>\
**Category:** Ruby\
**Created:** [August 13, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/confused-by-back-refs-in-gsub/111958 "2007-08-13T20:59:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 13, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/confused-by-back-refs-in-gsub/111958/1 "2007-08-13T20:59:35Z")

</div>

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\>/,  
‘\<SUB.HEAD4\>&\</SUB.HEAD4\>’)  
I’ve tried “\1” instead of “&,” too. Same result. I’ve also tried  
putting in “?” marks to make it non-greedy. Same result.

Yields:  
\<SUB.HEAD4\>Normandy Group  
LLC\</SUB.HEAD4\>

What I want:  
\<SUB.HEAD4\>Normandy Group LLC(/SUB.HEAD4\>

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 13, 2007, 9:09pm UTC](https://www.ruby-forum.com/t/confused-by-back-refs-in-gsub/111958/2 "2007-08-13T21:09:50Z")

</div>

On 8/13/07, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What I want:  
> \<SUB.HEAD4\>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\>/,‘\<SUB.HEAD4\>\1\</SUB.HEAD4\>’)  
assert\_equal(line, ‘\<SUB.HEAD4\>Normandy Group  
LLC\</SUB.HEAD4\>’)  
end  
end

Note that you have (/SUB.HEAD4\> instead of \</SUB.HEAD4\> (the  
parenthesis)

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 13, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/confused-by-back-refs-in-gsub/111958/3 "2007-08-13T21:16:52Z")

</div>

> I’ve tried “\1” instead of “&,” too. Same result. I’ve also

If you’re hardcoding replacements like that and are certain that your  
source  
is well formed xml, you could also just skip the back references:

irb(main):001:0\> “Normandy Group  
LLC”.gsub!(/registrantName\>/, ‘SUB.HEAD4\>’)  
=\> “\<SUB.HEAD4\>Normandy Group LLC\</SUB.HEAD4\>”  
irb(main):002:0\>

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 13, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/confused-by-back-refs-in-gsub/111958/4 "2007-08-13T21:17:47Z")

</div>

Jano S. wrote:

> On 8/13/07, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > What I want:  
> > \<SUB.HEAD4\>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\>/,‘\<SUB.HEAD4\>\1\</SUB.HEAD4\>’)  
> assert\_equal(line, ‘\<SUB.HEAD4\>Normandy Group  
> LLC\</SUB.HEAD4\>’)  
> end  
> end
> 
> Note that you have (/SUB.HEAD4\> instead of \</SUB.HEAD4\> (the  
> parenthesis)

Thank you, Jano. Yes, this worked for me now.

Cheers.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 13, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/confused-by-back-refs-in-gsub/111958/5 "2007-08-13T22:05:35Z")

</div>

Felix W. wrote:

> > I’ve tried “\1” instead of “&,” too. Same result. I’ve also
> 
> If you’re hardcoding replacements like that and are certain that your  
> source  
> is well formed xml, you could also just skip the back references:
> 
> irb(main):001:0\> “Normandy Group  
> LLC”.gsub!(/registrantName\>/, ‘SUB.HEAD4\>’)  
> =\> “\<SUB.HEAD4\>Normandy Group LLC\</SUB.HEAD4\>”  
> irb(main):002:0\>

I don’t quite understand your suggestion, Felix. Yes, I believe my  
source data is well-formed XML. Are you suggesting that, somehow,  
because it is well-formed XML, I can ignore the element closings? I  
tried what I thought you meant by:

xmlfile.gsub!(//, ‘\<SUB.HEAD4\>’)

and, I got the subhead callout at the beginning of the data, but, the  
closing element still is there–/

-Peter

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 13, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/confused-by-back-refs-in-gsub/111958/6 "2007-08-13T22:16:03Z")

</div>

Alle lunedÃ¬ 13 agosto 2007, Peter B. ha scritto:

> > irb(main):002:0\>
> 
> -Peter

What Felix is suggesting is that, if the source is valid XML, then it  
will  
have the form

text

so, if you call gsub! passing a regexp matching elementName\>, it should  
replace both the opening and closing tags. When you tried, it didn’t  
work  
because you left the opening \< in the regexp, which didn’t match the  
closing  
tag (it starts with \</r, not \<r). The correct call to gsub should be:

xmlfile.gsub!(/registrantName\>/, ‘SUB.HEAD4\>’)

(by the way, notice that the regexp doesn’t match the starting ‘\<’, so  
it gets  
removed from the replacement string)

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 14, 2007, 12:06am UTC](https://www.ruby-forum.com/t/confused-by-back-refs-in-gsub/111958/7 "2007-08-14T00:06:25Z")

</div>

- Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (21:18) schrieb:

> > line.gsub!(/(.\*)\</registrantName\>/,‘\<SUB.HEAD4\>\1\</SUB.HEAD4\>’)

> Thank you, Jano. Yes, this worked for me now.

Please note that regular expressions aren’t a very good way to parse  
XML. The above expression subgroup will match everything between the  
first “” and the last “” which is  
probably not what you want.

You can can use non-greedy \*? as a workaround in this case.

mfg, simon … l

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 13, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/confused-by-back-refs-in-gsub/111958/8 "2007-08-13T22:20:04Z")

</div>

> > >
> 
> ## I don’t quite understand your suggestion, Felix. Yes, I
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I’m leaving off the opening bracket ‘\<’:

line.gsub!(/registrantName\>/, ‘SUB.HEAD4\>’)

So that it will match the opening as well as closing statement.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 14, 2007, 12:07am UTC](https://www.ruby-forum.com/t/confused-by-back-refs-in-gsub/111958/9 "2007-08-14T00:07:26Z")

</div>

- Felix W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (22:18) schrieb:

> I’m leaving off the opening bracket ‘\<’:
> 
> line.gsub!(/registrantName\>/, ‘SUB.HEAD4\>’)
> 
> So that it will match the opening as well as closing statement.

As well as any substring “registrantName\>”. And well-formed XML won’t  
guarantee that only “” and “” will  
contain that.

gsub!(/(\</?)registrantName\>/, ‘\1SUB.HEAD4\>’) should do.

But again, CDATA-sections and comments may well contain these strings.  
I’d use XSLT or some SAX-Library if it has to be ruby.

mfg, simon … l

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [August 14, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/confused-by-back-refs-in-gsub/111958/10 "2007-08-14T13:32:08Z")

</div>

Simon K. wrote:

> - Felix W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (22:18) schrieb:
> 
> > I’m leaving off the opening bracket ‘\<’:
> > 
> > line.gsub!(/registrantName\>/, ‘SUB.HEAD4\>’)
> > 
> > So that it will match the opening as well as closing statement.
> 
> As well as any substring “registrantName\>”. And well-formed XML won’t  
> guarantee that only “” and “” will  
> contain that.
> 
> gsub!(/(\</?)registrantName\>/, ‘\1SUB.HEAD4\>’) should do.
> 
> But again, CDATA-sections and comments may well contain these strings.  
> I’d use XSLT or some SAX-Library if it has to be ruby.
> 
> mfg, simon … l

Thank you, everyone. Yes, my XML is well-formed, but, it’s also pretty  
simple, and, from what our vendor tells me, pretty consistent. I just  
need to convert it to SGML for our company publishing system. XSLT is  
probably better for this, I’m sure, but, it’s enough for me just to  
learn Ruby. (-: Plus, I love Ruby.  
Thanks again.
