How to recode existing text filter for regex search and replace?

天涯浪子 提交于 2020-05-17 18:44:10

问题


I was able to cobble the code below so that it performs multiple search and replace functions as a text filter. The code works in the EditPlus text editor program.

I would like to execute the same idea except with regex search and replace.

What is the correct way to code for multiple regex Search and Replaces in a text filter?

enter code here

Option Explicit

Dim oWS, oFS

Set oWS = WScript.CreateObject("WScript.Shell")
Set oFS = WScript.CreateObject("Scripting.FileSystemObject")

'----------
' Script Setup
'----------
Dim oInStream, oOutStream, sInFile, sOutFile, nTotalLines, sArg

'----------
' Process File(s)
'----------
If Wscript.Arguments.Count = 0 Then
If InStr(LCase(WScript.FullName), "cscript.exe") <> 0 Then
sInFile = "StdIn"
sOutFile = "StdOut"
Set oInStream = WScript.StdIn
Set oOutStream = WScript.StdOut
Call ProcessFile()
Else
Call HelpMsg()
End If
Else
For sArg = 0 To Wscript.Arguments.Count -1
sInFile = Wscript.Arguments(sArg)
If IsFile(sInFile) Then
sOutFile = Left(sInFile, InStrRev(sInFile, ".") - 1) & ".lst"
Set oOutStream = oFS.OpenTextFile(sOutFile, 2, True, 0)
Set oInStream = oFS.OpenTextFile(sInFile, 1)
Call ProcessFile()
oWS.Run "NotePad.exe " & sOutFile
Else
Wscript.Echo "File Not Found: " & sInFile, , "Error"
End If
Next
End If

Call CleanUp(0)

'---------------------
' Subroutines
' ********************

'---------------------
Sub CleanUp(exitCode)
Set oInStream = Nothing
Set oOutStream = Nothing
Set oWS = Nothing
Set oWS = Nothing
WScript.Quit(exitCode)
End Sub

'---------------------
Sub ProcessFile()
'oOutStream.WriteLine "<DIV class='mesa'>"
nTotalLines = SeaRep()
'oOutStream.WriteLine "</DIV>"
End Sub

'---------------------

'---------------------
' Functions
' ********************
'---------------------
Function SeaRep()
Dim nLine, sLine, nCount, outPut1, outPut2, outPut3
nCount = 0
Do Until oInStream.AtEndOfStream
nLine = oInStream.Line
sLine = oInStream.ReadLine

outPut1 = Replace(sLine,"1a","foo")
outPut2 = Replace(outPut1,"2b","foobar")
outPut3 = Replace(outPut2,"3c","foosod")
oOutStream.WriteLine (Replace(outPut3,"4d","fooyard"))

nCount = nCount + 1
Loop
AddLineNum = nCount
End Function


'---------------------
'---------------------
Function IsFile (fName)
If oFS.FileExists(fName) Then IsFile = True Else IsFile = False
End Function

' ********************

' End code

回答1:


Replacing with regular expressions isn't that different from "normal" replacing. You just have the additional step of preparing the regular expression(s):

Set re = New RegExp
re.Pattern    = "1a"
re.IgnoreCase = True
re.Global     = True

The actual replacement is done like this (no search text, because that information is contained in the regular expression):

outPut1 = re.Replace(sLine, "foo")

Since you may need several regular expressions it's probably best to encapsulate their creation in a function:

Function CreateRegExp(str)
  Set re = New RegExp
  re.Pattern    = str
  re.IgnoreCase = True
  re.Global     = True

  Set CreateRegExp = re
End Function

Then your code might look somewhat like this:

Set re1 = CreateRegExp("1a")
Set re2 = CreateRegExp("2b")
Set re3 = CreateRegExp("3c")
Set re4 = CreateRegExp("4d")

'...

Function SeaRep()
  Dim nLine, sLine, nCount, outPut1, outPut2, outPut3
  nCount = 0
  Do Until oInStream.AtEndOfStream
    nLine = oInStream.Line
    sLine = oInStream.ReadLine

    outPut1 = re1.Replace(sLine, "foo")
    outPut2 = re2.Replace(outPut1, "foobar")
    outPut3 = re3.Replace(outPut2, "foosod")
    oOutStream.WriteLine (re4.Replace(outPut3, "fooyard"))

    nCount = nCount + 1
  Loop
  AddLineNum = nCount
