Forum: Ruby on Rails Regex in HTML

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A77873df3a9766b208e009248a2a9a56?d=identicon&s=25 Hampton (Guest)
on 2006-04-24 03:09
(Received via mailing list)
So, I'm trying to write a nice bit of regex to handle finding anchor
tags in
a bit of html.

This is what I've got....

/<[aA][^>]*>[^<]*<\/[aA]>/

I'm planning on using this with a gsub!.

Here is what it has to do....

<html><a href="http://stuff.com" class="link">Anything in
here.</a></html>

As you can see, the regex I have will indeed work for this instance. It
will
match with '<a href="http://stuff.com" class="link">Anything in
here.</a>'

Except, here is where it fails....

<html><a href="http://stuff.com" class="link"><strong>Anything</strong>
in
here.</a></html>

I still need '<a href="http://stuff.com"
class="link"><strong>Anything</strong> in here.</a>'

This regex doesn't work...
/<[aA][^>]*>.*<\/[aA]>/
Because my middle tag will just keep going and return

<a href="http://stuff.com" class="link"><strong>Anything</strong> in
here.</a></html>

because it doesn't have any reason to stop.

Any suggestions?

-hampton.
D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2006-04-24 03:30
(Received via mailing list)
replace .* with [^(<\/a)(<\/A)]

That should work.

-N
Cbff7ea94c9b2c4d5f7cfefd25be9ae2?d=identicon&s=25 Just Someone (Guest)
on 2006-04-24 03:36
(Received via mailing list)
You could use the ? non-greedy modifier for this:

/<a.*?<\/a>/

This will match from the beginning of the <a to the first </a>.


On 4/23/06, Hampton <hcatlin@gmail.com> wrote:
>
> I still need '<a href="http://stuff.com"
>
>
>


--
Family management on rails: http://www.famundo.com - coming soon!
My development related blog: http://devblog.famundo.com
A77873df3a9766b208e009248a2a9a56?d=identicon&s=25 Hampton (Guest)
on 2006-04-24 04:03
(Received via mailing list)
WOooooowhoooo!!!!

Thanks!
This topic is locked and can not be replied to.