I have two function that replace certain strings in a text file. The first one works perfectly:

Dim readAlias As String
        Dim RxDIR As String = Nothing
        Try
            RxDIR = Registry.GetValue("HKEY_LOCAL_MACHINE\Software\RLtd\R", "MAIN", False)
            RxDIR = RxDIR + "\Bin\Configuration\alias.config"

            Dim sr As StreamReader = File.OpenText(RxDIR)

            readAlias = (sr.ReadToEnd())

            sr.Close()
            sr.Dispose()

            Dim aliasResults As String = readAlias.ToString

            Dim aliasMainDB As String = "database=""(?<name>.+)"""

            Dim m As Match = Regex.Match(readAlias, aliasMainDB, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
            If (m.Success) Then
                'MessageBox.Show(m.Groups("name").Value)
                Dim replace As String = m.Groups("name").Value

                readAlias = Regex.Replace(readAlias, replace, cbListDB.SelectedItem, RegexOptions.IgnoreCase)

                Dim sw As StreamWriter = New StreamWriter(RxDIR)
                sw.Write(readAlias)

                sw.Close()
                sw.Dispose()

                Call mainConfig()

            End If

        Catch ex As Exception

        End Try

The second one runs without error but does not make any changes:

Dim readAlias As String
        Dim RxDIR As String = Nothing
        Try

            workingSQLconnection = "Data Source= " + NameSQL + " ;Initial Catalog=Master;User ID=**;Password=**"

            RxDIR = Registry.GetValue("HKEY_LOCAL_MACHINE\Software\RLtd\R", "MAIN", False)
            RxDIR = RxDIR + "\Bin\Configuration\Templates\alias.config"

            Dim sr As StreamReader = File.OpenText(RxDIR)

            readAlias = (sr.ReadToEnd())

            sr.Close()
            sr.Dispose()

            Dim aliasResults As String = readAlias.ToString

            Dim aliasServer As String = "server=""(?<name>.+)"""

            Dim m As Match = Regex.Match(readAlias, aliasServer, RegexOptions.IgnoreCase Or RegexOptions.Multiline)

            If (m.Success) Then

                Dim replace As String = m.Groups("name").Value

                readAlias = Regex.Replace(aliasResults, replace, NameSQL, RegexOptions.IgnoreCase)

                Dim sd As StreamWriter = New StreamWriter(RxDIR)
                sd.Write(readAlias)

                sd.Close()
                sd.Dispose()

            End If

            Return True

        Catch ex As Exception
            MessageBox.Show(ex.Message)

            Return False

        End Try

The first one is setting a SQL database name ie: mainDB or backupDB.

The second sub is setting the SQL server instance ie: MYPC\SQLEXPRESS

Both are within private functions and the text file is the same alias.config text file.

I have two function that replace certain strings in a text file. The first one works perfectly:

Dim readAlias As String
        Dim RxDIR As String = Nothing
        Try
            RxDIR = Registry.GetValue("HKEY_LOCAL_MACHINE\Software\RLtd\R", "MAIN", False)
            RxDIR = RxDIR + "\Bin\Configuration\alias.config"

            Dim sr As StreamReader = File.OpenText(RxDIR)

            readAlias = (sr.ReadToEnd())

            sr.Close()
            sr.Dispose()

            Dim aliasResults As String = readAlias.ToString

            Dim aliasMainDB As String = "database=""(?<name>.+)"""

            Dim m As Match = Regex.Match(readAlias, aliasMainDB, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
            If (m.Success) Then
                'MessageBox.Show(m.Groups("name").Value)
                Dim replace As String = m.Groups("name").Value

                readAlias = Regex.Replace(readAlias, replace, cbListDB.SelectedItem, RegexOptions.IgnoreCase)

                Dim sw As StreamWriter = New StreamWriter(RxDIR)
                sw.Write(readAlias)

                sw.Close()
                sw.Dispose()

                Call mainConfig()

            End If

        Catch ex As Exception

        End Try

The second one runs without error but does not make any changes:

Dim readAlias As String
        Dim RxDIR As String = Nothing
        Try

            workingSQLconnection = "Data Source= " + NameSQL + " ;Initial Catalog=Master;User ID=**;Password=**"

            RxDIR = Registry.GetValue("HKEY_LOCAL_MACHINE\Software\RLtd\R", "MAIN", False)
            RxDIR = RxDIR + "\Bin\Configuration\Templates\alias.config"

            Dim sr As StreamReader = File.OpenText(RxDIR)

            readAlias = (sr.ReadToEnd())

            sr.Close()
            sr.Dispose()

            Dim aliasResults As String = readAlias.ToString

            Dim aliasServer As String = "server=""(?<name>.+)"""

            Dim m As Match = Regex.Match(readAlias, aliasServer, RegexOptions.IgnoreCase Or RegexOptions.Multiline)

            If (m.Success) Then

                Dim replace As String = m.Groups("name").Value

                readAlias = Regex.Replace(aliasResults, replace, NameSQL, RegexOptions.IgnoreCase)

                Dim sd As StreamWriter = New StreamWriter(RxDIR)
                sd.Write(readAlias)

                sd.Close()
                sd.Dispose()

            End If

            Return True

        Catch ex As Exception
            MessageBox.Show(ex.Message)

            Return False

        End Try

The first one is setting a SQL database name ie: mainDB or backupDB.

The second sub is setting the SQL server instance ie: MYPC\SQLEXPRESS

Both are within private functions and the text file is the same alias.config text file.

The reason your second function isn't working is because of your RegEx expression.

You need to account for the literal \
Here is an example:

Private Sub ReplacePattern()

        Dim strTxt As String
        Dim rgxExp As New System.Text.RegularExpressions.Regex("\d{2}/\d{2}/\d{4}")

        strTxt = "Alison, DOB 01/01/1961; Bryan, DOB 12/13/1969"
        strTxt = rgxExp.Replace(strTxt, "XX/XX/XXXX")

        MessageBox.Show(strTxt)

End Sub
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.