End Function



回答2:


sorry if I see that now. Look if you do not know yet it's very possible to code your filters with php cli. in case you know php and you want to write custom EDITPLUS filters (like I did): eg I use xampp (apachefriends.org) and is standard installed on C:\xampp in htdocs i have a folder called let say "x" in that folder i have index.php

<?php 
//var_dump($argv);
//echo('daaaaaaaaaaa\r\n');
//echo "\n";
$cmd='';
if (isset($argc)) {
    for ($i = 0; $i < $argc; $i++) {

        //echo "\$argv[" . $i . "] - " . $argv[$i] . "\n";
        if($i>1) $cmd.=$argv[$i];
    }
}
else {
    echo "argc and argv disabled\n";
}
/*echo('da');*/
switch ($argv[1]) {
    case 'tool1':
        echo $argv[2].":from cmd tool 1\n";
        break;
    case 'tool2':
        echo $argv[2].":from cmd tool 2\n";
        break;
    case 'run':
        //echo $argv[2].":from cmd tool 2\n";

        echo $cmd.PHP_EOL.'//';
        eval($cmd);

        break;

    case 'bench':
        ob_start();
        $start= microtime(true);
        for($i=0;$i<1000;$i++)  eval($cmd);
        $stop= microtime(true);
        ob_clean ();
        echo $cmd.PHP_EOL.'//';
        echo $stop-$start;


        break;

}

?>

in Editplus you have the menu in the top: Tools / Preferences /User tools and then Add tool .. program so now you should have in the bottom a form to fill ! put these:

Menu text: bench ( <--- write something ,i put bench)
Command: "c:\xampp\php\php.exe"
Argument: "c:\xampp\htdocs\x\index.php" bench $(CurSel)
Initial:
Action: Run as Text FIlter(Replace)

put an icon if you need so (i make some changes so the user tools appears on my editor in the front every time i open html,php,javascript,... so i click directly that why i talk about icons) .

so now with that settings you read before done just open a new php file and copy paste:

<?php 



echo 'hi from php!';



?>

now isn't need to save the file just select echo 'hi from php!'; now click on your "bench" icon on User toolbar

if everything is set fine (xampp,the settings ,and the server shouldn't to be started 'cause work by executing php script over php cli) you will have an result like this:

your selected php code will be executed by php 1000 times and you get the time lapsed

echo'hifromphp!';
//0.0016298294067383

===================== now if you change some code in index.php

case 'bench':
        ob_start();
        $start= microtime(true);
        for($i=0;$i<1000;$i++)  eval($cmd);
        $stop= microtime(true);
        ob_clean ();
        echo PHP_EOL.'//';
        echo $stop-$start;


        break;

and in Editplus the command in Run As Text Filter (insert) the reults will not affect the code line(like before)

echo 'hi from php!';
//0.0018229484558105
//0.0016310214996338
//0.0016298294067383

and for more precision ...

case 'bench':
        ob_start();
        $start= microtime(true);
        for($i=0;$i<10000;$i++) eval($cmd);
        $stop= microtime(true);
        ob_clean ();
        echo PHP_EOL.'//';

        echo number_format(($stop-$start)/10000, 8, '.', '');

        break;

so even in a html file or txt a selected line with your user toolbar active you can execute php over that selected text

In similar way you can recode that stuff or new stuff you need. This is only the simple way ,but if you know any programming in other compiled languages you can write some executables with cmd parameters .. In time others use c,c++,.. c based i use freebasic.net or purebasic.com(in case i want to hide some code i need to keep away from harmful people). GOOD LUCK !

this is only the method to use php cli to write your own regex: do not expect we will write it for you.. you could make a try yourself like a true programmer(no matter who you are: is no place for lamers)



来源:https://stackoverflow.com/questions/15398638/how-to-recode-existing-text-filter-for-regex-search-and-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!