• 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 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
Link to comment
Share on other sites

  • 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
Link to comment
Share on other sites

  • 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]

Link to comment
Share on other sites

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

    • No registered users viewing this page.