Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Problem with Regular Expression

Reply
Thread Tools

Problem with Regular Expression

 
 
Mario Ruiz
Guest
Posts: n/a
 
      05-19-2009

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...
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
F. Senault
Guest
Posts: n/a
 
      05-19-2009
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(oldValu e)}<\//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)
 
Reply With Quote
 
 
 
 
Mario Ruiz
Guest
Posts: n/a
 
      05-19-2009
F. Senault wrote:
> Le 19 mai � 18:54, Mario Ruiz a �crit :
>


Yep It works!!!

Thank you very much.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Mark Thomas
Guest
Posts: n/a
 
      05-20-2009
On May 19, 12:54*pm, Mario Ruiz <(E-Mail Removed)> 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!)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments