# Problem with Regular Expression

**URL:** <https://www.ruby-forum.com/t/problem-with-regular-expression/168335>\
**Category:** Ruby\
**Created:** [May 19, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/problem-with-regular-expression/168335 "2009-05-19T18:54:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 19, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/problem-with-regular-expression/168335/1 "2009-05-19T18:54:10Z")

</div>

I’m replacing a string using:  
xmlString.gsub!(/:#{tagName}\>#{oldValue}\<//i,":" + tagName + “\>” +  
value + “\</”)

It works perfectly but… if oldValue contains the character +, it  
doesn’t work.

Do you know how to solve this?

Thanks a lot…

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 19, 2009, 7:05pm UTC](https://www.ruby-forum.com/t/problem-with-regular-expression/168335/2 "2009-05-19T19:05:03Z")

</div>

On Wed, May 20, 2009 at 01:54:24AM +0900, Mario R. wrote:

> I’m replacing a string using:  
> xmlString.gsub!(/:#{tagName}\>#{oldValue}\<//i,":" + tagName + “\>” +  
> value + “\</”)
> 
> It works perfectly but… if oldValue contains the character +, it  
> doesn’t work.
> 
> Do you know how to solve this?

Use Regexp.escape():

xmlString.gsub!(/:#{tagName}\>#{Regexp.escape(oldValue)}\<//i,":" +  
tagName + “\>” +  
value + “\</”)

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 19, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/problem-with-regular-expression/168335/3 "2009-05-19T19:06:15Z")

</div>

Le 19 mai à 18:54, Mario R. a écrit :

> I’m replacing a string using:  
> xmlString.gsub!(/:#{tagName}\>#{oldValue}\<//i,":" + tagName + “\>” +  
> value + “\</”)
> 
> It works perfectly but… if oldValue contains the character +, it  
> doesn’t work.
> 
> Do you know how to solve this?

When you interpolate strings into a regexp, the special chars are taken  
as part of the regexp (meaning your ‘+’ means one or more of the  
expression before). If you want to match literals, use Regexp.escape :

xmlString.gsub!(/:#{Regexp.escape(tagName)}\>#{Regexp.escape(oldValue)}\<//i  
…

> > Regexp.escape(“abc+”)  
> > =\> “abc\+”
> 
> > Regexp.escape(“abc[]+”)  
> > =\> “abc\[\]\+”

Fred

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 19, 2009, 7:13pm UTC](https://www.ruby-forum.com/t/problem-with-regular-expression/168335/4 "2009-05-19T19:13:34Z")

</div>

F. Senault wrote:

> Le 19 mai ï¿½ 18:54, Mario R. a ï¿½crit :

Yep It works!!!

Thank you very much.

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 20, 2009, 1:31pm UTC](https://www.ruby-forum.com/t/problem-with-regular-expression/168335/5 "2009-05-20T13:31:05Z")

</div>

On May 19, 12:54 pm, Mario R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m replacing a string using:  
> xmlString.gsub!(/:#{tagName}\>#{oldValue}\<//i,“:” + tagName + “\>” +  
> value + “\</”)

In general, it’s not a good idea to parse XML with regular  
expressions. They tend to be more fragile. What happens if there is an  
attribute on the tag? Extra spaces around the brackets? The expression  
breaks.

If you want something more robust, use an XML parser. As a bonus, your  
code will also be more readable.

doc = Nokogiri::XML(xmlString)  
doc.xpath(“//mytag”).each do |tag|  
tag.content = value  
end

– Mark

(I’m surprised Aaron didn’t respond with such an example!)
