How to exclude selected text and extract the next text separated with " : "? - vb.net

i'm in the making of machine readable dictionary for my native language which is Malay. i need to extract the Malay translation from the .txt file. In the .txt file, the example are like this:
aberration : aberasi
aberration function : rangkap aberasi
ablation : ablasi
ablative material : bahan ablasi
the left one are the terms in English and after the separator : is Malay.
What I would like to ask is how do i do when the word search is "aberration function" and i need to display only "rangkap aberasi"?
i tried to used
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim text As String = "dictionary.txt"
Dim word As String = "\b" & TextBox2.Text & "\b\s+(\w+)"
For Each a As Match In Regex.Matches(text, word, RegexOptions.IgnoreCase)
MsgBox(a.Groups(1).Value)
Next
End Sub
End Class
See more at: http://www.visual-basic-tutorials.com/get-the-next-word-after-a-specific-word-in-visual-basic.htm#sthash.w8E1Qb6l.dpuf
however, my problem is, this code above only display the next word after seperation which is "rangkap". while i need the whole text after separation ":" which might be more than 2 words.
here is my current code
Using reader As New StreamReader("D:\Dictionary of Engineering A only.txt")
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
If line.Contains(wordsearch.Text) Then
Edef.Text = line
Exit While
End If
End While
End Using

One way to solve this would be like this:
Using reader As New StreamReader("D:\Dictionary of Engineering A only.txt")
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
If line.Contains(wordsearch.Text) Then
dim lineParts() as String = line.split(":")
Edef.Text = lineParts(1)
Exit While
End If
End While
End Using
You check if the line contains the text, if so, you split the line by the ":" and take the 2nd part of it.

Related

VB: Pull text from specific HTML tag into Textbox

I am trying to write a VB.Net application that needs to pull the text from a specific tag:
<span data-reactid="85">172,890,000</span>
and then enter the text found 172,890,000 into a textbox on the form.
In Textbox1, you enter the Stock Symbol you want to search.
The data for "TTM - Total Revenue" will always be held within the:
<span data-reactid="85">172,890,000</span> tag. Regardless of stock you check.
In RichTextBox1, is the downloaded source code for the url.
TextBox2 is where it pulls "TTM". I will probably change it to a label as it's a constant value. I cant put the number in the variable as it will vary on the company, i.e. the value entered into TextBox1.
TextBox3 is going to show the value I really need. The 172,890,000 held in the
<span data-reactid="85">172,890,000</span> tag.
I was wondering how to search for the string within RichTextBox1, and pull the next 7 characters after the end of the string if that would work?
My code so far is:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Source As String
Dim ttm1 As String
Dim ttmrev As String
Source = New System.Net.WebClient().DownloadString("https://finance.yahoo.com/quote/" + TextBox1.Text + "/financials?p=" + TextBox1.Text)
ttm1 = <span data-reactid="65">ttm</span>
ttmrev = <span data-reactid="85"></span>
RichTextBox1.Text = Source
If RichTextBox1.Find(ttm1) Then
TextBox2.Text = "ttm".ToUpper
End If
End Sub
End Class
As usual in coding, there are always many ways to accomplish the same end result. One way is to use a Regex.
For example:
Imports System.Text.RegularExpressions
Sub Main
' In place of a static string here, place the exact line you want to search instead.
Dim str As String = "<span data-reactid=""85"">172,890,000</span>"
' Search for the match
showMatch(str, "(\d+),?(\d+),?(\d+)")
End Sub
Sub showMatch(ByVal text As String, ByVal expr As String)
' Make sure you're expression looks the way you want it
Debug.WriteLine("The Expression: " + expr)
' Declare the variable and do the regex match
Dim mc As MatchCollection = Regex.Matches(text, expr)
Dim m As Match
' Handle the result however you wish; for example instead of a loop, since
' there should only be 1 result you could just do
' something like: TextBox3.Text = m.ToString()
For Each m In mc
Debug.WriteLine(m)
Next m
End Sub
The above regex will find any number that matches for example:
172,890,000
890,000
000
2,80,000
etc.
Another choice might be to use a WebBrowser control, fetch the source into it, and play around with the innerHtml as needed.

How can I ignore a new line character when reading CSV file in VB.NET?

