Hi all,

I know I can do basic text searches to see if the text is there but I have no idea how to actually pull some select data out of a text file into a variable.

I can search with something like this function:

Private Function CheckFile(ByVal FileName As String, ByVal strSearch As String) As Boolean
        Dim s As String = My.Computer.FileSystem.ReadAllText(FileName)
        If InStr(s, strSearch) Then
            Return True
        Else
            Return False
        End If
    End Function

But lets say my text file contains the following:

<add
				name="MAIN"
				server="192.168.1.103\SQLSERVER"
				database="PCMain"
				trustedConnection="false" />

and I want to put MAIN, 192.168.1.103\SQLSERVER and PCMain into variable's how would I do that?

If you can help me can you also please help me understand the code?

Here is an example of how you can use regex to extract the "name" component of the text. You can expand on the regex to get addition fields.

Imports System.Text.RegularExpressions
Imports System.IO

Public Class frmRegex

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim expr2 As String = "name=""(?<name>.+)"""
		Dim fileTxt As String = File.ReadAllText("C:\file.txt")
		Dim m As Match = Regex.Match(fileTxt, expr2, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
		If (m.Success) Then
			MessageBox.Show(m.Groups("name").Value)
		End If
	End Sub
End Class

[edit] C:\file.txt is the input you provided your original post [/edit]

commented: Great!!! +1

works perfect. Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.