Solved

Remove Hyperlinks From String

Posted on 2009-04-05
19
755 Views
Last Modified: 2012-05-06
Hello I need to remove hyperlinks from a string but leave the text of the hyperlink. The hyperlinks look like this. I am using vb.net 1.1. The only thing left in this example should be "Tollgate". The url employeeid, period, assignmentid, planid, allocatorid and elementid are variables not fixed values. Thank you much for any help as I am pulling what hair I have left out on it.

<a id="lnkVolumeDetails" href="/program.Statements/Y2005/Ultrasound/Rep/RevenueDetail.aspx?employeeid=6705&amp;period=200902&amp;assignmentid=12110&amp;planid=296&amp;elementid=2359&amp;allocatorid=3334" style="font-size:10pt;">= Current QTD Volume</a></TD>
0
Comment
Question by:scorpion53061
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
  • 4
  • +1
19 Comments
 
LVL 7

Expert Comment

by:garethh86
ID: 24071456
Sorry, I mainly deal with vb.net so my C# is a little ropey, you should get the gist of this though, hope it helps!
// Count the number of chars to ">"
 
    // Count characters. White space is treated as a character
    System.Text.RegularExpressions.MatchCollection charColl = System.Text.RegularExpressions.Regex.Matches(strOriginal, @">");
    strCount = charColl.Count.ToString();
 
// Removes specified number of characters(from the count before) starting at index 0
    strModified = strOriginal.Remove(0,strCount);
    MessageBox.Show(strModified);

Open in new window

0
 
LVL 7

Assisted Solution

by:garethh86
garethh86 earned 100 total points
ID: 24071475
Sorry, remove the </a> aswell
string strNew = strModified.Replace("</a>", "");

Open in new window

0
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

Expert Comment

by:Scanbox
ID: 24071476
sorry didnt read your text carefully enough
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 24071609
In one line:
System.Text.RegularExpressions.Regex.Replace(url_string_object_here, "</?a[^>]*>", String.Empty)

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24071614
Ammendment:

Capture the output of the previous post in a variable as the Replace() function returns the new string.
0
 

Author Comment

by:scorpion53061
ID: 24071633
kaufmed are you saying I can put the whole string in there and it will remove the hyperlinks?
0
 

Author Comment

by:scorpion53061
ID: 24071636
I mean the whole HTML string (I mean there is going to be  more than one link that needs to be removed)
0
 
LVL 7

Expert Comment

by:garethh86
ID: 24071638
Much cleaner kaufmed, was trying to convert my vb code badly!
0
 
LVL 7

Expert Comment

by:garethh86
ID: 24071646
scorpion, where are the links coming from, is it from a databound item?
0
 

Author Comment

by:scorpion53061
ID: 24071647
I am in vb.net so that is cool.
0
 

Author Comment

by:scorpion53061
ID: 24071656
I put the HTML string in there and it didn't work. Not sure what I did wrong.
0
 

Author Comment

by:scorpion53061
ID: 24071667
no it is a string I am reading from a webpage.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24071999
I tested the RegEx and it removed all occurrences from my example. Perhaps if you post some code, we can better assist you.

My test code follows:
Application:
 
    Sub Main()
        Dim link As String = "<a id=""lnkVolumeDetails"" href=""/program.Statements/Y2005/Ultrasound/Rep/RevenueDetail.aspx?employeeid=6705&amp;period=200902&amp;assignmentid=12110&amp;planid=296&amp;elementid=2359&amp;allocatorid=3334"" style=""font-size:10pt;"">= Current QTD Volume</a></TD><a id=""lnkVolumeDetails"" href=""/program.Statements/Y2005/Ultrasound/Rep/RevenueDetail.aspx?employeeid=6705&amp;period=200902&amp;assignmentid=12110&amp;planid=296&amp;elementid=2359&amp;allocatorid=3334"" style=""font-size:10pt;"">= Current QTD Volume</a></TD><a id=""lnkVolumeDetails"" href=""/program.Statements/Y2005/Ultrasound/Rep/RevenueDetail.aspx?employeeid=6705&amp;period=200902&amp;assignmentid=12110&amp;planid=296&amp;elementid=2359&amp;allocatorid=3334"" style=""font-size:10pt;"">= Current QTD Volume</a></TD><a id=""lnkVolumeDetails"" href=""/program.Statements/Y2005/Ultrasound/Rep/RevenueDetail.aspx?employeeid=6705&amp;period=200902&amp;assignmentid=12110&amp;planid=296&amp;elementid=2359&amp;allocatorid=3334"" style=""font-size:10pt;"">= Current QTD Volume</a></TD><a id=""lnkVolumeDetails"" href=""/program.Statements/Y2005/Ultrasound/Rep/RevenueDetail.aspx?employeeid=6705&amp;period=200902&amp;assignmentid=12110&amp;planid=296&amp;elementid=2359&amp;allocatorid=3334"" style=""font-size:10pt;"">= Current QTD Volume</a></TD>"
        link = System.Text.RegularExpressions.Regex.Replace(link, "</?a[^>]*>", String.Empty)
 
        Console.WriteLine(link)
        Console.ReadKey()
    End Sub
 
Output:
 
= Current QTD Volume</TD>= Current QTD Volume</TD>= Current QTD Volume</TD>= Cur
rent QTD Volume</TD>= Current QTD Volume</TD>

Open in new window

0
 

Author Comment

by:scorpion53061
ID: 24072212
Okay this is the code that gives me the string from the web page? WIll that help?
Public Function EmailHttpContent(ByVal url As String) As String
        Dim pr As New System.Net.WebProxy("0.0.0.0", 88)
        Dim objRequest As WebRequest = System.Net.HttpWebRequest.Create(url)
        objRequest.Proxy = pr
        Dim sr As New StreamReader(objRequest.GetResponse().GetResponseStream())
        Dim result As String = sr.ReadToEnd()
        sr.Close()
        Return result
    End Function
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24074826
So something like the following doesn't work for you?
Public Function EmailHttpContent(ByVal url As String) As String
        Dim pr As New System.Net.WebProxy("0.0.0.0", 88)
        Dim objRequest As WebRequest = System.Net.HttpWebRequest.Create(url)
        objRequest.Proxy = pr
        Dim sr As New StreamReader(objRequest.GetResponse().GetResponseStream())
        Dim result As String = sr.ReadToEnd()
        sr.Close()
 
        Return System.Text.RegularExpressions.Regex.Replace(result, "</?a[^>]*>", String.Empty)
End Function

Open in new window

0
 

Author Comment

by:scorpion53061
ID: 24099959
I will have to bring in the function locally as it used elsewhere in the program without the need for what we are doing. But I will let you know.
Again thank you for your help.
0
 

Author Comment

by:scorpion53061
ID: 24099998
Holy crap that did work....youguys rock
0
 

Author Closing Comment

by:scorpion53061
ID: 31566751
outstanding people all around. patient and helpful.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question