replacing verticle white spaces?

Discussion in 'Ruby' started by George George, Jul 17, 2009.

  1. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    George George, Jul 17, 2009
    #1
    1. Advertising

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

    > 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
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --=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
     
    Mirko Viviani, Jul 17, 2009
    #2
    1. Advertising

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

    2009/7/17 Glenn Jackman <>

    > At 2009-07-17 08:11AM, "Mirko Viviani" wrote:
    > > 2009/7/17 George George <>
    > > > 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?
    >
    > --
    > Glenn Jackman
    > Write a wise saying and your name will live forever. -- Anonymous
    >
    >



    --=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
     
    Mirko Viviani, Jul 17, 2009
    #3
  4. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    George George, Jul 23, 2009
    #4
  5. George George

    Guest

    On Thu, Jul 23, 2009 at 8:12 AM, George George<> wrote:
    > 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
     
    , Jul 23, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger
    Replies:
    4
    Views:
    464
    jasonkester
    Mar 23, 2005
  2. Niels Dybdahl

    Color.white vs. Color.WHITE

    Niels Dybdahl, Oct 6, 2004, in forum: Java
    Replies:
    3
    Views:
    484
    Chris Smith
    Oct 6, 2004
  3. Philip Ronan
    Replies:
    2
    Views:
    4,701
  4. Jim in Arizona
    Replies:
    2
    Views:
    1,002
    Jayakrishnan
    Jun 7, 2008
  5. Roger

    Datagrid Verticle Header is this possible? How?

    Roger, Mar 23, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    126
    Craig Deelsnyder
    Mar 23, 2005
Loading...

Share This Page