views:

464

answers:

1

I need to filter out anchor tags in a string. For instance,

Check out this site: <a href="http://www.stackoverflow.com"&gt;stackoverflow&lt;/a&gt;

I need to be able to filter out the anchor tag to this:

Check out this site: http://www.stackoverflow.com

That format may not be constant, either. There could be other attributes to the anchor tag. Also, there could be more than 1 anchor tag in the string. I'm doing the filtering in vb.net before it goes to the database.

+7  A: 

Here's a simple regular expression that should work.

Imports System.Text.RegularExpressions

' ....

Dim reg As New Regex("<a.*?href=(?:'|"")(.+?)(?:'|"").*?>.+?</a>")
Dim input As String = "This is a link: <a href='http://www.stackoverflow.com'&gt;Stackoverflow&lt;/a&gt;"
input = reg.Replace(input, "$1", RegexOptions.IgnoreCase)
Rich Reuter