Solved

Remove Hyperlinks From String

Posted on 2009-04-05
19
752 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
 

Expert Comment

by:Scanbox
ID: 24071460
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.

749 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