\s in regex

S

s moon

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
 
M

Mike Stok

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
 
A

ahoward

On Fri, 27 Jun 2003, s moon wrote:

/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
--
====================================
| Ara Howard
| NOAA Forecast Systems Laboratory
| Information and Technology Services
| Data Systems Group
| R/FST 325 Broadway
| Boulder, CO 80305-3328
| Email: (e-mail address removed)
| Phone: 303-497-7238
| Fax: 303-497-7259
| ~ > ruby -e 'p(%.\x2d\x29..intern)'
====================================
 
B

Brian Candler

what I want is to remove beginning and trailing spaces from lines.
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.
 
M

Mauricio Fernández

| ~ > 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 :)


--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
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
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top