# How do I extract the ID name of a Div and its content?

**URL:** <https://www.ruby-forum.com/t/how-do-i-extract-the-id-name-of-a-div-and-its-content/155490>\
**Category:** Ruby\
**Created:** [December 12, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/how-do-i-extract-the-id-name-of-a-div-and-its-content/155490 "2008-12-12T12:05:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pannda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pannda](https://www.ruby-forum.com/u/pannda)\
**Post date:** [December 12, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/how-do-i-extract-the-id-name-of-a-div-and-its-content/155490/1 "2008-12-12T12:05:01Z")

</div>

Hi folks,  
sorry to bother you with such a mundane question, but I tried for 3hours  
and don`t know how ☹  
I need to extract the strings, that means everything between “” or ‘’

that is the Input:

Ruby Forum is cool

- \<%= link\_to 'Cities', cities\_path %\>
- \<%= link\_to 'Restaurants', restaurants\_path %\>
- \<%= link\_to 'Categories', categories\_path %\>
- \<%= link\_to 'Products', products\_path %\>

wished output:

pagewrapper  
header  
navbar  
Cities  
Restaurants  
Categories  
Products

Please help  
Thanks in advance  
pannda

---

<div class="post-metadata">

**Author:** ![pannda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pannda](https://www.ruby-forum.com/u/pannda)\
**Post date:** [December 12, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/how-do-i-extract-the-id-name-of-a-div-and-its-content/155490/2 "2008-12-12T13:02:05Z")

</div>

Hans Wurst wrote:

> but I tried for 3hours and don`t know how ☹

So, what approach or approaches did you try? What was the solution that  
got closest to what you were trying, and what did it output?

There are a whole host of ways you might be approaching this. For  
example, you might be using the Hpricot (HTML parsing) library, or REXML  
(XML parsing), or simple regular expression matching.

If all you want is the bits between ‘single quotes’ then a regular  
expression match is probably easiest. Try using String#scan, and give it  
a regular expression which matches a single quote followed by any number  
of non-single-quote characters followed by a single quote.

Or you could use use String#split("’") and keep only the odd-numbered  
elements of the returned array.

I’m sure if you post your actual code and what it does, someone will  
help you tweak it to work.

---

<div class="post-metadata">

**Author:** ![pannda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pannda](https://www.ruby-forum.com/u/pannda)\
**Post date:** [December 12, 2008, 1:05pm UTC](https://www.ruby-forum.com/t/how-do-i-extract-the-id-name-of-a-div-and-its-content/155490/3 "2008-12-12T13:05:44Z")

</div>

Disclaimer: If there are other ’ somewhere in the document (comments,  
CDATA sections, Text elements)  
this will miserably break and you need HPricot or other HTML parsers.  
If however your data is simple enough and you can fulfill the  
prerequisite it becomes very easy…

robert@siena:~/log/ruby/ML 12:56:41  
505/6 \> cat strings.rb && ruby strings.rb  
#!/usr/bin/ruby

# vim: sw=2 ts=2 ft=ruby expandtab tw=0 nu syn=on:

# file: strings.rb

text = DATA.read

p text.scan /’(.\*?)’/  
**END**

Ruby Forum is cool

- \<%= link\_to 'Cities', cities\_path %\>
- \<%= link\_to 'Restaurants', restaurants\_path %\>
- \<%= link\_to 'Categories', categories\_path %\>
- \<%= link\_to 'Products', products\_path %\>

[["pagewrapper"], ["header"], ["navbar"], ["Cities"], ["Restaurants"], ["Categories"], ["Products"]]

---

<div class="post-metadata">

**Author:** ![pannda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pannda](https://www.ruby-forum.com/u/pannda)\
**Post date:** [December 12, 2008, 1:13pm UTC](https://www.ruby-forum.com/t/how-do-i-extract-the-id-name-of-a-div-and-its-content/155490/4 "2008-12-12T13:13:28Z")

</div>

Thaaaaaaaank You Robert!  
Thumbs up!, this is exactly what I wanted, clean & simple without  
hpricot, rexml or overly complicated regexs

Have a nice weekend  
greetz Pannda

---

<div class="post-metadata">

**Author:** ![pannda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pannda](https://www.ruby-forum.com/u/pannda)\
**Post date:** [December 12, 2008, 1:43pm UTC](https://www.ruby-forum.com/t/how-do-i-extract-the-id-name-of-a-div-and-its-content/155490/5 "2008-12-12T13:43:04Z")

</div>

Hi Brian

> I’m sure if you post your actual code and what it does, someone will  
> help you tweak it to work.

actual code was this

require ‘rubygems’  
require ‘yaml’  
require ‘hpricot’

html\_datei = File.open(ARGV[0]).readlines.collect do |line|

option 1# line.gsub(/\</?[^\>]_\>/, “”).to\_yaml  
option 2 # line.gsub(/\</,  
“”).gsub(/\>/,"").strip.to\_yaml.gsub(/—/,"").lstrip  
option 3 # line.gsub(/^\</?[^\>]_\>/,"").lstrip  
end

# all 3 options worked, but I couldn’t figure out how to get those “” or

‘’ in between

# so I tried hpricot

doc = open(ARGV[0]) { |f| Hpricot(f).search(“div”) }

# but how to go from here? I couldn’t figure out the documentation of

Hpricot, because .to\_inner\_html doesn’t work

yaml\_datei = File.new(ARGV[1], ‘w+’)

yaml\_datei \<\< html\_datei  
yaml\_datei \<\< doc  
yaml\_datei.close

so, that was my actual code
