Jump to content



Photo

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


  • Please log in to reply
4 replies to this topic

#1 DPyro

DPyro

    Neowinian Senior

  • Joined: 11-December 05
  • Location: Ontario,Canada

Posted 18 November 2012 - 04:14

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.


#2 Squirrelington

Squirrelington

    Squirrelies!

  • Tech Issues Solved: 1
  • Joined: 26-December 02
  • Location: Oshkosh, WI, USA
  • OS: Windows 8.1 64-bit Enterprise
  • Phone: HTC One X (GSM (International) Tegra 3 - S720e / HTC__203) - AT&T - 4.2.2

Posted 18 November 2012 - 04:42

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

or in C#

		    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;
		    }


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

#3 OP DPyro

DPyro

    Neowinian Senior

  • Joined: 11-December 05
  • Location: Ontario,Canada

Posted 18 November 2012 - 05:06

Hmm, maybe I could use something like this:

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

EDIT:
I think I found what I need


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

Edited by DPyro, 18 November 2012 - 05:22.


#4 OP DPyro

DPyro

    Neowinian Senior

  • Joined: 11-December 05
  • Location: Ontario,Canada

Posted 18 November 2012 - 06:35

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

Edited by DPyro, 18 November 2012 - 06:58.


#5 OP DPyro

DPyro

    Neowinian Senior

  • Joined: 11-December 05
  • Location: Ontario,Canada

Posted 20 November 2012 - 03:36

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()
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