Problem with Regular Expression

Discussion in 'Ruby' started by Mario Ruiz, May 19, 2009.

  1. Mario Ruiz

    Mario Ruiz Guest

    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/.
     
    Mario Ruiz, May 19, 2009
    #1
    1. Advertising

  2. Mario Ruiz

    F. Senault Guest

    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)
     
    F. Senault, May 19, 2009
    #2
    1. Advertising

  3. Mario Ruiz

    Mario Ruiz Guest

    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/.
     
    Mario Ruiz, May 19, 2009
    #3
  4. Mario Ruiz

    Mark Thomas Guest

    On May 19, 12:54 pm, Mario Ruiz <> 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!)
     
    Mark Thomas, May 20, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. godfather2

    Regular Expression problem

    godfather2, Jul 20, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,012
    Shawn Corey
    Jul 20, 2003
  2. VSK
    Replies:
    2
    Views:
    2,349
  3. Nazir
    Replies:
    3
    Views:
    5,095
    jayendra gadhavi
    Jan 2, 2008
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    871
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    12,018
    vforvikash
    Dec 29, 2008
Loading...

Share This Page