# Parsing HTML code with regex

**URL:** <https://www.ruby-forum.com/t/parsing-html-code-with-regex/73467>\
**Category:** Ruby\
**Created:** [October 24, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/parsing-html-code-with-regex/73467 "2006-10-24T20:34:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [October 24, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/parsing-html-code-with-regex/73467/1 "2006-10-24T20:34:52Z")

</div>

I’m trying to parse through some html code and count the number of times  
a match happens. The file is a large table with a ton of

 and \<tr  
‘something’\>. There are no spaces in the file. I’m trying to count and  
print each and \<tr ‘something’\>.

I haven’t even gotten to counting my matches. I’m still working on  
matching with

 or \<tr ‘anything’\>

I’ve done:

op\_file = HTML\_CODE  
if op\_file =~ /(\<tr(.\*?)\>)+/

but it catches everything on from the first \<tr to the end of the line.  
Any ideas?

-Shinkaku

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [October 24, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/parsing-html-code-with-regex/73467/2 "2006-10-24T20:57:22Z")

</div>

Anthony W. wrote:

> op\_file = HTML\_CODE  
> if op\_file =~ /(\<tr(.\*?)\>)+/

You want if op\_file =~ /\<tr.\*?\>/

But see below.

> but it catches everything on from the first \<tr to the end of the line.

Also, try scanning for matches, like this:

#!/usr/bin/ruby -w

path=“path-to-HTML-page”

data = File.read(path)

array = data.scan(%r{\<tr.\*?\>})

puts array

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [October 24, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/parsing-html-code-with-regex/73467/3 "2006-10-24T21:07:27Z")

</div>

Anthony W. wrote:

> I’m trying to parse through some html code and count the number of times  
> a match happens.

If the code is not yet XHTML, use Tidy to upgrade it.

Then parse it with XPath, looking for your match.

(Tip: All HTML that you control should be XHTML, of the highest quality.  
Don’t rely on sloppy HTML and “browser forgiveness”!)

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [October 25, 2006, 2:49pm UTC](https://www.ruby-forum.com/t/parsing-html-code-with-regex/73467/4 "2006-10-25T14:49:23Z")

</div>

Hi –

On Wed, 25 Oct 2006, Michael P. wrote:

> > op\_file = HTML\_CODE  
> > if op\_file =~ /(\<tr(.\*?)\>)+/
> 
> You are parsing always one line only.  
> Perhaps you mean a Regular Expression like
> 
> /(\<tr([^\>]\*?\>)+/m

The /m doesn’t make any difference there, because you’re not using the  
wildcard dot. /m just adds \n to the dot class.

David

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [October 25, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/parsing-html-code-with-regex/73467/5 "2006-10-25T17:14:42Z")

</div>

> Also, try scanning for matches, like this:
> 
> #!/usr/bin/ruby -w
> 
> path=“path-to-HTML-page”
> 
> data = File.read(path)
> 
> array = data.scan(%r{\<tr.\*?\>})
> 
> puts array

Thanks, this worked.

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [October 25, 2006, 2:11pm UTC](https://www.ruby-forum.com/t/parsing-html-code-with-regex/73467/6 "2006-10-25T14:11:15Z")

</div>

Anthony W. wrote:

> op\_file = HTML\_CODE  
> if op\_file =~ /(\<tr(.\*?)\>)+/

You are parsing always one line only.  
Perhaps you mean a Regular Expression like

/(\<tr([^\>]\*?\>)+/m

Anyway I am not sure if the if… is the right  
construct. Don’t you want to get the return value  
of the match, which delivers you a MatchData  
object from which you can get the results as  
an array or so.

MP
