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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:03:44+00:00 2026-05-22T16:03:44+00:00

The explanation is in the comment. I put it there because is interpreted as

  • 0

The explanation is in the comment. I put it there because is interpreted as bold or something, and it screws up the post.

# I need to capture text that is
# enclosed in tags that are both <b> and
# <i>, but if there is more than one
# text enclosed in <i> in the same <b>
# block, then I only want the text
# enclosed in the first <i> tag, For
# example, for the following line:
# 
# <b> <i> Important text here </i>
# irrelevant text everywhere else <i>
# irrelevant text here </i> </b>  <b>
# <i> Also Important </i> not important
# <i> not important </i> </b> 
# 
# I want to retrieve only: 
# - Important text here 
# - Also Important
# 
# I also must not retrieve text inside an
# <h2> block. I have been trying to
# delete the block with nodes.delete(nodes. search('h2')), 
# but it doesn't actually delete the h2 block 


require "rubygems"
require "nokogiri"

html = <<EOT
  <b><i> Important text here </i> more text <i> not important text here </i> </b>
  <b> <i> Also Important </i> more text <i> not important </i> </b> 

  <h2><b> <i> I don't want this text either</i></b></h2> 
EOT


doc = Nokogiri::HTML(html)

nodes = doc.search('b i')

nodes.each { |e| puts e }

# Expected output:
# Important text here
# Also Important
  • 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-22T16:03:44+00:00Added an answer on May 22, 2026 at 4:03 pm
    require "nokogiri"
    require 'pp'
    html = <<EOT
      <b><i>Important text here</i>more text<i>not important text here</i></b>
      <b><i>Also Important</i>more text<i>not important</i></b> 
    
      <h2><b><i>I don't want this text either</i></b></h2> 
    EOT
    
    
    doc = Nokogiri::HTML(html)
    nodes = doc.search('b')
    nodes.each { |e| puts e.children.children.first unless e.parent.name == "h2" }
    

    or with xpath:

    nodes = doc.xpath("//../*[local-name() != 'h2']/b/i[1]")
    nodes.each { |e| puts e.children.first}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Explanation: Say package A is always built first and then package B. I need
Explanation: i have few objects and im declaring them inside $(document).ready(). WHY? because in
Explanation I have a multidimensional array that is iterated over to created a categorized
I have a project for a client that will be using a YouTube comment
We have a commenting system built into our engine that allows programmers to put
The most recent comment on PHP's in_array() help page ( http://uk.php.net/manual/en/function.in-array.php#106319 ) states that
I've read somewhere that using a base controller is bad and that there are
When I expand comment body that is floated to right my container does not
Is there a simple explanation for why the latex / pdflatex compiler is funky
For explanation imagine a simple address. Written in a HTML paragraph with line breaks

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.