Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4309

Re: gsub and multiple-replacement

From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: gsub and multiple-replacement
Date 2011-05-11 18:59 -0500
Organization Service de news de lacave.net
Message-ID <66c4f50170c6af00f7d902793406d8b9@ruby-forum.com> (permalink)
References <eb286679b6d688c796525a6786d4394f@ruby-forum.com>

Show all headers | View raw


And here is a xml parsing solution:

require 'rexml/document'
include REXML

xml =<<ENDOFXML
#your xml here
ENDOFXML

replacements = {
  '{username}' => 'BOB',
  '{password}' => 'BOB-PASSWORD',
  '{userroot}' => 'BOB-USERROOT'
}

pattern = '{.*?}'
doc = REXML::Document.new(xml)

XPath.each(doc, '//string') do |element|
  text = element.text
  element.text = text.gsub(/#{pattern}/, replacements)
end

puts doc.to_s


Note that you are using an unfortunate character in the string:

   domain\{userroot}

A backslash has a special meaning in ruby strings.  You need to change 
that to a forward slash(or escape it).

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

gsub and multiple-replacement Greg Hacke <greghacke@gmail.com> - 2011-05-10 16:16 -0500
  Re: gsub and multiple-replacement 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-10 16:25 -0500
  Re: gsub and multiple-replacement 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-10 16:43 -0500
    Re: gsub and multiple-replacement Greg Hacke <greghacke@gmail.com> - 2011-05-10 19:49 -0500
      Re: gsub and multiple-replacement 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-10 21:07 -0500
      Re: gsub and multiple-replacement Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-05-11 03:00 -0500
  Re: gsub and multiple-replacement Phillip Gawlowski <cmdjackryan@googlemail.com> - 2011-05-10 16:28 -0500
  Re: gsub and multiple-replacement Greg Hacke <greghacke@gmail.com> - 2011-05-11 08:45 -0500
    Re: gsub and multiple-replacement Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-05-11 10:01 -0500
  Re: gsub and multiple-replacement 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 18:31 -0500
  Re: gsub and multiple-replacement 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 18:59 -0500

csiph-web