I wrote a utility in VB.NET that reads an input CSV file, does some processing (specifically it ignores the first 5 lines of the input file and replaces them with a header row saved in another file) and writes the information from the input file into a new output CSV file.
Where my program fails is when the input data includes new line characters within one column value within the CSV.
I would like to ignore the new line character within a CSV data row when I load it into my string array.
Here is my code (its embedded in a form)
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim incsvPath = strFileName
Dim outcsvPath = fi.DirectoryName & "\" & outfilename
Dim headerPath = fi.DirectoryName & "\ACTIVITY_HISTORY_HEADER.csv"
Dim fileP As String = incsvPath
Dim fileheader As String = headerPath
Dim CSVheaderIn As New ArrayList
Dim CSVlinesIn As New ArrayList
Dim CSVout As New List(Of String)
CSVheaderIn.AddRange(IO.File.ReadAllLines(fileheader))
CSVlinesIn.AddRange(IO.File.ReadAllLines(fileP))
messageTB.AppendText(vbCrLf & vbCrLf)
For Each line As String In CSVheaderIn
Dim nameANDnumber As String() = line.Split(",")
messageTB.AppendText("csv file header row = " & line & vbCrLf & vbCrLf & "csv file contents follow ..." & vbCrLf)
CSVout.Add(line)
Next
Dim mySubAL As ArrayList = CSVlinesIn.GetRange(5, CSVlinesIn.Count - 5)
For Each line As String In mySubAL 'CSVlinesIn
messageTB.AppendText(line & vbCrLf)
CSVout.Add(line)
Next
IO.File.WriteAllLines(outcsvPath, CSVout.ToArray)
End Sub
This is fairly hard work actually; it'll be easier to use a library that knows how to read and write CSV with newlines in the data than roll your own - not saying you couldn't, but it's a wheel that has already been invented so why do it again?
I used Steve Hansen's Csv - right click your project in solution explorer, choose Manage Nuget Packages, click Browse, Search csv, install the right one
Imports System.Text
Imports Csv
Imports System.IO
Module Module1
Sub Main(args As String())
'open the headers file
Using hIn = File.OpenText("C:\temp\h.csv")
'setup instruction to the csv reader with headersabsent flag so we can get the first line as data
Dim hOptions = New CsvOptions With {.HeaderMode = HeaderMode.HeaderAbsent}
'take the first line into an array - these are our headers
Dim headers = CsvReader.Read(hIn, hOptions)(0).Values
'open the data file,
Using fIn = File.OpenText("C:\temp\a.csv")
'setup instruction for the reader to skip 5 rows, treat first row as data, and allow newlines in quoted fields
Dim fOptions = New CsvOptions With {.RowsToSkip = 5, .HeaderMode = HeaderMode.HeaderAbsent, .AllowNewLineInEnclosedFieldValues = True}
Using fOut = File.CreateText("C:\temp\a_out.csv")
'convert the ICsvLine rows in the reader to rows of String() that the writer will accept, and write them under the headers
CsvWriter.Write(fOut, headers, CsvReader.Read(fIn, fOptions).Select(Function(line) line.Values))
End Using
End Using
End Using
End Sub
End Module
You don't have to use this lib to read the headers; you could just file.ReadText().ReadLine().Split(","c) it
If you want to perform per-line processing on the elements, do this:
CsvWriter.Write(fOut, headers, CsvReader.Read(fIn, fOptions).Select(Function(line) ProcessLine(line.Values)))
...
Function ProcessLine(input As String()) As String()
'Note: If(input(8), "") returns input(8) unless it is nothing in which case "" is returned instead
If If(input(8), "").Length > 10 Then input(8) = input(8).Remove(10) 'Trim if over 10
If If(input(14), "").Length > 10 Then input(14) = input(14).Remove(10)
Return input 'Always return
End Function

VB.NET - It keep replacing itself

