使用工具替换脚本中的特定代码

时间:2013-10-29 09:50:32

标签: batch-file notepad++

我有一个包含随机代码的脚本,但我在notepad ++或批处理文件或任何可以替换sepcifque代码的工具中寻找方法,这是一个例子:

Random
If this equal that then you soulAd do this and do that therefore..
the code should be executed immediatly
--stackb

select * from user_error where object_name = name
select * from user_error where table= randomly

case 1 a = b else c=a
--stacke
Begin with the structure of the data and divide the codes
end with what you know

我想替换注释堆栈b和堆栈a之间的单词,结果将如下所示

Random
If this equal that then you sould do this and do that therefore..
the code should be executed immediatly
--stackb

The codes here has been replaced,
can you do that ?

case 1 a = b else c=a
--stacke
Begin with the structure of the data and divide the codes
end with what you know

批处理文件或记事本++中是否有代码可以实现我的结果?

1 个答案:

答案 0 :(得分:2)

在Notepad ++中,转到Search > Replace菜单(快捷键 CTRL + H )并执行以下操作:

  1. 找到什么(见下面的解释):

    (\-\-stackb.*?)select.+?$\r?\nselect.+?$(\r?\n.*?\-\-stacke)
    
  2. 替换:

    $1replaced text$2
    
  3. 选择单选按钮“正则表达式”&选中复选框“。匹配换行符”

  4. 然后按Replace All

  5. 这将转换以下文件:

    Random
    If this equal that then you soulAd do this and do that therefore..
    the code should be executed immediatly
    --stackb
    
    select * from user_error where object_name = name
    select * from user_error where table= randomly
    
    case 1 a = b else c=a
    --stacke
    Begin with the structure of the data and divide the codes
    end with what you know
    

    要:

    Random
    If this equal that then you soulAd do this and do that therefore..
    the code should be executed immediatly
    --stackb
    
    replaced text
    
    case 1 a = b else c=a
    --stacke
    Begin with the structure of the data and divide the codes
    end with what you know
    

    正则表达式解释:

    • \-\-stackb 匹配字符串--stackb。这里没什么特别的,除了逃避特殊字符的反斜杠。这意味着-将被解释为文字-,而不是正则表达式特殊字符
    • .*? .匹配任何字符,加上新线,因为我们激活了选项“.matre newline”。星号*量词匹配0次或更多次。所以.*表示匹配任何字符或换行符0次或更多次。当量词后跟一个问号?时,它会使量词非贪婪,这是一个更高级的主题,但简单来说,就像对量词试图满足一样您可以使用.
    • 的最小数量
    • (\-\-stackb.*?) 现在您已了解正则表达式的含义,您可以添加一个括号以捕获匹配结果。您可以使用特殊变量$1(或\1它是相同的)来访问结果。正如您所见,我们正在替换中使用它。
    • select.+?$\r?\n 此处唯一的新内容是$行尾和特殊字符\r相匹配(回车),\n(换行符),用于查找换行符。请注意,\r之后是?量词,这意味着匹配1或0次