Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:22:02+00:00 2026-06-05T17:22:02+00:00

Using Ruby, I’m trying to parse some documentation in which I need to split

  • 0

Using Ruby, I’m trying to parse some documentation in which I need to split blocks of text, each with a heading and followed by an unknown length of text, and push them to an array;

SECTION 1. A HEADING

Some undetermined length of text,
which can be multiple lines and paragraphs.


SECTION 2. ANOTHER HEADING

Another big block of text.

should become

["SECTION 1. A HEADING

Some undetermined length of text,
which can be multiple lines and paragraphs.",
"SECTION 2. ANOTHER HEADING

Another big block of text."]

I could just use string.split(/\n\n\n/), but I want something more specific as I can’t guarantee that each section will have two blank lines after it. A little more experimenting led me to this;

string.split(/(?:^|\n)(SECTION.+\n)/).each do |s|
  sections << s
end

but I’d have to process the output again to get what I need.

Is there some way to get this done without having to do multiple passes?

Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:22:07+00:00Added an answer on June 5, 2026 at 5:22 pm

    You can use String#scan with multiline-mode regexp and positive look-ahead:

    text = <<ENDTEXT
    SECTION 1. A HEADING
    
    Some undetermined length of text,
    which can be multiple lines and paragraphs.
    
    
    SECTION 2. ANOTHER HEADING
    
    Another big block of text.
    ENDTEXT
    
    header = /^SECTION\s+\d+\./
    sections = text.scan(/(?m)#{header}.*?(?=#{header}|\Z)/)
    
    puts sections.join("\n---\n")
    
    # =>
    SECTION 1. A HEADING
    
    Some undetermined length of text,
    which can be multiple lines and paragraphs.
    
    
    
    ---
    SECTION 2. ANOTHER HEADING
    
    Another big block of text.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Ruby I'm trying to split the following text with a Regex ~foo\~\=bar =cheese~monkey
I'm using ruby 1.9.2 I'm trying to parse a CSV file that contains some
I'm using ruby 1.9 and trying to find out which regex I need to
Using Ruby (newb) and Regex, I'm trying to parse the street number from the
Using Ruby 1.9.2 I need to parse a CSV file, and output lines with
Using Ruby on Rails. I'm trying to sort a query by number (saved as
Using ruby-1.9.3... I've read some of the canonical blog posts on the subject of
Using Ruby and Haml, I have a property which is cost. I believe (im
Using Ruby, how can I perform background and foreground text colorization for output in
Using Ruby 1.8.7 I need to grab everything up to a certain word -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.