【问题标题】:VB.Net Search for text and replace with file contentVB.Net 搜索文本并替换为文件内容
【发布时间】:2019-03-26 07:08:47
【问题描述】:

这是对我发布的帖子的后续问题。 Append one file into another file

我需要在主文档中搜索实体"&CH1.sgm""&CH33.sgm", 标记它们在主文档中的位置,并将实体调用替换为“fnFiles”中的匹配文件"Chapter1.sgm"。如果有帮助,我可以将文件名和实体更改为任何内容。

我的代码复制文件的文本并将其附加到 master_document.sgm 的底部。但现在我需要它更智能。在主文档中搜索实体标记,然后将该实体标记替换为该文件内容匹配。文件编号和实体编号匹配。例如(&CH1; 和 Bld1_Ch1.sgm)

Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click
    Dim searchDir As String = txtSGMFile.Text 'Input field from form
    Dim masterFile = "Bld1_Master_Document.sgm"
    Dim existingFileMaster = Path.Combine(searchDir, masterFile)

    'Read all lines of the Master Document
    Dim strMasterDoc = File.ReadAllText(existingFileMaster) '// add each line as String Array.

    '?search strMasterDoc for entities &Ch1.sgm
    '?replace entity name "&Ch1.sgm" with content of file "Bld1_Ch1.sgm" this content if found below
    '? do I use a book mark? Replace function?


    'Get all the sgm files in the directory specified
    Dim fndFiles = Directory.GetFiles(searchDir, "*.sgm")
    'Set up the regular expression you will make as the condition for the file
    Dim rx = New Regex(".*_Ch\d\.sgm")
    Dim ch1 = New Regex(".*_Ch[1]\.sgm")
    'Use path.combine for concatenatin directory together

    'Loop through each file found by the REGEX
    For Each fileNo In fndFiles
        If rx.IsMatch(fileNo) Then
            If ch1.IsMatch(fileNo) Then
                Dim result = Path.GetFileName(fileNo)
                'Use path.combine for concatenatin directory together
                Dim fileToCopy = Path.Combine(searchDir, result)

                'This is the file we want to copy into MasterBuild but at specific location.
                'match &ch1.sgm inside strMasterDoc

                Dim fileContent = File.ReadAllText(fileToCopy)

                'Search master file for entity match then append all content of fileContent


                File.AppendAllText(existingFileMaster, fileContent)

                MessageBox.Show("File Copied")
            End If
        End If
    Next
    Close()
End Sub

【问题讨论】:

  • IMO,您应该使用正则表达式在您的主文档中查找匹配模式(&CH1.sgm&CH2.sgm 等)。 Regex.Matches 返回匹配的集合,它报告在字符串中找到匹配的索引位置以及(除其他外)匹配模式的长度。然后您可以使用这些信息将Insert 一个字符串(对应于外部文件内容)中的引用位置。使用找到的模式加载相关的文本文件。使用StringBuilder 而不是简单的string 来保存文件内容。
  • 看起来您刚刚粘贴了另一个问题的固定代码,但我没有看到任何尝试实现您的新要求的痕迹。尝试自己解决问题,然后返回遇到的任何具体问题。
  • 我一直在研究它并在网上搜索答案。我发布的是我文件的最干净版本,因此您不会看到我之前的所有尝试和错误。我不会只是粘贴代码并期望每个人都为我编写它。但我很感激你想让我诚实。最大

标签: vb.net


【解决方案1】:

如果我理解正确(大如果),您想将主文件中的缩写章节名称的文本替换为它在找到缩写的位置引用的文件的内容。

我做了一个类来处理细节。

Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click
    'Add a FolderBrowseDialog to your form designer
    FolderBrowserDialog1.ShowDialog()
    Dim searchDir As String = FolderBrowserDialog1.SelectedPath
    Dim existingFileMaster = Path.Combine(searchDir, "Bld1_Master_Document.sgm")
    Dim lstFileChanges = CreateList(searchDir)
    'The following method does NOT return an array of lines
    Dim strMasterDoc = File.ReadAllText(existingFileMaster)
    For Each fc In lstFileChanges
        strMasterDoc = strMasterDoc.Replace(fc.OldString, fc.NewString)
    Next
    File.WriteAllText(existingFileMaster, strMasterDoc)
End Sub

Private Function CreateList(selectedPath As String) As List(Of FileChanges)
    Dim lstFC As New List(Of FileChanges)
    For i = 1 To lstFC.Count
        Dim fc As New FileChanges
        fc.OldString = $"&CH{i}.sgm"
        fc.FileName = $"Chapter{i}.sgm"
        fc.NewString = File.ReadAllText(Path.Combine(selectedPath, fc.FileName))
        lstFC.Add(fc)
    Next
    Return lstFC
End Function

Public Class FileChanges
    Public Property OldString As String '&CH1.sgm 
    Public Property FileName As String 'Chapter1.sgm
    Public Property NewString As String 'Contents of Chapter1.sgm, the string to insert
End Class

测试.替换

Dim s As String = "The quick brown fox jumped over the lazy dogs."
s = s.Replace("fox", "foxes")
MessageBox.Show(s)

【讨论】:

  • strMasterDoc = strMasterDoc.Replace(fc.OldString, fc.NewString)For i = 1 To 33?嗯。所以,你事先数过了? (我知道,问题是这样说的……但是……)
  • @Mary 谢谢你,这很棒,但它不会替换 strMasterDoc.Replace(fc.OldString, fc.NewString) 中的文本。如果我做一个对话框检查以查看每个变量中的内容。没错,它抓住了正确的文字。
  • strMasterDoc 计数是无限的。我只是把 33 作为一个起点
  • @Jimi 更改为 lstFC.Count。谢谢
  • @MaxineHammett 将 33 更改为 lstFC.Count
猜你喜欢
  • 2016-09-22
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多