F. Senault
5/19/2009 5:02:00 PM
Le 19 mai à 18:54, Mario Ruiz 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
--
When your pubic hair's on fire, something's wrong
When you think you're the Messiah, something's wrong
When you mistake a plane for Venus, something's wrong
When your girlfriend's got a penis... (K's Choice, Something's wrong)