Solved

VB.Net - Remove Text Between Characters

Posted on 2015-02-09
9
203 Views
Last Modified: 2015-02-11
Good Day Experts!

I have quite the little project here today.  I need to remove text between 2 characters and also remove the 2 characters.  This happens 2 times in a line.  It "feels" like it is fairly attainable but I cannot seem to find a solution.  Here is a sample of what I am looking at:

GBP       British Pound       0.6528 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=bdcf3f2cf0&e=a2e1cf5770>        1.5318 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=47712730df&e=a2e1cf5770>       

This is all 1 line in Textpad. I need to remove the text between both sets of <> as well as the <>. So I need to be left with:

GBP       British Pound       0.6528 1.5318

Is this a pretty straight forward endeavor? Can you help me get started with a few tips?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
9 Comments
 
LVL 13

Accepted Solution

by:
LIONKING earned 200 total points
ID: 40598572
Quick and dirty way... You can find the substring by finding the < and > symbols, then do that two times (one for each link).

I'm sure there's a neater and more elegant way, I'm just typing this out of the top of my head.
0
 
LVL 32

Assisted Solution

by:it_saige
it_saige earned 100 total points
ID: 40598631
An implementation of a quick and dirty method:
Module Module1
	Sub Main()
		Dim gbp = "GBP       British Pound       0.6528 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=bdcf3f2cf0&e=a2e1cf5770>        1.5318 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=47712730df&e=a2e1cf5770>"
		Dim index As Integer = gbp.IndexOf("<")
		While (index <> -1)
			If gbp.IndexOf(">", index) <> -1 Then
				gbp = String.Format("{0}{1}", gbp.Substring(0, index), gbp.Substring(gbp.IndexOf(">", index) + 1))
			End If
			index = gbp.IndexOf("<", index + 1)
		End While
		Console.WriteLine(gbp)
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPG-saige-
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 40598641
Hi jimbo99999;

This should do what you need.
Imports System.Text.RegularExpressions


'' Test Data
Dim data = "GBP       British Pound       0.6528 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=bdcf3f2cf0&e=a2e1cf5770>        1.5318 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=47712730df&e=a2e1cf5770>      "
'' Remove URL's from the string and trim any white spaces from the start and end
data = Regex.Replace(data, "<http[^>]*>\s*", String.Empty).Trim()

Open in new window


Resulting string.
GBP       British Pound       0.6528 1.5318
0
 
LVL 32

Expert Comment

by:it_saige
ID: 40598656
A change to the substring code presented above:
Module Module1
	Sub Main()
		Dim gbp = "GBP       British Pound       0.6528 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=bdcf3f2cf0&e=a2e1cf5770>        1.5318 <http://exchangerates.us2.list-manage2.com/track/click?u=216e81aed0649ca9d1a45cc41&id=47712730df&e=a2e1cf5770>"
		Dim index As Integer = gbp.IndexOf("<")
		While (index <> -1)
			If gbp.IndexOf(">", index) <> -1 Then
				gbp = String.Format("{0}{1}", gbp.Substring(0, index), gbp.Substring(gbp.IndexOf(">", index) + 1))
			End If
			' Have to ensure that the index does not surpass the length
			index = gbp.IndexOf("<", If(index + 1 > gbp.Length, 0, index + 1)) 
		End While
		Console.WriteLine(gbp)
		Console.ReadLine()
	End Sub
End Module

Open in new window


Otherwise the output is the same.

-saige-
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 32

Expert Comment

by:it_saige
ID: 40598659
@Fernando, nice regex.  ;)

-saige-
0
 
LVL 22

Assisted Solution

by:rspahitz
rspahitz earned 100 total points
ID: 40598660
Regex could probably handle this well, but I'm not very versed in that.  I'd simply do this:

(assuming that text is stored in a variable called conversion:
Dim iLeftBracketPosn As Integer
Dim iRightBracketPosn As Integer 
Do
   iLeftBracketPosn = conversion.IndexOf("<")
   iRightBracketPosn = conversion.IndexOf(">")
   If iLeftBracketPosn < 0 Or iRightBracketPosn < 0 Then
      Exit Do
   End If
   conversion = conversion.Substring(0, iLeftBracketPosn - 1) & conversion.Substring(iRightBracketPosn + 1)
Loop

Open in new window


(Do a quick check on that to ensure that the left and right brackets are removed and the adjacent text is intact; otherwise you may need to +/- 1 to fix it.)
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40598743
If you are attempting to do this wholly in TextPad, then Fernando's regex should work there to. Just be sure to check the "Regular expression" box and leave the Replace box blank in Find/Replace.
0
 

Author Comment

by:Jimbo99999
ID: 40599098
Thanks everyone for the responses.  I will be working on this shortly.
0
 

Author Closing Comment

by:Jimbo99999
ID: 40604185
Thanks everyone for the help.  Lionking spurred my thought process and went went down the path of that suggestion.  I liked the other ways for my toolbox.

Thanks again,
jimbo99999
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now