what I want is to remove beginning and trailing spaces from lines.
this is a failed try.
ifile.each { | line |
ofile.print line.sub(/^\s+/,"").sub(/\s+$/,"")
}
this removes spaces from begin and end of lines
but it also removes blank lines.
it seems \s matches \r or \n also.
this is another one, this time it works.
ifile.each { | line |
if line =~ /^\s*$/ # take care of blank lines
ofile.print line
else
ofile.print line.sub(/^\s+/,"").sub(/\s+$/,"")
end
}
but can i do this with one line without if-then-else?
thanks
Not the answer to your question necessarily, but try String#strip (or its
bang cousin)
···
-----Original Message-----
From: s moon [mailto:internetletter@yahoo.com]
Sent: Friday, June 27, 2003 8:47 AM
To: ruby-talk ML
Subject: \s in regex
what I want is to remove beginning and trailing spaces from lines.
this is a failed try.
ifile.each { | line |
ofile.print line.sub(/^\s+/,"").sub(/\s+$/,"")
}
this removes spaces from begin and end of lines
but it also removes blank lines.
it seems \s matches \r or \n also.
this is another one, this time it works.
ifile.each { | line |
if line =~ /^\s*$/ # take care of blank lines
ofile.print line
else
ofile.print line.sub(/^\s+/,"").sub(/\s+$/,"")
end
}
but can i do this with one line without if-then-else?
thanks
How about String#strip! ?
untested
ifile.each { |line|
ofile.print line.strip!
}
···
On Fri, 27 Jun 2003 07:36:35 +0000, s moon wrote:
what I want is to remove beginning and trailing spaces from lines.
this is a failed try.
ifile.each { | line |
ofile.print line.sub(/^\s+/,“”).sub(/\s+$/,“”)
}
this removes spaces from begin and end of lines
but it also removes blank lines.
it seems \s matches \r or \n also.
–
Simon Strandgaard
In article eec52c7a.0306270536.6178a985@posting.google.com,
what I want is to remove beginning and trailing spaces from lines.
this is a failed try.
ifile.each { | line |
ofile.print line.sub(/^\s+/,“”).sub(/\s+$/,“”)
}
this removes spaces from begin and end of lines
but it also removes blank lines.
it seems \s matches \r or \n also.
One approach might be to use String#strip as suggested by another
poster, and to use IO#puts to supply the line ending e.g.
ifile.each { |line|
ofile.puts line.strip
}
This is “quick and dirty” and will add a \n to the last line from the
input even if there wasn’t a final \n in the inpyt stream.
Hope this helps,
Mike
···
s moon internetletter@yahoo.com wrote:
–
mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA
/usr/home/howardat/eg/ruby > cat blank.rb
#!/usr/bin/env ruby
re = %r/^\s*(.?)\s$/o
DATA.each do |line|
p re.match(line).to_a
end
END
zero zero
one one one
three
five
/usr/home/howardat/eg/ruby > ruby blank.rb
[" zero zero\t “, “zero zero”]
[” one\t one one “, “one\t one one”]
[”“, “”]
[” three “, “three”]
[”“, “”]
[” five\t", “five”]
-a
···
On Fri, 27 Jun 2003, s moon wrote:
–
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
~ > ruby -e ‘p(%.\x2d\x29…intern)’
====================================
s,
what I want is to remove beginning and trailing
spaces from lines.
this is a failed try.
ifile.each { | line |
ofile.print line.sub(/^\s+/,“”).sub(/\s+$/,“”)
}
this removes spaces from begin and end of lines
but it also removes blank lines.
it seems \s matches \r or \n also.
That is correct. /\s/ is the same as /[\s\t\r\n\f]/ or, in English,
spaces, TABs, Carriage Returns (CR), Line Feeds (LF), and Form Feeds (FF).
If all you want to remove is spaces, use / /:
ifile.each { | line |
ofile.print line.sub(/^ +/,“”).sub(/ +$/,“”)
}
I hope this helps.
- Warren Brown
what I want is to remove beginning and trailing spaces from lines.
this is a failed try.
ifile.each { | line |
ofile.print line.sub(/^\s+/,“”).sub(/\s+$/,“”)
}
this removes spaces from begin and end of lines
but it also removes blank lines.
it seems \s matches \r or \n also.
A blank line will be seen as “\n” in your example, and yes it is eaten by \s:
irb(main):001:0> “\n”.sub(/^\s+/,“”)
=> “”
But it’s not eaten at the other end, since $ doesn’t match the end of
string, it matches before the newline:
irb(main):002:0> “\n”.sub(/\s+$/,“”)
=> “\n”
So you can solve it like this:
line.sub(/^\s*(.*?)\s*$/,'\1')
where .*? means “match any character any number of times, but eat as few
characters as possible whilst still allowing the whole regexp to match”
Or else, as others have mentioned, just remove it anyway (String#strip does
this) and add a “\n” back again.
Regards,
Brian.
batsman@tux-chan:~$ ruby -v
ruby 1.8.0 (2003-03-03) [i686-linux]
batsman@tux-chan:~$ ruby -e ‘p(%.\x2d\x29…intern)’
:“-)”
batsman@tux-chan:~$ /usr/bin/ruby -v
ruby 1.6.8 (2002-12-24) [i386-linux]
batsman@tux-chan:~$ /usr/bin/ruby -e ‘p(%.\x2d\x29…intern)’
batsman@tux-chan:~$ ruby -e ‘p “)”.intern’
:“)”
batsman@tux-chan:~$ /usr/bin/ruby -e ‘p “)”.intern’
Beware
···
On Sat, Jun 28, 2003 at 12:28:16AM +0900, ahoward wrote:
~ > ruby -e ‘p(%.\x2d\x29…intern)’
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_
_ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
Actually, typing random strings in the Finder does the equivalent of
filename completion.
– Discussion on file completion vs. the Mac Finder
damn! back to the drawing board…
-a
···
On Sat, 28 Jun 2003, Mauricio [iso-8859-1] Fernández wrote:
On Sat, Jun 28, 2003 at 12:28:16AM +0900, ahoward wrote:
~ > ruby -e ‘p(%.\x2d\x29…intern)’
batsman@tux-chan:~$ ruby -v
ruby 1.8.0 (2003-03-03) [i686-linux]
batsman@tux-chan:~$ ruby -e ‘p(%.\x2d\x29…intern)’
:“-)”
batsman@tux-chan:~$ /usr/bin/ruby -v
ruby 1.6.8 (2002-12-24) [i386-linux]
batsman@tux-chan:~$ /usr/bin/ruby -e ‘p(%.\x2d\x29…intern)’
batsman@tux-chan:~$ ruby -e ‘p “)”.intern’
:“)”
batsman@tux-chan:~$ /usr/bin/ruby -e ‘p “)”.intern’
Beware
–
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
~ > ruby -e ‘p(%.\x2d\x29…intern)’
====================================