• 0

[VB.NET] Regex.Match write line to text file.


Question

I am reading the StandardOutput of a process to a string and would like to match certain text and write the full line to a text file.

For example:

Filename: file1

ID = ID100GH

Version = 1.00

Size: 251,088 bytes

I want it to read the standard output and write the entire line if it matches say ID or Version on separate lines to a text file.

4 answers to this question

Recommended Posts

  • 0

not sure if this is what you're looking for and my vb.net is a little rusty (and i hate the syntax) but this might help.


Dim TestString As String = "Filename: file1" & vbCrLf &
"ID = ID100GH" & vbCrLf &
"Version = 1.0" & vbCrLf &
"Size: 251,088 bytes"

Dim PatternString As String = "ID = (.*)"

Dim Result As Match = Regex.Match(TestString, PatternString, RegexOptions.None)

If (Result.Captures.Count > 0) Then
Dim Test As String = Result.Captures(0).Value
End If
[/CODE]

or in C#

[CODE]
String Content = @"Filename: file1
ID = ID100GH
Version = 1.00
Size: 251,088 bytes";

String PatternMatcher = @"ID = (.*)";
Match RegexMatcher = Regex.Match(Content, PatternMatcher, RegexOptions.None);

if (RegexMatcher.Captures.Count > 0)
{
String Result = RegexMatcher.Captures[0].Value;
}
[/CODE]

This is an example fyi. Replace TestString with your StdOutput data, etc.

  • 0

Hmm, maybe I could use something like this:


Dim value As String = "ID = ID100GH"
If value.StartsWith("ID") Then
WriteLine
End If
[/CODE]

EDIT:

I think I found what I need

[CODE]


Dim output = New StringBuilder()

' Read the lines from FileName into an array of strings. '
Dim input = File.ReadAllLines(FileName)

For Each line in input

If line.StartsWith("ID") OrElse
line.StartsWith("Version") Then

output.AppendLine(line)

End If

Next
[/CODE]

Edited by DPyro
  • 0

Ok that didn't seem to work :/

This works but now I need to write it to a file:


For Each line As String In txtOutputLog.Lines
If line.StartsWith("ID") OrElse
line.StartsWith("Version") Then
txtOutputLog.Text += line & vbCrLf
End If
Next
[/CODE]

Edited by DPyro
  • 0

For reference, here is my final code:


Public out as string
While output.HasExited = False
Dim sLine As String = output.StandardOutput.ReadLine
If (Not String.IsNullOrEmpty(sLine)) Then
out &= sLine & "~"
End If
Application.DoEvents()
End While
.WaitForExit()
.Close()
[/CODE]

[CODE]
Dim swriter As StreamWriter
Dim strOutput As String = (Application.StartupPath & "\" & output & ".txt")
For Each line As String In out.Split("~")
swriter = File.AppendText(strOutput)
If line.StartsWith("ID") OrElse
line.StartsWith("Version") Then
swriter.WriteLine(line)
End If
swriter.Close()
Next
out = ""
txtOutputLog.Text = My.Computer.FileSystem.ReadAllText(strOutput) & vbCrLf & "Output file saved to: " & vbCrLf & strOutput
[/CODE]

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Isn't that allowed on member forums?
    • Sure is a shame that Google spent all of its efforts on maximizing milking ###### ad revenue instead of actually improving their once ubiquitous and best-in-class search engine with, say, a useful AI that could actually improve search results... /S
    • The blatant anti-competitive nature of the EU's DMA rules are obscenely IRONIC and wholly damaging to upwards innovations from within the EU. Why?! Because it penalizes marketplace competitors whom the users CHOOSE to sign contracts with (basically dictating the product management models and strategies whilst handicapping popular marketplace choices) all under the guise of 'fair competition' and assuring continued operations. The only beneficiary (as the DMA is written and applied) are the toxic wannabes with whom most consumers have chosen to NOT do business with! What kind of bass-ackwards nonsense is that?! Not everyone in the marketplace that's lower-ranked deserves some sort of advantage whilst being otherwise garbage and toxic. Ugh, so dumb.
    • Assetto Corsa Competizione, House Flipper 2, and more join Xbox Free Play Days by Pulasthi Ariyasinghe There is a brand-new Free Play Days offer available to Xbox players, giving them a chance to try out a new selection of games over the weekend. Microsoft's latest promotion brings in three games for Xbox players to try out, and they are House Flipper 2, Blades of Fire, and Assetto Corsa Competizione. Unlike in recent weeks, no games from this selection are being offered without a Game Pass subscription this time. This means only Xbox Game Pass Ultimate, Premium, and Essential members can jump into the three titles, even if they are single-player. From the trio, House Flipper 2 is for simulation fans. The title lets you buy run-down properties, renovate them, and sell them for a profit. A sandbox mode even lets you build houses from scratch too. Next, Blades of Fire lands as a fantasy adventure that combines metroidvania exploration with punishing combat. One unique aspect is that instead of finding loot, you'll be physically forging your own weapons. Lastly, we go back to simulators with Assetto Corsa Competizione, with the hit racing title offering a hardcore, highly competitive racing game focused entirely on GT3 and GT4 class cars. Everything from the tracks and tire physics to the dynamic weather conditions are focused on giving players an authentic driving experience. Here are the announced games and the platforms they are available to play on: House Flipper 2 (Xbox Series X|S) Blades of Fire (Xbox Series X|S) Assetto Corsa Competizione (Xbox Series X|S, Xbox One) To easily find the titles on Xbox consoles, first head to the Store, and then in the sidebar, find the Home section. In there, open the Subscriptions tab. All the games from the Free Play Days collection will show up in this section for quick access. This week's Free Play Days promotions will end on Sunday, June 28, at 11:59 pm PT.
  • Recent Achievements

    • First Post
      kinowa earned a badge
      First Post
    • Rookie
      krychek57 went up a rank
      Rookie
    • Grand Master
      Jaybonaut went up a rank
      Grand Master
    • One Year In
      Philsl earned a badge
      One Year In
    • Dedicated
      Scoobystu earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      405
    2. 2
      +Edouard
      168
    3. 3
      PsYcHoKiLLa
      131
    4. 4
      Michael Scrip
      72
    5. 5
      Xenon
      72
  • Tell a friend

    Love Neowin? Tell a friend!