How to truncate the spaces in the front of a line

M

Milo Luo

Hi, guys
I am new to Ruby. Do you have any idea to truncate or remove the space
in the front of a line?
Thanks very much.

Milo
 
P

Phillip Gawlowski

Hi, guys
I am new to Ruby. Do you have any idea to truncate or remove the space
in the front of a line?
irb(main):001:0> " spaces in front".gsub /^ +/,""
=> "spaces in front"
irb(main):002:0> RUBY_VERSION
=> "1.8.6"
irb(main):003:0>

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). ;)
 
P

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("^")".
 
R

Rajinder Yadav

Phillip said:
irb(main):001:0> " spaces in front".gsub /^ +/,""
=> "spaces in front"
irb(main):002:0> RUBY_VERSION
=> "1.8.6"
irb(main):003:0>

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). ;)

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

http://DevMentor.org

Do Good! - Share Freely
 
M

Milo Luo

Thanks for great explainations.
And one more question, how can you get this method so quickly? Is that
your experience or is tip there on it?

Thanks.
Milo
 
P

Phillip Gawlowski

Thanks for great explainations.
And one more question, how can you get this method so quickly? Is that
your experience or is tip there on it?

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.
 
M

Milo Luo

Phillip said:
Thanks for great explainations.
And one more question, how can you get this method so quickly? Is that
your experience or is tip there on it?

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.

Cool. I should really have a try. :)
Thank you, Phillip. You really help me a lot.

Milo
 
P

Phillip Gawlowski

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.
 
M

Milo Luo

Phillip said:
You are very welcome. :)

Asking, then answering questions are a great way to learn, so I'm glad
to be of assistance.

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.

Milo
 
P

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.

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).
 
M

Milo Luo

Phillip said:
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).

so strange...
/s should match all the spaces, including tabs,but it doesn't work.
I will try to use XML Parser.

Milo
 
F

Fleck Jean-Julien

Hello,
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 =3D 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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
M

Milo Luo

Fleck said:
Hello,


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,

Oh, I used the wrong syntax and it works now.
Thank you, Fleck. :)

Milo
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top