# Extracting text from HTML

**URL:** <https://rubytalk.org/t/extracting-text-from-html/5938>\
**Category:** ruby-talk\
**Created:** [10 May 2003 13:05 UTC](https://rubytalk.org/t/extracting-text-from-html/5938 "2003-05-10T13:05:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robo](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Robo](https://rubytalk.org/u/Robo)\
**Post date:** [10 May 2003 13:05 UTC](https://rubytalk.org/t/extracting-text-from-html/5938/1 "2003-05-10T13:05:59Z")

</div>

Given a HTML file, I’m looking for a regex that can give me the text that  
matches the following:

ANYTHING

So for I’ve got the following:

file = File.new(‘file.txt’, ‘r’)  
s = file.read  
file.close

channelBlocks = s.scan(/\<table width=“132” border=“0” cellpadding="0"  
cellspacing=“0”\>(.\*?)\</table\>/)

channelBlocks.each do |s|  
puts s  
end

But it doesn’t work, is it because the match is span across multiple lines  
in the HTML? If so, what’s the best method to solve it?

Robo

---

<div class="post-metadata">

**Author:** ![Kent\_Dahl2](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@Kent\_Dahl2](https://rubytalk.org/u/Kent_Dahl2)\
**Post date:** [10 May 2003 13:26 UTC](https://rubytalk.org/t/extracting-text-from-html/5938/2 "2003-05-10T13:26:34Z")

</div>

Robo wrote:

> channelBlocks = s.scan(/\<table width="132" border="0" cellpadding="0"  
> cellspacing="0"\>(.\*?)\</table\>/)
> 
> channelBlocks.each do |s|  
> puts s  
> end
> 
> But it doesn’t work, is it because the match is span across multiple lines  
> in the HTML? If so, what’s the best method to solve it?

Yes, it is due to the multiple lines. Try

channelBlocks = Regexp.new(  
‘

(.\*)

’,  
Regexp::MULTILINE  
)

Watch out for greed and nested tables, though.

I gave up on trying to make the literal Regexp work, escaping things by  
hand is a real chore. 😛

HTH

> **···**
>
> –  
> ([Kent Dahl]/)\_ _~_**[[http://www.stud.ntnu.no/~kentda/](http://www.stud.ntnu.no/~kentda/) ]**_/~  
> ))\_student_/(( \__d L b_\_/ NTNU - graduate engineering - 5. year )  
> ( \__\_õ|õ_/**/ ) _)Industrial economics and technological management(  
> \__**/_ö_\_\_\_\_/ (\_**_engineering.discipline_=_Computer::Technology_**)

---

<div class="post-metadata">

**Author:** ![Kent\_Dahl2](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@Kent\_Dahl2](https://rubytalk.org/u/Kent_Dahl2)\
**Post date:** [10 May 2003 13:26 UTC](https://rubytalk.org/t/extracting-text-from-html/5938/3 "2003-05-10T13:26:46Z")

</div>

Kent Dahl wrote:

> channelBlocks = Regexp.new(  
> ‘
> 
> (.\*)
> 
> ’,  
> Regexp::MULTILINE  
> )

Arg, line wrapping was too aggressive there…

channelBlocks = Regexp.new(  
'\<table width=“132” border=“0” cellpadding=“0” ’ +  
‘cellspacing=“0”\>(.\*)’,  
Regexp::MULTILINE  
)

> **···**
>
> –  
> ([Kent Dahl]/)\_ _~_**[[http://www.stud.ntnu.no/~kentda/](http://www.stud.ntnu.no/~kentda/) ]**_/~  
> ))\_student_/(( \__d L b_\_/ NTNU - graduate engineering - 5. year )  
> ( \__\_õ|õ_/**/ ) _)Industrial economics and technological management(  
> \__**/_ö_\_\_\_\_/ (\_**_engineering.discipline_=_Computer::Technology_**)

---

<div class="post-metadata">

**Author:** ![Robo](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Robo](https://rubytalk.org/u/Robo)\
**Post date:** [10 May 2003 23:49 UTC](https://rubytalk.org/t/extracting-text-from-html/5938/4 "2003-05-10T23:49:09Z")

</div>

> Arg, line wrapping was too aggressive there…
> 
> channelBlocks = Regexp.new(  
> '\<table width=“132” border=“0” cellpadding=“0” ’ +  
> ‘cellspacing=“0”\>(.\*)’,  
> Regexp::MULTILINE  
> )

Thanks, just a slight problem, I want it to match from the start of the

 tag to the next 

 tag, but I think Ruby matched it with the last tag in the file.

In the file, there’re 3 of these tables with width 132, I want to extract  
those three tables. This is what I have so far:

file = File.new(‘your\_tv\_guide.txt’, ‘r’)  
s = file.read  
file.close

#puts s  
regex = Regexp.new(  
‘

(.\*)

’,  
Regexp::MULTILINE  
)

channelBlocks = s.scan(regex)

channelBlocks.each do |s|  
puts s  
end

---

<div class="post-metadata">

**Author:** ![David\_A\_Black2](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@David\_A\_Black2](https://rubytalk.org/u/David_A_Black2)\
**Post date:** [11 May 2003 00:06 UTC](https://rubytalk.org/t/extracting-text-from-html/5938/5 "2003-05-11T00:06:46Z")

</div>

Hi –

> > Arg, line wrapping was too aggressive there…
> > 
> > channelBlocks = Regexp.new(  
> > '\<table width=“132” border=“0” cellpadding=“0” ’ +  
> > ‘cellspacing=“0”\>(.\*)’,  
> > Regexp::MULTILINE  
> > )
> 
> Thanks, just a slight problem, I want it to match from the start of the
> 
> tag to the next 
> 
> tag, but I think Ruby matched it with the last tag in the file.

That’s why Kent told you to watch out for regex ‘greed’ 🙂

If you change .\* to .\*? you should be in business.

> channelBlocks.each do |s|  
> puts s  
> end

Much simpler way to print an array:

puts channelBlocks

or

puts channel\_blocks

(if you want to do your bit to preserve classic Ruby style 🙂

David

> **···**
>
> On Sun, 11 May 2003, Robo wrote:
> 
> –  
> David Alan Black  
> home: [dblack@superlink.net](mailto:dblack@superlink.net)  
> work: blackdav@shu.edu  
> Web: [http://pirate.shu.edu/~blackdav](http://pirate.shu.edu/~blackdav)

---

<div class="post-metadata">

**Author:** ![Kent\_Dahl2](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@Kent\_Dahl2](https://rubytalk.org/u/Kent_Dahl2)\
**Post date:** [11 May 2003 07:11 UTC](https://rubytalk.org/t/extracting-text-from-html/5938/6 "2003-05-11T07:11:35Z")

</div>

[dblack@superlink.net](mailto:dblack@superlink.net) wrote:

> If you change .\* to .\*? you should be in business.

Oh dear, looks like I removed that part from the original code posted by  
Robo when testing. My regexp-nuby status is showing. Guess a good RE  
book should be on my wishlist for my birthday then. 🙂

> **···**
>
> –  
> ([Kent Dahl]/)\_ _~_**[[http://www.stud.ntnu.no/~kentda/](http://www.stud.ntnu.no/~kentda/) ]**_/~  
> ))\_student_/(( \__d L b_\_/ NTNU - graduate engineering - 5. year )  
> ( \__\_õ|õ_/**/ ) _)Industrial economics and technological management(  
> \__**/_ö_\_\_\_\_/ (\_**_engineering.discipline_=_Computer::Technology_**)

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [11 May 2003 08:29 UTC](https://rubytalk.org/t/extracting-text-from-html/5938/7 "2003-05-11T08:29:02Z")

</div>

Beware that regexp syntax and features vary from language to language, so  
don’t expect the more esoteric examples to work in Ruby without some  
modification.

You might want to try reading the documentation for “PCRE”, the  
Perl-Compatible Regular Expressions library by Philip Hazel, author of  
Exim. He writes excellent documents! Find it on [freshmeat.net](http://freshmeat.net).

Regards,

Brian.

> **···**
>
> On Sun, May 11, 2003 at 04:11:35PM +0900, Kent Dahl wrote:
> 
> > Oh dear, looks like I removed that part from the original code posted by  
> > Robo when testing. My regexp-nuby status is showing. Guess a good RE  
> > book should be on my wishlist for my birthday then. 🙂

---

<div class="post-metadata">

**Author:** ![David\_A\_Black2](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@David\_A\_Black2](https://rubytalk.org/u/David_A_Black2)\
**Post date:** [11 May 2003 13:12 UTC](https://rubytalk.org/t/extracting-text-from-html/5938/8 "2003-05-11T13:12:40Z")

</div>

Hi –

> **···**
>
> On Sun, 11 May 2003, Brian Candler wrote:
> 
> > On Sun, May 11, 2003 at 04:11:35PM +0900, Kent Dahl wrote:
> > 
> > > Oh dear, looks like I removed that part from the original code posted by  
> > > Robo when testing. My regexp-nuby status is showing. Guess a good RE  
> > > book should be on my wishlist for my birthday then. 🙂
> > 
> > Beware that regexp syntax and features vary from language to language, so  
> > don’t expect the more esoteric examples to work in Ruby without some  
> > modification.
> 
> I’d recommend “Mastering Regular Expressions” by Jeff Friedl for the  
> wishlist; it takes those variations into account, and the 2nd edition  
> includes Ruby.
> 
> David
> 
> –  
> David Alan Black  
> home: [dblack@superlink.net](mailto:dblack@superlink.net)  
> work: blackdav@shu.edu  
> Web: [http://pirate.shu.edu/~blackdav](http://pirate.shu.edu/~blackdav)
