Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to truncate the spaces in the front of a line

1 view
Skip to first unread message

Milo Luo

unread,
Dec 26, 2009, 10:22:08 PM12/26/09
to
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
--
Posted via http://www.ruby-forum.com/.

Phillip Gawlowski

unread,
Dec 26, 2009, 10:40:29 PM12/26/09
to
On 27.12.2009 04:22, Milo Luo wrote:
> 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). ;)

--
Phillip Gawlowski

Phillip Gawlowski

unread,
Dec 26, 2009, 10:48:58 PM12/26/09
to
On 27.12.2009 04:40, Phillip Gawlowski wrote:

> 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

Rajinder Yadav

unread,
Dec 26, 2009, 11:02:12 PM12/26/09
to

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

Milo Luo

unread,
Dec 26, 2009, 11:20:40 PM12/26/09
to
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.

Phillip Gawlowski

unread,
Dec 26, 2009, 11:27:34 PM12/26/09
to
On 27.12.2009 05:20, Milo Luo wrote:
> 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.

--
Phillip Gawlowski

Milo Luo

unread,
Dec 27, 2009, 12:06:54 AM12/27/09
to

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

Phillip Gawlowski

unread,
Dec 27, 2009, 12:40:27 AM12/27/09
to
On 27.12.2009 06:06, Milo Luo wrote:

> 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

Milo Luo

unread,
Dec 27, 2009, 1:32:09 AM12/27/09
to

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.

Phillip Gawlowski

unread,
Dec 27, 2009, 2:23:59 AM12/27/09
to
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

Milo Luo

unread,
Dec 27, 2009, 3:17:35 AM12/27/09
to

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

Fleck Jean-Julien

unread,
Dec 27, 2009, 4:07:30 AM12/27/09
to
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 = 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

Milo Luo

unread,
Dec 27, 2009, 4:31:40 AM12/27/09
to

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

0 new messages