Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何为正则表达式搜索和替换重新编码现有文本筛选器?_Regex_Text_Vbscript_Replace - Fatal编程技术网

Regex 如何为正则表达式搜索和替换重新编码现有文本筛选器?

Regex 如何为正则表达式搜索和替换重新编码现有文本筛选器?,regex,text,vbscript,replace,Regex,Text,Vbscript,Replace,我能够拼凑下面的代码,以便它作为文本过滤器执行多个搜索和替换功能。代码在EditPlus文本编辑器程序中工作 我想执行相同的想法,除了正则表达式搜索和替换 在文本过滤器中编码多个正则表达式搜索和替换的正确方法是什么 在此处输入代码 Option Explicit Dim oWS, oFS Set oWS = WScript.CreateObject("WScript.Shell") Set oFS = WScript.CreateObject("Scripting.FileSystemObj

我能够拼凑下面的代码,以便它作为文本过滤器执行多个搜索和替换功能。代码在EditPlus文本编辑器程序中工作

我想执行相同的想法,除了正则表达式搜索和替换

在文本过滤器中编码多个正则表达式搜索和替换的正确方法是什么

在此处输入代码

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
选项显式
模糊的oWS,oFS
设置oWS=WScript.CreateObject(“WScript.Shell”)
Set of s=WScript.CreateObject(“Scripting.FileSystemObject”)
'----------
'脚本设置
'----------
Dim oInStream、oOutStream、sInFile、sOutFile、nTotalLines、sArg
'----------
'进程文件
'----------
如果Wscript.Arguments.Count=0,则
如果InStr(LCase(WScript.FullName),“cscript.exe”)为0,则
sInFile=“StdIn”
sOutFile=“StdOut”
设置oInStream=WScript.StdIn
设置oOutStream=WScript.StdOut
调用ProcessFile()
其他的
调用HelpMsg()
如果结束
其他的
对于Wscript.Arguments.Count-1的sArg=0
sInFile=Wscript.Arguments(sArg)
如果是IsFile(sInFile),那么
sOutFile=Left(sInFile,InStrRev(sInFile,“.”-1)和“.lst”
设置oOutStream=oFS.OpenTextFile(sOutFile,2,True,0)
设置oInStream=oFS.OpenTextFile(sInFile,1)
调用ProcessFile()
运行“NotePad.exe”&sOutFile
其他的
Wscript.Echo“未找到文件:”&sInFile,“错误”
如果结束
下一个
如果结束
调用清理(0)
'---------------------
'子程序
' ********************
'---------------------
子清理(exitCode)
设置为空=无
设置oOutStream=Nothing
设置oWS=无
设置oWS=无
WScript.Quit(exitCode)
端接头
'---------------------
子进程文件()
“oOutStream.WriteLine”
nTotalLines=SeaRep()
“oOutStream.WriteLine”
端接头
'---------------------
'---------------------
"功能",
' ********************
'---------------------
函数SeaRep()
Dim nLine、sLine、nCount、outPut1、outPut2、outPut3
n计数=0
直到oInStream.AtEndOfStream
nLine=oInStream.Line
sLine=oInStream.ReadLine
输出1=替换(sLine,“1a”,“foo”)
输出2=替换(输出1,“2b”,“foobar”)
输出3=替换(输出2,“3c”,“foosod”)
oOutStream.WriteLine(替换(outPut3,“4d”,“fooyard”))
nCount=nCount+1
环
AddLineNum=nCount
端函数
'---------------------
'---------------------
函数IsFile(fName)
如果oFS.FileExists(fName),则IsFile=True,否则IsFile=False
端函数
' ********************
'结束代码

用正则表达式替换与“普通”替换没有什么不同。您只需要准备正则表达式的附加步骤:

实际替换是这样完成的(没有搜索文本,因为该信息包含在正则表达式中):

因为您可能需要几个正则表达式,所以最好将它们的创建封装在一个函数中:

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

  Set CreateRegExp = re
End Function
然后,您的代码可能看起来像这样:

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
对不起,如果我现在看到的话。 如果您还不知道,那么可以使用PHPCLI编写过滤器代码。 如果您了解php,并且希望编写自定义EDITPLUS过滤器(就像我所做的那样): 例如,我使用xampp(apachefriends.org),标准安装在C:\xampp上 在htdocs中,我有一个名为“x”的文件夹,在这个文件夹中我有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;

}

?>
===================== 现在如果您更改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;
为了更精确

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;
case“bench”:
ob_start();
$start=microtime(真);

对于($i=0;$iDo)您想在脚本中以编程方式进行预定义替换,还是需要能够将任意替换字符串传递给它?我处理的项目需要对文本(xml、html)反复进行相同的替换我想要完成的是能够在我的IDE(EditPlus)中点击一个按钮,在一个打开的文本文件中执行大量替换.在上面的工作示例代码中,有四个非正则表达式替换。为了回答您的问题,是的,我希望以编程方式执行正则表达式S&R。-----对于整个目录,这是我发现的最有用的工具(出于我的目的)在windows环境中,是ReplaceText2.2,它有一个直观的界面,做得很好。抱歉,但我现在更困惑了。你想替换编辑器中打开的文件中的文本吗?还是想递归替换目录树中所有文本文件中的文本?在任何文本、XML和HTML文件中,还是只在一个类别中?想象你有在文本编辑器上打开一个文件。您需要运行搜索和替换10次。使用上面的代码,您按下一个按钮,所有更改都会进行。当天晚些时候,相同的场景不同的文件,相同的10个更改;您按下相同的按钮。第二天,新文件,单击一下,相同的S&R x 10!上面的代码将其作为EditP中的过滤器提供lus文本编辑器。现在,如果我能为正则表达式搜索和替换做同样的事情,那将对我很有用。(同样的一把S&R,一遍又一遍)---一次完成,只需点击一下。谢谢Ansgar。我试图采纳你的建议,但如果成功,我就做不到。我会继续调查。
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)
<?php 



echo 'hi from php!';



?>
echo'hifromphp!';
//0.0016298294067383
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;
echo 'hi from php!';
//0.0018229484558105
//0.0016310214996338
//0.0016298294067383
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;