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 6118271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:25:01+00:00 2026-05-23T15:25:01+00:00

i’m trying to get a group of text in between 2 strings in ruby,

  • 0

i’m trying to get a group of text in between 2 strings in ruby, and i can’t seem to get the right method or use the right regex.

text:

<html>
<body>

<!-- begin posts --> 

<h1>all kinds of html<h1>
<p> blah blah </p>
<p> i've been working on this forever </p>

<!-- end posts --> 

</html>
</body>

and i just want to get everything from <!-- begin posts --> to <!-- end posts -->, inclusive, and save that block of text in a text file.

i figured out how to print the line in the beginning:

File.open("index.html").each_line do |line|
body.each {|line| puts line if line =~ /<!-- begin/}

but not the lines after up and until the last string.

i have a rubular here http://rubular.com/r/0W9QDpMGkM where i haven’t been able to figure out anything.

thanks everyone in advance.

  • 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-05-23T15:25:02+00:00Added an answer on May 23, 2026 at 3:25 pm

    Don’t do it line by line, just slurp the whole thing into a string and rip it apart:

    s    = File.read('index.html')
    want = s.match(/<!-- begin posts -->(.*)<!-- end posts -->/m)[1]
    

    And now everything between your markers is in want. Don’t forget the m modifier on the regex.

    While you’re mangling your input you can strip out the stray leading and trailing whitespace too:

    want = s.match(/<!-- begin posts -->(.*)<!-- end posts -->/m)[1].strip
    

    As Tudor notes below, you might want to use a non-greedy (.*?) for the group if you think there is any chance of multiple <!-- end posts --> markers; doesn’t hurt to be a little paranoid when they really are you to get you.

    References:

    • File.read (actually IO.read)
    • String#match
    • String#strip

    UPDATE: the match method on a string returns a MatchData object. The array access operator:

    … mtch[0] is equivalent to the special variable $&, and returns the entire matched string. mtch[1], mtch[2], and so on return the values of the matched backreferences (portions of the pattern between parentheses).

    Is used to access the matching parts. There’s only one group in the regex so [1] gets you the contents of that group without the surrounding HTML comment delimiters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

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.