replacing verticle white spaces?

G

George George

Any ideas on how to replace a verticle white space?

for example

tetetetetetetete
tetetettetetetete



rererererererere
rerererererererr


such that you end up with

tetetetetetetettete
etetetetetetetetete

rererererererererer
erererererererereer


Thank you
 
M

Mirko Viviani

puts s.gsub(/\r\n(\r\n)+/,"\r\n\r\n")

Just adjust the number of \r\n ;-)

p.s. have a look mainly at (\r\n)+ : a serie of one or more \r\n


2009/7/17 George George said:
Any ideas on how to replace a verticle white space?

for example

tetetetetetetete
tetetettetetetete



rererererererere
rerererererererr


such that you end up with

tetetetetetetettete
etetetetetetetetete

rererererererererer
erererererererereer


Thank you


--=20
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"=93Machines take me by surprise with great frequency.=94 A. Turing
 
M

Mirko Viviani

Well... I just tried out with \r\n.... neverthless the solution is the same
;)

2009/7/17 Glenn Jackman said:
At said:
2009/7/17 George George said:
Any ideas on how to replace a verticle white space?
[...]
puts s.gsub(/\r\n(\r\n)+/,"\r\n\r\n")

I would write:
s.gsub(/((?:\r?\n){2})(?:\r?\n)*/, '\1')

In Ruby, are newlines represented just as "\n" regardless of your
platform?


--=20
-- Mirko Viviani --
GPG-PGP Public Key: 0xE4E8FAB1
Fingerprint: 14D3 A373 E926 7737 DF32 502B A4C4 1CE2 E4E8 FAB1
***********************************************
"=93Machines take me by surprise with great frequency.=94 A. Turing
 
G

George George

Hi,
Thanks you for the replies. Both approaches did not seem to achieve what
i envisaged. Is because of reading the input as a string? rather than as
a file?

Maybe to rephrase what i really wanted was to be able to delete the
repeated lines and only remain with a single empty line.
meaning if a file had the lines;


tetetetetetetete
tetetettetetetete



rererererererere
rerererererererr

remove the empty lines leaving only a single one

tetetetetetetete
tetetettetetetete

rererererererere
rerererererererr

Thank you
George
 
B

brabuhr

Thanks you for the replies. Both approaches did not seem to achieve what
i envisaged. Is because of reading the input as a string? rather than as
a file?

Maybe to rephrase what i really wanted was to be able to delete the
repeated lines and only remain with a single empty line.
meaning if a file had the lines;

tetetetetetetete
tetetettetetetete



rererererererere
rerererererererr

remove the empty lines leaving only a single one

tetetetetetetete
tetetettetetetete

rererererererere
rerererererererr

Do you have a more specific test case (with code) that is failing?
Reading from a file and using Glenn Jackman's gsub, seems to produce
your desired output:
cat z tetetetetetetete
tetetettetetetete



rererererererere
rerererererererr
ruby -ve 'puts File.new("z").read.gsub(/((?:\r?\n){2})(?:\r?\n)*/, "\\1")'
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-linux]
tetetetetetetete
tetetettetetetete

rererererererere
rerererererererr
unix2dos z
unix2dos: converting file z to DOS format ...
ruby -ve 'puts File.new("z").read.gsub(/((?:\r?\n){2})(?:\r?\n)*/, "\\1")'
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-linux]
tetetetetetetete
tetetettetetetete

rererererererere
rerererererererr
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top