From: Phillip Gawlowski Date: 2009-12-27T16:23:59+09:00 Subject: Re: How to truncate the spaces in the front of a line On 27.12.2009 07:32, Milo Luo wrote: > Hi, Phillip > Here comes the new problem. > I want to use this gsub to substitue the spaces in the front of some > lines in a XML file. I have tried your method with string variable, but > it seems not work for this XML file. I check this file very carefully, I > found the spaces before some lines are 3 tabs. Well, tabs are special characters "\t", so you'd have to write a regex to match spaces *and* \t characters, eventually mixed. That's the point where I'd look into an XML parser, to read in the XML, and write it out again since you'd deal with loads of special characters, anyway, and regexen give me headaches. ;) REXML is included with Ruby 1.8.x, and its documentation available here: http://www.germane-software.com/software/rexml_doc However, its documentation is a bit unwieldy and lacking (certainly not for the faint of heart). Or Nokogiri (which will have to be installed extra, preferably via RubyGems): nokogiri.org Googling for "nokogiri prettify XML", I get this link: http://emmanueloga.wordpress.com/2009/09/29/pretty-printing-xhtml-with-nokogiri-and-xslt/ That should work for prettifying XML. ;) However, I wouldn't actually bother to prettify it. After all, XML is intended to be used by machines, and not so much humans, and XML parsers have to be able to deal with the whitespace (i.e. spaces and tabs). -- Phillip Gawlowski