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

  • Home
  • SEARCH
  • 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 8614737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:11:30+00:00 2026-06-12T05:11:30+00:00

I have a string (@description) that contains HTML code and I want to extract

  • 0

I have a string (@description) that contains HTML code and I want to extract the content between two elements. It looks something like this

<b>Content title<b><br/>
*All the content I want to extract*
<a href="javascript:print()">

I’ve managed to do something like this

@want = @description.match(/Content title(.*?)javascript:print()/m)[1].strip

But obviously this solution is far from perfect as I get some unwanted characters in my @want string.

Thanks for your help

Edit:

As requested in the comments, here is the full code:

I’m already parsing an HTML document doing something where the following code:

@description = @doc.at_css(".entry-content").to_s
puts @description

returns:

<div class="post-body entry-content">
<a href="http://www.photourl"><img alt="Photo title" height="333"     src="http://photourl.com" width="500"></a><br><br><div style="text-align: justify;">
Some text</div>
<b>More text</b><br><b>More text</b><br><br><ul>
<li>Numered item</li>
<li>Numered item</li>
<li>Numered item</li>
</ul>
<br><b>Content Title</b><br>
Some text<br><br>
Some text(with links and images)<br>
Some text(with links and images)<br>
Some text(with links and images)<br>
<br><br><a href="javascript:print()"><img src="http://url.com/photo.jpg"></a>
<div style="clear: both;"></div>
</div>

The text can include more paragraphs, links, images, etc. but it always starts with the “Content Title” part and ends with the javascript reference.

  • 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-12T05:11:32+00:00Added an answer on June 12, 2026 at 5:11 am

    This XPath expression selects all (sibling) nodes between the nodes $vStart and $vEnd:

      $vStart/following-sibling::node()
               [count(.|$vEnd/preceding-sibling::node())
               =
                count($vEnd/preceding-sibling::node())
               ]
    

    To obtain the full XPath expression to use in your specific case, simply substitute $vStart with:

    /*/b[. = 'Content Title']
    

    and substitute $vEnd with:

    /*/a[@href = 'javascript:print()']
    

    The final XPath expressions after the substitutions is:

    /*/b[. = 'Content Title']/following-sibling::node()
             [count(.|/*/a[@href = 'javascript:print()']/preceding-sibling::node())
             =
              count(/*/a[@href = 'javascript:print()']/preceding-sibling::node())
             ]
    

    Explanation:

    This is a simple corollary of the Kayessian formula for the intersection of two nodesets $ns1 and $ns2:

    $ns1[count(.|$ns2) = count($ns2)]
    

    In our case, the set of all nodes between the nodes $vStart and $vEnd is the intersection of two node-sets: all following siblings of $vStart and all preceding siblings of $vEnd.

    XSLT – based verification:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:variable name="vStart" select="/*/b[. = 'Content Title']"/>
     <xsl:variable name="vEnd" select="/*/a[@href = 'javascript:print()']"/>
    
     <xsl:template match="/">
         <xsl:copy-of select=
         "$vStart/following-sibling::node()
                   [count(.|$vEnd/preceding-sibling::node())
                   =
                    count($vEnd/preceding-sibling::node())
                   ]
         "/>
    ==============
    
         <xsl:copy-of select=
         "/*/b[. = 'Content Title']/following-sibling::node()
                   [count(.|/*/a[@href = 'javascript:print()']/preceding-sibling::node())
                   =
                    count(/*/a[@href = 'javascript:print()']/preceding-sibling::node())
                   ]
         "/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document (converted to a well-formed XML document):

    <div class="post-body entry-content">
        <a href="http://www.photourl">
            <img alt="Photo title" height="333"     src="http://photourl.com" width="500"/>
        </a>
        <br />
        <br />
        <div style="text-align: justify;">
        Some text</div>
        <b>More text</b>
        <br />
        <b>More text</b>
        <br />
        <br />
        <ul>
            <li>Numered item</li>
            <li>Numered item</li>
            <li>Numered item</li>
        </ul>
        <br />
        <b>Content Title</b>
        <br />
        Some text
        <br />
        <br />
        Some text(with links and images)
        <br />
        Some text(with links and images)
        <br />
        Some text(with links and images)
        <br />
        <br />
        <br />
        <a href="javascript:print()">
            <img src="http://url.com/photo.jpg"/>
        </a>
        <div style="clear: both;"></div>
    </div>
    

    the two XPath expressions (with and without variable references) are evaluated and the nodes selected in each case, conveniently delimited, are copied to the output:

    <br/>
        Some text
        <br/>
    <br/>
        Some text(with links and images)
        <br/>
        Some text(with links and images)
        <br/>
        Some text(with links and images)
        <br/>
    <br/>
    <br/>
    ==============
    
         <br/>
        Some text
        <br/>
    <br/>
        Some text(with links and images)
        <br/>
        Some text(with links and images)
        <br/>
        Some text(with links and images)
        <br/>
    <br/>
    <br/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a quick question regarding RegEx... I have a string that looks something
i have a string that contains HTML structure : <p> ... </p> <p> ....
I have a string (from a CDATA element) that contains description of XML. I
I have string that displays UTF-8 encoded characters, and I want to convert it
i have string stored in python variables, and i am outputting a html that
I have an XML string that contains an apostrophe. I replace the apostrophe with
i have the following view that contains an @Html.DropDownList :- <fieldset> <legend>Answer here</legend> <div
I have an HTML Table that looks like this: The user can update any
I have a description column in my SQLite database that contains some text to
I currently have SP list that contains the following columns. Item, Category & Description

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.