SemlerPDX

VoiceAttack VB.net Inline Function Examples - Parsing Text File

Nov 25th, 2020 (edited)
1,355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.21 KB | None | 0 0
  1. 'Examples on how to use Inline VB functions to parse a text file
  2.  
  3. Imports Microsoft.VisualBasic
  4. Imports System
  5. Imports System.Collections
  6. Imports System.Collections.Generic
  7. Imports System.Data
  8. Imports System.Drawing
  9. Imports System.Diagnostics
  10. Imports System.Windows.Forms
  11. Imports System.Linq
  12. Imports System.Xml.Linq
  13. Imports System.Threading.Tasks
  14. 'These two may be needed for text parsing functions below, not normally listed in a new VB.net Inline Function action through VA
  15. Imports System.Text
  16. Imports System.Text.RegularExpressions
  17.  
  18. Public Class VAInline
  19.     'This area lets us pre-define variables (and their names) and datatype for use later
  20.     dim dataFinal as new list(of string) ()
  21.     dim contents() as string
  22.     dim entry() as string
  23.    
  24.     'Example Func:  If needed, before Main and after defines, you can define functions used inside Main (if needed)
  25.     function AddOne(ByVal num1 as integer) as integer
  26.         dim result as integer
  27.         if (not(num1 = 0))
  28.             result = num1 + 1
  29.         end if
  30.         AddOne = result
  31.     end function
  32.    
  33.     Public Sub Main()
  34.         'This is where all our inline code goes
  35.        
  36.         'Use of Function Example (from above):
  37.         dim number as integer = 1
  38.         dim number_added as integer = AddOne(number)
  39.         'following this example, the variable 'number_added' will now equal "number" plus 1
  40.        
  41.        
  42.         'RULE 1 for Inlines: check everything is valid before performing any operation (isNot null/Contains a thing/is not == "")
  43.         if VA.GetText("~text_file") IsNot nothing
  44.            
  45.             'Split contents of text file into single lines separated by the newline
  46.             if VA.GetText("~text_file").Contains(vbNewLine)
  47.                 contents = VA.GetText("~text_file").Split(new string() {Environment.NewLine},StringSplitOptions.None)
  48.                
  49.                 'In each line, isolate text to parse, check if already on list, set Text Variable to line
  50.                 for each line as string in contents
  51.                    
  52.                     'Check each line to be sure it is not already been processed / is not empty - This will loop through EACH line until done
  53.                     if ((not(line = "")) and (not(dataFinal.Contains(line))))
  54.                        
  55.                         'By using a list of strings we build at each line, we can avoid duplicates (if needed)
  56.                         dataFinal.Add(line)
  57.                        
  58.                         'EXAMPLE 1:  SIMPLE ENTRY REPLACE
  59.                         'You can transform and parse each line using Replace (if needed)
  60.                         if (line.Contains("<"))
  61.                             line = line.Replace("<", "")
  62.                         end if
  63.                         ' ↑ The example above would replace any "<" characters in this line with nothing (no space, culled completely)
  64.                        
  65.                         'You can split each line further into parts separated by a string/character of your choosing (if needed)
  66.                         if (line.Contains("="))
  67.                             entry = line.Split("=")
  68.                            
  69.                             'NOTE: If you used a split, entry(0) is everything before the split, entry(1) is everything after the split
  70.                            
  71.                             'EXAMPLE 2:  SPLIT ENTRY REPLACE
  72.                             'You can transform and parse each entry using Replace, removing unneeded symbols/syntax
  73.                             if (entry(0).Contains("<"))
  74.                                 entry(0) = entry(0).Replace("<", "")
  75.                             end if
  76.                             ' ↑ The example above would replace any "<" characters in entry(0) with nothing (no space, culled completely)
  77.                            
  78.                             'EXAMPLE 2:  REGEX ENTRY REPLACE
  79.                             'You can even change or add text as needed in place of known existing symbols using RegEx
  80.                             dim regex as Regex = new Regex("\d*\.\d*\.\d*\.\d*")
  81.                             ' ↓ example where you can define a variable name/data type while using it, as opposed to pre-defined above
  82.                             dim match as Match = regex.Match(entry(0))
  83.                             if (match.Success)
  84.                                 entry(0) = Regex.Replace(entry(0), "\d*\.\d*\.\d*\.\d*", "IP ADDRESS REDACTED")
  85.                             end if
  86.                             ' ↑ The example above would replace anything matching the format of an IP address with the literal text in quotes, "IP ADDRESS REDACTED"
  87.                            
  88.                             'When done, you can set it to a text variable for use elsewhere in VA
  89.                             VA.SetText("~text_entry_final", entry(0).ToString())
  90.                            
  91.                             '--OR-- if using Split, you can address one part of it via (0) or (1)
  92.                             VA.SetText("~text_entry_final", entry(1).ToString())
  93.                        
  94.                         end if
  95.                        
  96.                     end if
  97.                
  98.                 'Loop Around to the next line in contents...
  99.                 next
  100.                
  101.             end if
  102.            
  103.         end if
  104.    
  105.     End Sub
  106. End Class
Add Comment
Please, Sign In to add comment