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

Lui*_*iro 6 notepad++

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

data="32/3"
count id="3" data="0.0237/4"
ext min="1" max="3" data="28.69*2"
Run Code Online (Sandbox Code Playgroud)

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

data="10.666667"
count id="3" data="0.005925"
ext min="1" max="3" data="57.38"
Run Code Online (Sandbox Code Playgroud)

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

ome*_*pes 5

您可以通过创建指向外部 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
Run Code Online (Sandbox Code Playgroud)

请执行下列操作:

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

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

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

  • 根据要求,我已将正则表达式模式更改为`(\w+=)"([\.\d\(\)\\\*\+/-]*)"`:等号是必须的,带括号的表达式也被评估。 (2认同)