?
Solved

Remove Hyperlinks From String

Posted on 2009-04-05
19
Medium Priority
?
758 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 400 total points
ID: 24071475
Sorry, remove the </a> aswell
string strNew = strModified.Replace("</a>", "");

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

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

Accepted Solution

by:
käµfm³d   👽 earned 1600 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
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.…
Suggested Courses

752 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