I have in a text file lines of this format:
word1|word2|word3
anotherword1|anotherword2
I'm trying to split each word one by one per every line of that file and once program detect if the richtextbox has one of these words will replace that word with the unsplitted line. Example: From word1 to word1|word2|word3
Here is what I have so far:
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
For Each line As String In File.ReadLines("C:\text.txt")
Dim input As String = line
Dim result As String() = line.Split(New String() {"|"}, StringSplitOptions.None)
For Each s As String In result
Try
Dim linex As String = line
RichTextBox1.Text = RichTextBox1.Text.Replace(s, " " & linex)
Catch exxx As Exception
End Try
Next
Next
End Sub
It works great, but after the replacement, the replaced text still have the detected word and it keep replacing itself with word1|word2|word3 forever. And I want do do the process just once.
Like this: Click to see
Due to the format the words are stored in, it will be much easier to achieve what you want using Regular Expressions:
Dim lines = File.ReadLines("C:\text.txt")
For Each line As String In lines
Dim pat = String.Format("\b({0})\b", line)
RichTextBox1.Text = Regex.Replace(RichTextBox1.Text, pat, line)
Next
This should do pretty much what you want.
Check it here.

position of words in VB.net

I am trying to make a program that shows a keyword's position(s) in a string
my program :
Sub Button1Click(sender As Object, e As EventArgs)
Dim text1 As String = textBox1.Text
Dim keyword As String = textBox2.Text
Dim Array1() As String = text1.Split(" ")
For Each item In Array1
If item = keyword Then
For c = 1 To Array1.Length
Dim input As String
input = c
listbox1.Items.Add("your word appears in the positions" & input)
Next
End If
Next
End Sub
But it does not display the position of that specific word but just the position of every word. Any1 help?
Using the method provided by the .Net framework called .IndexOf("<yourWord>"), you can find the position of a word in VB.Net. If a word exists more than once in a string, use a loop which searches for the word, returns the position of it to an array, then cuts the string to that position and starts over again. At the end, you get an array with all positions of the word you were searching for.
Try this.... it should give you some ideas....
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim Pattern As String = "The"
Dim Test As String = "The quick brown fox jumps over the lazy dog"
For Each m As Match In Regex.Matches(Test, Pattern, RegexOptions.IgnoreCase)
Console.WriteLine(String.Format("'{0}' :: occurs at position {1}", m.Value, m.Index))
Next
End Sub
End Module

VB 2010 - Replace all letters of a string with a dash

VB 2010 - Beginner here,
I am creating a hangman game for a assignment and I am having trouble replacing the text of a string with dashes. I am not sure if I need to convert the string to a charArray() or if I can use the string.Replace function. i know i need to loop it unsure how..
terribly confused i need some help. As I am learning please try and keep it simple with reason please.
My sandbox code so far:
Imports System.IO
Public Class Form1
Private Const TEST = "test.txt"
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim WordString As String = Nothing
Dim NewWordInteger As Integer
Dim RandomWord As New Random(System.DateTime.Now.Millisecond) 'Load a new word at the time it was initiated
'Load words from test file into the array
Dim WordArray() As String = File.ReadAllLines(TEST) 'Reads words from list and declares each as a string
'Select Random word from the number of words in the dictionary.txt file
NewWordInteger = RandomWord.Next(0, 4)
'Display RandomWord in textbox as STRING..
WordString = WordArray(NewWordInteger) ' Assigns wordstring a word from the arrany & random by the NewWordInterger Substring..
WordDisplayTextBox.Text = WordString ' will display the word in the textbox
SolveLabel.Text = WordString ' will display the word in the Solve label
'Will shoe the array word and the word/string position in the TEST.file
ListBox1.Items.Add(WordString) ' will show the word
ListBox2.Items.Add(NewWordInteger) ' will show the string position in the TEST.file
'search string and replace letters with _ (Dashes)
Dim charArray() As Char = WordDisplayTextBox.Text.ToCharArray
For Each item As Char In WordDisplayTextBox.Text
WordDisplayTextBox.Text = WordString.Replace(item, "_")
Next
End Sub
End Class
If you want to replace all the characters of a string with dashes, why not just make a new string that consists only of dashes, that is the same length as the starting string? Isn't that the same thing?
WordDisplayTextBox.Text = New String("_", WordString.Length)
Loop through the length of the WordString and each time write "__" inside the WordDisplayTextBox
For i As Integer = 1 To Len(txtWordString.Text)
WordDisplayTextBox.Text += "__" + " " 'Space separates the dashes for clarity
Next