From: ishamid Date: 2006-12-03T04:05:05+09:00 Subject: Re: search and replace Thank you, David, for your pointers. I'm still very much a novice (at the level of Chris Pine's Learn to Program) so I could not follow them all, but I do hope to learn more fast. I just sent a follow-up with more detail, including the script I'm trying to modify; I hope you have a chance to look at it... Thank you again Idris On Dec 2, 11:15 am, David Vallner wrote: > Regexps and XML always tend to blow up for me. The pattern you're > searching for seems to be a complete element, why not use parser of choice> and XPath? > > With REXML, it should be something like: > > document.elements.each('//text:sequence') {|sequence| > sequence.replace_with(REXML::Text.new("\\head\n", true))} > > Substitute the XPath expression with one of desired precision. I'm a > little unsure around how REXML treats namespaces in XPath and such, but > if you know what prefix will be used in the document, that should work out. > > The script might also require a little more massaging if you're > outputting to plaintext, but treating XML like, well, XML might get the > heavy lifting of searching for patterns in it done faster if you use a > pattern language operating on the DOM structure directly.