# Gathering Links

**URL:** <https://www.ruby-forum.com/t/gathering-links/52895>\
**Category:** Ruby\
**Created:** [January 20, 2006, 12:46am UTC](https://www.ruby-forum.com/t/gathering-links/52895 "2006-01-20T00:46:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joeySS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b19c9b/32.png) [@joeySS](https://www.ruby-forum.com/u/joeySS)\
**Post date:** [January 20, 2006, 12:46am UTC](https://www.ruby-forum.com/t/gathering-links/52895/1 "2006-01-20T00:46:11Z")

</div>

Hello,  
I am looking for some help on a regex expression. I would like a regexp  
that matches against Html Links. I have tried, but I can’t seem to get  
anything working. I would appreciate help.

Thanks  
Joey

---

<div class="post-metadata">

**Author:** ![joeySS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b19c9b/32.png) [@joeySS](https://www.ruby-forum.com/u/joeySS)\
**Post date:** [January 20, 2006, 1:32am UTC](https://www.ruby-forum.com/t/gathering-links/52895/2 "2006-01-20T01:32:56Z")

</div>

joey\_\_ wrote:

> Hello,  
> I am looking for some help on a regex expression. I would like a regexp  
> that matches against Html Links. I have tried, but I can’t seem to get  
> anything working. I would appreciate help.

You might just want to run the HTML through htmltidy  
to generate an XML document and parse that or then use  
the htree library for the same purpose, it would probably  
be the more robust solution.

On the other hand, if you want to use regexps,  
something like this would work (though not tested).

First you have to match the beginning tag  
(there might be some whitespace:

/\<\s\*a

Next, gather any attributes in the opening tag:

([^\>]\*)\>

The link text comes next:

(.\*?)

The text section is ended by the closing anchor  
tag (no other tags are appropriate):

\<\s\*/\s_a[^\>]_\>

Finally, we want to match case-insensitively  
(A vs. a) and over multiple lines:

/im

So, $1 will be the attributes and $2 the link text.

> Thanks  
> Joey

E
