# Regexp help: Matching HTML having trouble w/greediness

**URL:** <https://www.ruby-forum.com/t/regexp-help-matching-html-having-trouble-w-greediness/61398>\
**Category:** Ruby\
**Created:** [May 23, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/regexp-help-matching-html-having-trouble-w-greediness/61398 "2006-05-23T20:05:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 23, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/regexp-help-matching-html-having-trouble-w-greediness/61398/1 "2006-05-23T20:05:32Z")

</div>

All,

I am attempting to do some matching on some HTML.

Here’s what I want:

I want to be able to match any tag which is of the form \<area  
…\> that does NOT contain a “mailto:” href. The grouping is for some  
substitution that I’m doing.

Here’s my pattern:

/(\<area ._?href=[’|"])(?!mailto:)(._?)([’|"].\*?\>)/mi

What I find is that this pattern will successfully handle most area  
tags, however, when an area tag is followed by a tag (which also has  
a href attribute, it will match everything between \<area and the end of  
the tag). So, for example,

\<area href="mailto: removed\_email\_address@domain.invalid\>other stuff, including tags

this pattern matches all the way through the end of the tag.

I tried to stick a negative lookahead (?!\<) at the end of the pattern  
but that doesn’t seem to help.

How do I get this pattern to STOP matching?

Thanks for any help,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 23, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/regexp-help-matching-html-having-trouble-w-greediness/61398/2 "2006-05-23T21:49:19Z")

</div>

Wes G. wrote:

> All,
> 
> I am attempting to do some matching on some HTML.
> 
> Here’s what I want:
> 
> I want to be able to match any tag which is of the form \<area  
> …\> that does NOT contain a “mailto:” href. The grouping is for some  
> substitution that I’m doing.
> 
> Here’s my pattern:
> 
> /(\<area ._?href=[’|"])(?!mailto:)(._?)([’|"].\*?\>)/mi
> 
> What I find is that this pattern will successfully handle most area  
> tags, however, when an area tag is followed by a tag (which also has  
> a href attribute, it will match everything between \<area and the end of  
> the tag). So, for example,
> 
> \<area href="mailto: removed\_email\_address@domain.invalid\>other stuff, including tags
> 
> this pattern matches all the way through the end of the tag.
> 
> I tried to stick a negative lookahead (?!\<) at the end of the pattern  
> but that doesn’t seem to help.
> 
> How do I get this pattern to STOP matching?
> 
> Thanks for any help,  
> Wes

This appears to work better:

/(\<area [^\>]_?)(href=[’|"])(?!mailto:)(._?)([’|"].\*?\>)/mi

The “any character but ‘\>’” seems to stop the evaluation of the match  
from making it any further than the end of the tag.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 23, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/regexp-help-matching-html-having-trouble-w-greediness/61398/3 "2006-05-23T21:55:25Z")

</div>

On 5/23/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here’s my pattern:
> 
> /(\<area ._?href=['|"])(?!mailto:)(._?)(['|"].\*?\>)/mi

Let’s try  
\<area [^\>]\*?etc.  
You will find out that this will solve this particular problem but  
creates  
other ones like e.g. with this HTML  
  
unless, and you tell me, that is not legal HTML  
anyway I feel that maybe Regexen are not the right tool for your task  
anymore, dunno.  
But maybe you can work with the above regex anyway.  
Cheers  
Robert

What I find is that this pattern will successfully handle most area

> but that doesn’t seem to help.
> 
> How do I get this pattern to STOP matching?
> 
> Thanks for any help,  
> Wes
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 23, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/regexp-help-matching-html-having-trouble-w-greediness/61398/4 "2006-05-23T22:06:32Z")

</div>

Robert D. wrote:

> On 5/23/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > substitution that I’m doing.  
> > > \<area href="mailto: removed\_email\_address@domain.invalid\>other stuff, including tags\<a  
> > > Wes
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> So I was just typing and typing and typing…  
> Glad Ur happy with it.  
> Robert
> 
> –  
> Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
> concerne l’univers, je n’en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

Robert,

I am pretty sure that it’s illegal to put a “\>” in an HTML attribute  
(you would achieve the desired affect with \>).

I agree that perhaps regexen are not the best way to manipulate this -  
however, I’ve already been down the path of using a HTML parser  
(RubyfulSoup/htmltools) and trying to output the resulting parse tree.  
Unfortunately, that parser attempts to “fix” the HTML for me and when I  
attempt to render the fixed HTML in a browser, it is “too fixed” for the  
browser to handle.

So unfortunately, I’m forced to accept what I get and only change it so  
that I don’t break any existing stuff.

Hence, I’m using regexen to do my write - manipulation.

Thanks for the help though!

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 23, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/regexp-help-matching-html-having-trouble-w-greediness/61398/5 "2006-05-23T21:55:32Z")

</div>

On 5/23/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > substitution that I’m doing.  
> > \<area href="mailto: removed\_email\_address@domain.invalid\>other stuff, including tags\<a  
> > Wes
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

So I was just typing and typing and typing…  
Glad Ur happy with it.  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [May 23, 2006, 10:21pm UTC](https://www.ruby-forum.com/t/regexp-help-matching-html-having-trouble-w-greediness/61398/6 "2006-05-23T22:21:03Z")

</div>

On 5/23/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > >
> 
> Robert,
> 
> I am pretty sure that it’s illegal to put a “\>” in an HTML attribute

Well than you might be flying with your solution.

(you would achieve the desired affect with \>).

> I agree that perhaps regexen are not the best way to manipulate this -  
> however, I’ve already been down the path of using a HTML parser  
> (RubyfulSoup/htmltools) and trying to output the resulting parse tree.  
> Unfortunately, that parser attempts to “fix” the HTML for me and when I  
> attempt to render the fixed HTML in a browser, it is “too fixed” for the  
> browser to handle.

Not surprised ☹

So unfortunately, I’m forced to accept what I get and only change it so

> that I don’t break any existing stuff.
> 
> Hence, I’m using regexen to do my write - manipulation.

I was not thinking about throwing them away altogether 😉 but maybe  
scanning  
from one attribute to another inside a tag.  
But thinling about me personal expirence sometimes I got threw with  
grep!

Thanks for the help though!

Well you found out for yourself 😉

Wes

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Cheers  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein
