在vb.net上使用正则表达式替换多个文本文件中的字符串

在vb.net上使用正则表达式替换多个文本文件中的字符串,vb.net,Vb.net,我希望使用visual studio 2015社区工具构建一个简单的文本字符串替换工具,该工具将对文本框中给定路径的所有*.txt文件进行以下替换: 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: \&l

我希望使用visual studio 2015社区工具构建一个简单的文本字符串替换工具,该工具将对文本框中给定路径的所有*.txt文件进行以下替换:

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>

对于“替换”按钮,一旦有了指向文件夹目录的链接,就需要读入所有标有“*.txt”的文件。下面的一行就是这样做的

targetDirectory = TextBox1.text
Dim txtFilesArray As String() = Directory.GetFiles(targetDirectory,"*.txt")
然后,您可以循环这个数组并执行替换逻辑

    For each txtFile in txtFilesArray
        'here we grab the files information as we need the files directory
        Dim FileInfo As New FileInfo(txtFile)
        Dim FileLocation As String = FileInfo.FullName
        Dim input() as string = File.ReadAllLines(FileLocation)
        'now you have read in your text file you can edit each file as it goes through the loop
        'you can now use your regex here to edit each file, 
        'then once done editing the file, dont forget to write back to your file or it wont save
        'you will need to loop through the input array now to change the line
       For x as integer = 0 to (input.length - 1)
         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(input(x), rep1)
         Dim result2 As String = rgx2.Replace(result1, rep2)
         Dim result3 As String = rgx3.Replace(result2, rep3)
         input(x) = result3
       Next
      'now you can write your corrected file back to the file
      File.WriteAllLines(FileLocation, input)
    Next

  MsgBox("process complete")
txtFilesArray中每个txtFile的

'这里我们获取文件信息,因为我们需要文件目录
将文件信息作为新文件信息(txtFile)进行调整
Dim FileLocation为String=FileInfo.FullName
Dim input()作为字符串=File.ReadAllLines(文件位置)
'现在您已经在文本文件中读取了内容,您可以在每个文件通过循环时对其进行编辑
'您现在可以在此处使用正则表达式编辑每个文件,
一旦编辑完文件,别忘了写回你的文件,否则它不会保存
'现在需要循环输入数组以更改行
对于x作为整数=0到(input.length-1)
将图案1变暗为字符串=“\”
将图案2变暗为字符串=“\”
将图案3变暗为字符串=“\”
Dim rep1 As String=“”
Dim rep2 As String=“”
Dim rep3 As String=“”
将rgx1调整为新正则表达式(模式1)
将rgx2调整为新正则表达式(模式2)
将rgx3调整为新正则表达式(模式3)
Dim result1作为字符串=rgx1.Replace(输入(x),rep1)
Dim result2作为字符串=rgx2。替换(result1,rep2)
Dim result3作为字符串=rgx3。替换(result2,rep3)
输入(x)=结果3
下一个
'现在您可以将已更正的文件写回该文件
File.writeAllines(文件位置,输入)
下一个
MsgBox(“流程完成”)

@Tamal Banerjee,试试这个进度条,把下面的代码放在编码中最后一个
Next
之后

Next
ProgressBar1.PerformStep()
ProgressBar1.Value = 100
MessageBox.Show("Process complete")
End Sub

我不确定这是否是一种写入方式,但在我的计算机上用您的编码尝试时,它起了作用:)

亲爱的Cal cium,谢谢您的回复,请您详细说明一下如何将替换代码Dim result1修改为String=rgx1。替换(输入,rep1)在“文件中的每个文件”中????因为替换方法需要“输入”和“替换”。那么,我如何修改“输入”部分?您是否试图编辑这些文件的内容?是的。每个文件的内容将根据给定的正则表达式替换。在每个“for each file in Files”循环中,您需要读取文本文件并对其进行编辑。您已经有了文件列表“files”,循环正在遍历每个文件“file”。另外,请检查更新的答案,了解我是如何根据您的说明更新编码的,但它显示了一些错误,您能帮我吗。请检查我更新的问题。在我的问题上,当进度条仍在增加时,会出现消息框。我所做的方式是将它放在for each循环
ProgressBar1.max=txtfilesrarray.Count
之前,然后将它放在最后一个
ProgressBar1.Increment(1)Refresh()线程。Sleep(100)
最后把它放在messagebox
线程之前。Sleep(400)MsgBox(“进程完成”)
ProgressBar1。Increment(1)Refresh()线程。Sleep(100)
在一行吗?我把它像
ProgressBar1.Increment(1)
Refresh()
Thread.Sleep(100)
一样放在三行中,但它显示了一个错误
error BC30451'Thread'未声明。由于它的保护级别,它可能无法访问。
不抱歉,每一行都在一条单独的线上。它们是3个独立的代码位,您需要确保程序中有
Imports System.Threading
    For each txtFile in txtFilesArray
        'here we grab the files information as we need the files directory
        Dim FileInfo As New FileInfo(txtFile)
        Dim FileLocation As String = FileInfo.FullName
        Dim input() as string = File.ReadAllLines(FileLocation)
        'now you have read in your text file you can edit each file as it goes through the loop
        'you can now use your regex here to edit each file, 
        'then once done editing the file, dont forget to write back to your file or it wont save
        'you will need to loop through the input array now to change the line
       For x as integer = 0 to (input.length - 1)
         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(input(x), rep1)
         Dim result2 As String = rgx2.Replace(result1, rep2)
         Dim result3 As String = rgx3.Replace(result2, rep3)
         input(x) = result3
       Next
      'now you can write your corrected file back to the file
      File.WriteAllLines(FileLocation, input)
    Next

  MsgBox("process complete")
Next
ProgressBar1.PerformStep()
ProgressBar1.Value = 100
MessageBox.Show("Process complete")
End Sub