RegExp problem

Discussion in 'Ruby' started by Charlie S, May 17, 2007.

  1. Charlie S

    Charlie S Guest

    I need to match a block of text as follows

    Discard all lines which begin with //
    Capture all lines in between as single strings

    So

    // section 1
    bit

    bit more

    // section 2


    more stuff

    // section 3


    and yet more

    Should yeild 3 strings:
    [ bit\n\nbit more\n\n]
    [\n\n\n more stuff\n\n]
    [\n\n\nand yet more\n]

    Can anyone help?

    I've tried:

    builds = s.scan(%r[\n//[^\n]*\n([^\n]*\n)+?]m)

    but this yields
    [ bit]
    [\n]
    [\n]

    how can I make the group capture more than a line?

    --
    Posted via http://www.ruby-forum.com/.
     
    Charlie S, May 17, 2007
    #1
    1. Advertising

  2. Charlie S

    Alex Young Guest

    Charlie S wrote:
    > I need to match a block of text as follows
    >
    > Discard all lines which begin with //
    > Capture all lines in between as single strings
    >
    > So
    >
    > // section 1
    > bit
    >
    > bit more
    >
    > // section 2
    >
    >
    > more stuff
    >
    > // section 3
    >
    >
    > and yet more
    >
    > Should yeild 3 strings:
    > [ bit\n\nbit more\n\n]
    > [\n\n\n more stuff\n\n]
    > [\n\n\nand yet more\n]
    >
    > Can anyone help?
    >
    > I've tried:
    >
    > builds = s.scan(%r[\n//[^\n]*\n([^\n]*\n)+?]m)
    >
    > but this yields
    > [ bit]
    > [\n]
    > [\n]
    >
    > how can I make the group capture more than a line?
    >

    s.split(%r{//.*})
    => ["", "\n bit\n\nbit more\n\n", "\n\n\n more stuff\n\n", "\n\n\nand
    yet more\n\n"]

    Close enough?

    --
    Alex
     
    Alex Young, May 17, 2007
    #2
    1. Advertising

  3. On 5/17/07, Charlie S <> wrote:
    > I need to match a block of text as follows
    >
    > Discard all lines which begin with //
    > Capture all lines in between as single strings
    >
    > So
    >
    > // section 1
    > bit
    >
    > bit more
    >
    > // section 2
    >
    >
    > more stuff
    >
    > // section 3
    >
    >
    > and yet more
    >
    > Should yeild 3 strings:
    > [ bit\n\nbit more\n\n]
    > [\n\n\n more stuff\n\n]
    > [\n\n\nand yet more\n]
    >
    > Can anyone help?
    >
    > I've tried:
    >
    > builds = s.scan(%r[\n//[^\n]*\n([^\n]*\n)+?]m)
    >
    > but this yields
    > [ bit]
    > [\n]
    > [\n]
    >
    > how can I make the group capture more than a line?
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    Try something like this.
    You can clean it up as necessary.

    s = "// section 1\nbit\n\nbit more\n\n// section 2\n\nmore stuff\n\n//
    section 3\n\nand yet more"
    s.split(/\/\/\s+section\s+\d+/).each {|x| p x}

    Harry

    --

    A Look into Japanese Ruby List in English
    http://www.kakueki.com/
     
    Harry Kakueki, May 17, 2007
    #3
    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. Greg Hurrell
    Replies:
    4
    Views:
    167
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    507
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    379
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    196
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    191
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page