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

时间:2021-08-11 02:08:25

I have a script that contain random codes but I am searching for a way in notepad ++ or for a batch-file or any tool that can replace sepcifque codes, here is an example:

我有一个包含随机代码的脚本,但我在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

I want to replace the words between the comments stack b and stack a so the result will be like below

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

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

Is there a code in batch file or note pad ++ where I can acheive my result?

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

1 个解决方案

#1


2  

In Notepad++, go to Search > Replace menu (shortcut CTRL+H) and do the following:

在Notepad ++中,转到“搜索”>“替换”菜单(快捷键CTRL + H)并执行以下操作:

  1. Find what (see below for explanation):

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

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

    $1replaced text$2
    
  3. 替换:$ 1替换文本$ 2

  4. Select radio button "Regular Expression" & select checkbox ". matches newline"

    选择单选按钮“正则表达式”并选择复选框“。匹配换行符”

  5. Then press Replace All.

    然后按全部替换。

This will transform the following file:

这将转换以下文件:

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

To:

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

Regex explanation:

  • \-\-stackb matches the string --stackb. Nothing special here, except of the backslashes which escapes special characters. This means that - will be interpreted as a literal - and not as a regex special character
  • \ - \ - stackb匹配字符串--stackb。这里没什么特别的,除了逃避特殊字符的反斜杠。这意味着 - 将被解释为文字 - 而不是正则表达式特殊字符

  • .*? the dot . matches any character, plus the newline since we activated the option ". matches newline". The asterisk * is a quantifier for matching 0 or more times. So .* means match any character or newlines 0 or more times. When a quantifier is followed by a questionmark ? it makes the quantifier non-greedy, which is a more advanced topic but in simple words it's like saying to the quantifier try to satisfy yourself with the possible minimum quantity of .
  • 。*?点。匹配任何字符,加上换行符,因为我们激活了选项“。匹配换行符”。星号*是匹配0次或更多次的量词。所以。*表示匹配任何字符或换行符0次或更多次。当量词后跟一个问号?它使量词非贪婪,这是一个更高级的主题,但简单来说,就像对量词的说法试图用可能的最小数量来满足自己。

  • (\-\-stackb.*?) now that you have understood the meaning of the regex, you can add a parenthesis in order to capture the matching result. You can access the result using the special variable $1 (or \1 it's the same). We are using it in the replacement as you can see.
  • (\ - \ - stackb。*?)现在您已经理解了正则表达式的含义,您可以添加一个括号以捕获匹配结果。您可以使用特殊变量$ 1(或\ 1,它是相同的)访问结果。我们正在替换中使用它,如您所见。

  • select.+?$\r?\n the only new stuff here, is the $ which matches the end of line and the special characters \r (carriage return), \n (newline) which are used to find a line break. Note that \r is followed by the ? quantifier which means match 1 or 0 times.
  • 选择。+?$ \ r?\ n这里唯一的新东西是$匹配行尾和特殊字符\ r \ n(回车),\ n(换行符),用于查找换行符。注意\ r \ n后跟?量词,表示匹配1或0次。

#1


2  

In Notepad++, go to Search > Replace menu (shortcut CTRL+H) and do the following:

在Notepad ++中,转到“搜索”>“替换”菜单(快捷键CTRL + H)并执行以下操作:

  1. Find what (see below for explanation):

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

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

    $1replaced text$2
    
  3. 替换:$ 1替换文本$ 2

  4. Select radio button "Regular Expression" & select checkbox ". matches newline"

    选择单选按钮“正则表达式”并选择复选框“。匹配换行符”

  5. Then press Replace All.

    然后按全部替换。

This will transform the following file:

这将转换以下文件:

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

To:

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

Regex explanation:

  • \-\-stackb matches the string --stackb. Nothing special here, except of the backslashes which escapes special characters. This means that - will be interpreted as a literal - and not as a regex special character
  • \ - \ - stackb匹配字符串--stackb。这里没什么特别的,除了逃避特殊字符的反斜杠。这意味着 - 将被解释为文字 - 而不是正则表达式特殊字符

  • .*? the dot . matches any character, plus the newline since we activated the option ". matches newline". The asterisk * is a quantifier for matching 0 or more times. So .* means match any character or newlines 0 or more times. When a quantifier is followed by a questionmark ? it makes the quantifier non-greedy, which is a more advanced topic but in simple words it's like saying to the quantifier try to satisfy yourself with the possible minimum quantity of .
  • 。*?点。匹配任何字符,加上换行符,因为我们激活了选项“。匹配换行符”。星号*是匹配0次或更多次的量词。所以。*表示匹配任何字符或换行符0次或更多次。当量词后跟一个问号?它使量词非贪婪,这是一个更高级的主题,但简单来说,就像对量词的说法试图用可能的最小数量来满足自己。

  • (\-\-stackb.*?) now that you have understood the meaning of the regex, you can add a parenthesis in order to capture the matching result. You can access the result using the special variable $1 (or \1 it's the same). We are using it in the replacement as you can see.
  • (\ - \ - stackb。*?)现在您已经理解了正则表达式的含义,您可以添加一个括号以捕获匹配结果。您可以使用特殊变量$ 1(或\ 1,它是相同的)访问结果。我们正在替换中使用它,如您所见。

  • select.+?$\r?\n the only new stuff here, is the $ which matches the end of line and the special characters \r (carriage return), \n (newline) which are used to find a line break. Note that \r is followed by the ? quantifier which means match 1 or 0 times.
  • 选择。+?$ \ r?\ n这里唯一的新东西是$匹配行尾和特殊字符\ r \ n(回车),\ n(换行符),用于查找换行符。注意\ r \ n后跟?量词,表示匹配1或0次。

相关文章