Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Examples on how to use Inline VB functions to parse a text file
- Imports Microsoft.VisualBasic
- Imports System
- Imports System.Collections
- Imports System.Collections.Generic
- Imports System.Data
- Imports System.Drawing
- Imports System.Diagnostics
- Imports System.Windows.Forms
- Imports System.Linq
- Imports System.Xml.Linq
- Imports System.Threading.Tasks
- 'These two may be needed for text parsing functions below, not normally listed in a new VB.net Inline Function action through VA
- Imports System.Text
- Imports System.Text.RegularExpressions
- Public Class VAInline
- 'This area lets us pre-define variables (and their names) and datatype for use later
- dim dataFinal as new list(of string) ()
- dim contents() as string
- dim entry() as string
- 'Example Func: If needed, before Main and after defines, you can define functions used inside Main (if needed)
- function AddOne(ByVal num1 as integer) as integer
- dim result as integer
- if (not(num1 = 0))
- result = num1 + 1
- end if
- AddOne = result
- end function
- Public Sub Main()
- 'This is where all our inline code goes
- 'Use of Function Example (from above):
- dim number as integer = 1
- dim number_added as integer = AddOne(number)
- 'following this example, the variable 'number_added' will now equal "number" plus 1
- 'RULE 1 for Inlines: check everything is valid before performing any operation (isNot null/Contains a thing/is not == "")
- if VA.GetText("~text_file") IsNot nothing
- 'Split contents of text file into single lines separated by the newline
- if VA.GetText("~text_file").Contains(vbNewLine)
- contents = VA.GetText("~text_file").Split(new string() {Environment.NewLine},StringSplitOptions.None)
- 'In each line, isolate text to parse, check if already on list, set Text Variable to line
- for each line as string in contents
- 'Check each line to be sure it is not already been processed / is not empty - This will loop through EACH line until done
- if ((not(line = "")) and (not(dataFinal.Contains(line))))
- 'By using a list of strings we build at each line, we can avoid duplicates (if needed)
- dataFinal.Add(line)
- 'EXAMPLE 1: SIMPLE ENTRY REPLACE
- 'You can transform and parse each line using Replace (if needed)
- if (line.Contains("<"))
- line = line.Replace("<", "")
- end if
- ' ↑ The example above would replace any "<" characters in this line with nothing (no space, culled completely)
- 'You can split each line further into parts separated by a string/character of your choosing (if needed)
- if (line.Contains("="))
- entry = line.Split("=")
- 'NOTE: If you used a split, entry(0) is everything before the split, entry(1) is everything after the split
- 'EXAMPLE 2: SPLIT ENTRY REPLACE
- 'You can transform and parse each entry using Replace, removing unneeded symbols/syntax
- if (entry(0).Contains("<"))
- entry(0) = entry(0).Replace("<", "")
- end if
- ' ↑ The example above would replace any "<" characters in entry(0) with nothing (no space, culled completely)
- 'EXAMPLE 2: REGEX ENTRY REPLACE
- 'You can even change or add text as needed in place of known existing symbols using RegEx
- dim regex as Regex = new Regex("\d*\.\d*\.\d*\.\d*")
- ' ↓ example where you can define a variable name/data type while using it, as opposed to pre-defined above
- dim match as Match = regex.Match(entry(0))
- if (match.Success)
- entry(0) = Regex.Replace(entry(0), "\d*\.\d*\.\d*\.\d*", "IP ADDRESS REDACTED")
- end if
- ' ↑ The example above would replace anything matching the format of an IP address with the literal text in quotes, "IP ADDRESS REDACTED"
- 'When done, you can set it to a text variable for use elsewhere in VA
- VA.SetText("~text_entry_final", entry(0).ToString())
- '--OR-- if using Split, you can address one part of it via (0) or (1)
- VA.SetText("~text_entry_final", entry(1).ToString())
- end if
- end if
- 'Loop Around to the next line in contents...
- next
- end if
- end if
- End Sub
- End Class
Add Comment
Please, Sign In to add comment