Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15556

[RESOLVED] REGEX - Find and Replace

$
0
0
I am trying to perform a find and replace on blocks of text. I am looking for each block that opens with <GenericEquipment> and closes with <\/GenericEquipment>. I am able to do that with this:

<GenericEquipment>([\S\s]*?)<\/GenericEquipment>

If that block of text contains ("Nokia AEUB"), replace specific lines, shown in code below, to ONLY the blocks containing ("Nokia AEUB").

What I have posted below works but it also changes some of those lines within blocks that DONT contain ("Nokia AEUB"). I added a picture for clarification.

Any help would be greatly appreciated.

Code:


Public Sub FindAndReplaceEquipment(ByVal ocalcFile As String, ByVal revision As String)

    Try

        CreateSubDirectories(ocalcFile, revision)

        Dim saveAsNameAndLocation As String = GetSaveAsNameAndLocation(ocalcFile, revision)

        Dim originalOcalcFile As String = File.ReadAllText(ocalcFile.ToString)

        Dim matches As MatchCollection = Regex.Matches(originalOcalcFile, "<GenericEquipment>([\S\s]*?)<\/GenericEquipment>")

        For Each match As Match In matches

            Dim foundMatch As String = match.Value()

            If foundMatch.Contains("Nokia AEUB") Then

                originalOcalcFile = Regex.Replace(originalOcalcFile, "<VALUE NAME=""DescriptionOverride"" TYPE=""String"">Nokia AEUB</VALUE>", "<VALUE NAME=""DescriptionOverride"" TYPE=""String"">Samsung AT1K01</VALUE>")
                originalOcalcFile = Regex.Replace(originalOcalcFile, "<VALUE NAME=""Description"" TYPE=""String"">Nokia AEUB</VALUE>", "<VALUE NAME=""Description"" TYPE=""String""> Samsung AT1K01</VALUE>")
                originalOcalcFile = Regex.Replace(originalOcalcFile, "<VALUE NAME=""CoordinateZ"" TYPE=""Double"">708</VALUE>", "<VALUE NAME=""CoordinateZ"" TYPE=""Double"">710.75</VALUE>")
                originalOcalcFile = Regex.Replace(originalOcalcFile, "<VALUE NAME=""CoordinateZ"" TYPE=""Double"">648</VALUE>", "<VALUE NAME=""CoordinateZ"" TYPE=""Double"">650.75</VALUE>")
                originalOcalcFile = Regex.Replace(originalOcalcFile, "<VALUE NAME=""CoordinateZ"" TYPE=""Double"">588</VALUE>", "<VALUE NAME=""CoordinateZ"" TYPE=""Double"">590.75</VALUE>")
                originalOcalcFile = Regex.Replace(originalOcalcFile, "<VALUE NAME=""CoordinateZ"" TYPE=""Double"">528</VALUE>", "<VALUE NAME=""CoordinateZ"" TYPE=""Double"">530.75</VALUE>")
                originalOcalcFile = Regex.Replace(originalOcalcFile, "<VALUE NAME=""DiameterOrWidthInInches"" TYPE=""Double"">12</VALUE>", "<VALUE NAME=""DiameterOrWidthInInches"" TYPE=""Double"">9.6</VALUE>")
                originalOcalcFile = Regex.Replace(originalOcalcFile, "<VALUE NAME=""DepthInInches"" TYPE=""Double"">8</VALUE>", "<VALUE NAME=""DepthInInches"" TYPE=""Double"">6.9</VALUE>")
                originalOcalcFile = Regex.Replace(originalOcalcFile, "<VALUE NAME=""HeightInInches"" TYPE=""Double"">24</VALUE>", "<VALUE NAME=""HeightInInches"" TYPE=""Double"">18.5</VALUE>")
                originalOcalcFile = Regex.Replace(originalOcalcFile, "<VALUE NAME=""Weight"" TYPE=""Double"">71</VALUE>", "<VALUE NAME=""Weight"" TYPE=""Double"">45</VALUE>")

            End If

        Next

        My.Computer.FileSystem.WriteAllText(saveAsNameAndLocation, originalOcalcFile, True)

    Catch ex As Exception

        MessageServices.DisplayMessage(Color.FromArgb(64, 64, 64), "Error Finding and Replacing!", "Please ensure you are connected to the Network and try again.", My.Resources.msbExclamation)

    End Try

End Sub

Name:  Diff_Checker.jpg
Views: 65
Size:  39.5 KB
Attached Images
 

Viewing all articles
Browse latest Browse all 15556

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>