在 Notepad++ 中搜索并用结果替换数学运算

在 Notepad++ 中搜索并用结果替换数学运算

我有几个大型 txt 文件以下示例:

data="32/3"
count id="3" data="0.0237/4"
ext min="1" max="3" data="28.69*2"

我的目标是搜索所有数学运算并用其结果替换它们,如下所示:

data="10.666667"
count id="3" data="0.005925"
ext min="1" max="3" data="57.38"

有没有办法在 Notepad++ 中自动执行此操作?

答案1

您可以通过创建外部 VBScript 的快捷方式在 Notepad++ 中自动执行此操作。以下是脚本:

Option Explicit

Const FileEncoding = 0 ' 0 = ASCII, -1 = Unicode, -2 = System Default
Const FractDigits = 6 ' number of fractional digits

Dim objList, strPath

If WScript.Arguments.Count = 0 then
    CreateObject("WScript.Shell").PopUp "Drop folder(s) and / or file(s) to the script to process", 3, , 48
    WScript.Quit
End If

Set objList = ReadContent(WScript.Arguments)

If objList.Count = 0 Then
    CreateObject("WScript.Shell").PopUp "No files found", 3, , 48
    WScript.Quit
End If

With CreateObject("VBScript.RegExp")
    .Global = True
    .MultiLine = True
    .IgnoreCase = False
    .Pattern = "(\w+=)""([\.\d\(\)\\\*\+/-]*)"""
    For Each strPath In objList
        WriteToFile .Replace(objList(strPath), GetRef("FnReplace")), strPath, FileEncoding
    Next
End With
CreateObject("WScript.Shell").PopUp "Completed", 1, , 64

Function FnReplace(strMatch, strSubMatch1, strSubMatch2, lngPos, strSource)
    Dim strResult
    On Error Resume Next
    strResult = CStr(Round(Eval(strSubMatch2), FractDigits))
    If Err Then
        Err.Clear
        FnReplace = strMatch
    Else
        FnReplace = strSubMatch1 & """" & strResult & """"
    End If
End Function

Function ReadContent(arrList)
    Dim objList, strPath
    Set objList = CreateObject("Scripting.Dictionary")
    For Each strPath In arrList
        AddContent strPath, objList
    Next
    Set ReadContent = objList
End Function

Sub AddContent(strPath, objList)
    Dim objItem
    With CreateObject("Scripting.FileSystemObject")
        If .FileExists(strPath) Then
            objList(strPath) = ReadFromFile(strPath, FileEncoding)
        End If
        If .FolderExists(strPath) Then
            For Each objItem In .GetFolder(strPath).Files
                AddContent objItem.Path, objList
            Next
            For Each objItem In .GetFolder(strPath).SubFolders
                AddContent objItem.Path, objList
            Next
        End If
    End With
End Sub

Function ReadFromFile(strPath, intFormat)
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 1, False, intFormat)
        ReadFromFile = ""
        If Not .AtEndOfStream Then ReadFromFile = .ReadAll
        .Close
    End With
End Function

Sub WriteToFile(strCont, strPath, intFormat)
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 2, True, intFormat)
        .Write(strCont)
        .Close
    End With
End Sub

请执行下列操作:

  • 将此脚本保存到文件,例如C:\Test\MathResults.vbs
  • 在 Notepad++ 中打开文本文件
  • 点击菜单 - 运行(或F5
  • "C:\Test\MathResults.vbs" "$(FULL_CURRENT_PATH)"在要运行的程序字段中输入引号
    指定路径
  • 点击Save...
  • 创建快捷方式,输入 MathResults 作为名称,输入Ctrl+F7作为热键
    选择热键
  • 点击OK
  • 点击Run

现在你的快捷方式已经保存在配置文件中,你可以直接打开文本文件,按Ctrl+ F7,一旦脚本完成处理,就会出现重新加载对话框,单击Yes以显示更改的文件(你可以设置文件在更改后自动重新加载)。就是这样。

顺便说一句,这个脚本可以完美地独立运行,您可以在资源管理器窗口或桌面上选择要处理的文件和文件夹,然后将其拖放到脚本文件中。

答案2

仅使用 Notepad++ 技术无法实现。但您可以使用 Powershell 来完成工作,并在 Notepad++ 中使用它

之前和之后

在此处输入图片描述在此处输入图片描述

评估公式.ps1

$Content = Get-Content -Path $Args[0]

$Formulas = ($Content | Select-String -Pattern '(?<=data=").*?(?=")' -AllMatches).Matches

ForEach ($Formula in $Formulas){
    $Result = Invoke-Expression -Command $Formula.Value
    $Result = [Math]::Round($Result,6)
    $Content = $Content -Replace [Regex]::Escape($Formula), $Result
}
Set-Content -Path $Args[0] -Value $Content

解释

  • 正则表达式模式(?<=data=").*?(?=")使用前瞻与后瞻捕获介于data="和下一个之间的所有内容"。这样,您便会得到字符串 32/3、0.0237/4 和 28.69*2 作为示例
  • 接下来,我们用Invoke-Expression这些字符串计算数学结果。您将得到 10.6666666666667、0.005925 和 57.38
  • 如果需要,快速Math::Round(x,6)将结果四舍五入为 6 位小数
  • 现在我们使用正则表达式搜索和替换来搜索整个文件中的当前公式字符串(记住,我们处于循环中)并将其替换为我们的计算结果
  • 最后一步是将修改后的内容写入任何所需的输出文件

没什么特别的。最难的部分是找出如何替换特殊字符(*就是这样的)。这里有[regex]::Escape()解决办法。


该脚本可以在 Notepad++ 中以与@omegastripes 描述的相同的方式使用。

  1. 在 Notepad++ 中按F5并输入:

    Powershell -File "C:\your\path\to\EvaluateFormulas.ps1" "$(FULL_CURRENT_PATH)"
    
  2. 单击Save并定义快捷方式

注意事项

在当前状态下,所有更改都会立即保存到文件中。运行前请进行备份

相关内容