Milo
--
Posted via http://www.ruby-forum.com/.
Translation:
String#gsub applies the regexp given as first argument to a given
String, and replaces the matches with the second argument.
The regexp "^ +" (the // notation is one of the many ways to tell Ruby
we are using a regexp) means "At the beginning of the line ("^"), find
any spaces(" "), and then stop ("+")". More or less, anyway (I'm not a
Regexp guru). ;)
--
Phillip Gawlowski
> The regexp "^ +" (the // notation is one of the many ways to tell Ruby
> we are using a regexp) means "At the beginning of the line ("^"), find
> any spaces(" "), and then stop ("+")". More or less, anyway (I'm not a
> Regexp guru). ;)
Actually, toss that explanation where it belongs: The waste receptacle.
The regex means "match one or more spaces (" +"), at the beginning of
the line("^")".
--
Phillip Gawlowski
a more convenient way is to use one of the strip methods
irb(main):004:0> " spaces in front".lstrip
=> "spaces in front"
irb(main):005:0> "spaces in back ".rstrip
=> "spaces in back"
irb(main):006:0> " spaces on both side ".strip
=> "spaces on both side"
--
Kind Regards,
Rajinder Yadav
Do Good! - Share Freely
Thanks.
Both. ;)
For one, experience allows me to remember where, roughly, something is
in the documentation, or if something that I want to do exists already.
Then it is down to reading the documentation. ;)
Ruby is, for the most part, logically organized (once you now the lingo).
In your example, you have a String (appending .class to anything should
reveal the object's class, [].class reveals that this is an Array), so
you look at what methods the String class offers.
With a little experimentation in irb, you can see if what you found is
useful. :)
A few pointers:
You can access Ruby's documentation online at ruby-doc.org.
Another help is the excellent Pickaxe book (Programming Ruby, 2nd
Edition is the most relevant for 1.8.x; 3rd Edition for Ruby 1.9.x),
which works as a Ruby tutorial (for experienced developers, anyway), and
an excellent reference to the language.
--
Phillip Gawlowski
Cool. I should really have a try. :)
Thank you, Phillip. You really help me a lot.
> Cool. I should really have a try. :)
> Thank you, Phillip. You really help me a lot.
You are very welcome. :)
Asking, then answering questions are a great way to learn, so I'm glad
to be of assistance.
--
Phillip Gawlowski
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.
<code>
require 'fileutils'
#Some basic variables
LPT_FILE_NAME="abc.xml"
File.file? LPT_FILE_NAME
#Open XML file
f = File.open(LPT_FILE_NAME)
begin
while (line = f.readline)
line.chomp
if line =~ /<Properties>/
while ( line !~ /<\/Properties>/ && line =
f.readline)
#puts line.gsub /^\/t+/,"" if line =~
/<Name>/
if line =~ /<Name>/
name = line..gsub /^\/s+/,""
puts name
end
end
end
end
rescue EOFError
f.close
end
</code>
The abc.xml file should simplely look like this:
<code>
<Properties>
<Name>TestData</Name>
<LoopCount>1</LoopCount>
<Priority>100</Priority>
</Properties>
</code>
Could you give me a hand?
Thank you.
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
so strange...
/s should match all the spaces, including tabs,but it doesn't work.
I will try to use XML Parser.
> so strange...
> /s should match all the spaces, including tabs,but it doesn't work.
> I will try to use XML Parser.
Well, it would be more useful if you used \s instead (wih a backslash
and not a slash). Try with
name = line.gsub(/^\s+/,"")
By the way, all useful shortcuts in regexp start with a backslash and
not a slash (usually reserved to delimit the regexp)
Cheers,
--
JJ Fleck
PCSI1 Lycée Kléber
Oh, I used the wrong syntax and it works now.
Thank you, Fleck. :)