Hack Forums
[.NET] Using regex match function to modify data? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Using regex match function to modify data? (/showthread.php?tid=5396888)



Using regex match function to modify data? - Whateva_ - Sep 5, 2016

I want trying make a program which will look for a expression <disp-formula id="deqn(\d+)-(\d+)"> in files and if there is one or more match, then it will search the whole file for expressions in the form <xref ref-type="disp-formula" rid="deqnX">(X)</xref> or <xref ref-type="disp-formula" rid="deqnX">X</xref> where X ranges from the first (\d+) to the last (\d+) and replace it to <xref ref-type="disp-formula" rid="deqn$1-$2">(X)</xref> or <xref ref-type="disp-formula" rid="deqn$1-$2">X</xref>. This is what I've come up with so far,

Code
Dim targetDirectory As String = TextBox1.Text
        Dim txtFilesArray As String() = Directory.GetFiles(targetDirectory, "*txt", SearchOption.AllDirectories)

        For Each txtFile In txtFilesArray
            Dim input As String = File.ReadAllText(txtFile)
            Dim disp As New Regex("<disp-formula id=""deqn(\d+)-(\d+)"">")
            Dim match As Match = disp.Match(input)
            If match.Success Then
                Dim a = match.Groups(1).Value
                Dim b = match.Groups(2).Value
                Dim d As String = a  & "-" & b
                For Each c = a To b In input
                        Dim xref As New Regex("rid=""deqn" & c & "")
                    Dim result As String = xref.Replace(input, "rid=""deqn" & d & "")
                Next
            End If
            File.WriteAllText(txtFile, result)
        Next

Can anyone help me on this??


* - Chickendipper - Sep 5, 2016

Why not use some sort of XML parser? There's a gazillion of them built in to the .NET framework and all of them would make this much, much easier.