• 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

    • Apple reportedly has a second-generation iPhone Fold planned for 2027 by Hamid Ganji The iPhone Fold is one of the most anticipated tech products expected to debut this fall. It will be Apple’s first foldable iPhone, ushering in a new product category for the company. While the first generation has yet to hit the shelves, a new leak suggests Apple has already begun work on its successor. Chinese leaker Digital Chat Station claims that the second-generation iPhone Fold has already been confirmed, meaning Apple could launch a successor in fall 2027. The foldable iPhone is also reportedly referred to as the “iPhone Ultra,” though it remains unclear whether Apple will ultimately choose that branding, especially as Samsung is rumored to rename the Galaxy Z Fold 8 as the Galaxy Z Fold Ultra this year. The leaker also claims that the second-generation foldable will feature a wider folding display while reusing the same screen found in the first generation. Apple’s first foldable iPhone is expected to feature a 7.8-inch inner display and a 5.3-inch outer screen in a passport-style form factor. It has already been reported that Apple plans to change its iPhone release cycle in 2026 to spread launches throughout the year. Under this strategy, the iPhone Fold is expected to debut this fall alongside the iPhone 18 Pro and iPhone 18 Pro Max. The standard iPhone 18 and iPhone Air 2 are expected to arrive later in 2026 or in early 2027. Speaking of the iPhone Air, Digital Chat Station says Apple remains undecided about a third-generation model. The company is reportedly waiting to see how the iPhone Air 2 performs in the market, and if sales disappoint, a successor may never materialize. As we reported this week, the iPhone Air has not been scrapped from Apple’s plans. The second-generation model is reportedly scheduled for spring 2027 and could introduce upgrades such as an additional rear camera for ultrawide photography and improved battery life.
    • ahh yes the good old your opinion differs from mine so you are therefore insane lol destiny 1 had no agenda pushing and was a massive success of a game, if you clearly look online the team for some reason thought they had too many men on the team and went on a woman and dei recruitment drive and we all know how destiny 2 performed from then on in
    • The limited imaginations and business acumen of non-dominant players is simply that: the abject lack of creative business acumen. Businesses often want to operate in a financially-rewarding marketplace (free market economics) and/or exit/cash-out at maximal financial recompense. Money is their incentive; regulations are both their obstacles and their tools; politics is their means of influencing the marketplace. Google, in this story's example, is crying that AWS and Azure are "too dominant" -- cuz Google Cloud is not printing as much money as Alphabet wants (although it is still dramatically more than they actually need). The EU DMA should truly follow-the-money and treat the EU as its own sovereign nation in order to protect European market players: Domestic entities are exempt from market-influence regulations until absolute monopoly is achieved; Foreign (non-EU/non-Euro) entities are all regulated via stricter DMA measures whereby regulated partnership with independent domestic entity becomes the only way for foreign entities to 'tip the scale' for favorable financial remunerations. Basically create a dual-track aligning with China's foreign investment models. In my eyes, this is the only way to properly protect the European marketplace beyond the current dot-com/ai-bubble/social-media crazes.
    • I have a fire n ice theme w my bedroom laptops. one is a red lenovo gaming laptop (fire) and the precision is ice
  • Recent Achievements

    • One Year In
      Vistor earned a badge
      One Year In
    • 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
  • Popular Contributors

    1. 1
      +primortal
      406
    2. 2
      +Edouard
      172
    3. 3
      PsYcHoKiLLa
      131
    4. 4
      Xenon
      72
    5. 5
      Michael Scrip
      69
  • Tell a friend

    Love Neowin? Tell a friend!