I'm looking to build a simple text string replacement tool using visual studio 2015 community tool, which will do the below replacements on all *.txt files whose path is given in a textbox:

Find: \<figure (\d+)>
Replace: <a href id="fig\1">figure \1</a>

Find: \<table (\d+)>
Replace: <a href id="tab\1">table \1</a>

Find: \<section (\d+)>
Replace: <a href id="sec\1">section \1</a>

I have coded the programme but struggling to successfully run it. I'm completely new in programming and in visual basic is well. Can anyone help complete this programme

Imports System.IO
Imports System.Text.RegularExpressions

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If FBD.ShowDialog = DialogResult.OK Then
            TextBox1.Text = FBD.SelectedPath
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim targetDirectory As String
        targetDirectory = TextBox1.Text
        Dim Files As String() = Directory.GetFiles(targetDirectory, "*.txt")
        Dim pattern1 As String = "\<figure (\d+)\>"
        Dim pattern2 As String = "\<table (\d+)\>"
        Dim pattern3 As String = "\<section (\d+)\>"
        Dim rep1 As String = "<a href id= \""fig\1\"" > figure \1</a>"
        Dim rep2 As String = "<a href id= \""tab\1\"" > table \1</a>"
        Dim rep3 As String = "<a href id= \""sec\1\"" > section \1</a>"
        Dim rgx1 As New Regex(pattern1)
        Dim rgx2 As New Regex(pattern2)
        Dim rgx3 As New Regex(pattern3)
        Dim result1 As String = rgx1.Replace(Files, rep1)
        Dim result2 As String = rgx2.Replace(Files, rep2)
        Dim result3 As String = rgx3.Replace(Files, rep3)
    End Sub
End Class

The tool contains a folderbrowserdialog, a text box and two buttons, one browse and the other replace

Well to correct some of the things within your code.
1) First of all on your Button1 I think you want when you click it shows the FolderBrowserDialog and when click ok you assign the path to a textbox. This is how to do it.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FBD.Show()
REM: Now check to see if the path is not empty
If FBD.SelectedPath = "" Then
REM: Handle here the empty path
Else
REM: The path is not empty so assign it to textbox
TextBox1.Text = FBD.SelectedPath.ToString
End If
End Sub

2) You need to open a file read it lines either all in one or one line at a time and search for your match and if you find it replace it. Also this should be done in a loop so that you wont write many lines, a For Each loop will help loop through each file in an array collection and do desired work for each file.

A simple googling would have helped you as this has some answers online.
Here is how to use regex , heres another way as well , Also this a simple google would have gave you answers. Hope this help you. I think this may also 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.