Sign in to follow this  
Followers 0
DPyro

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

5 posts in this topic

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.