Notepad++ 如果某一行包含一个单词,则用短语替换下面一定距离处的行

Notepad++ 如果某一行包含一个单词,则用短语替换下面一定距离处的行

我很确定这是可能的,并且我认为它涉及正则表达式,但我不知道正则表达式格式如何工作,而且我还无法在任何地方找到我需要的表达式。

基本上,我有一个文件,想要找到包含单词 juice 的每一行,然后修改第 10 行和第 34 个字符,将现有数字(几乎全部以 0.0 开头)替换为 1。

答案1

我先举一个小例子,这样答案就更容易解释了。如果你的问题是找到第 3 个字符,并用“1”替换第 9 个字符:

找什么:(.*juice.*\r\n)(.*\r\n.*\r\n)(.{8})(.)

用。。。来代替:$1$2${3}1

搜索模式:正则表达式

解释

Query:
()   Defines register in regex query. Later to be referenced as ${number}
.*   Defines 0 or more of any character
\r\n Defines a newline
.{8} Defines any character 0 or one time, {8} times
(.)  Defines one character, puts it in its own register 
     In this example, it is register $4, and unused in the replacement

Replace:
$1   Recall register 1 (line containing the word juice)
$2   Recall register 2 (multiple newlines to get us to the third row)
${3} Same meaning as $3, with the "3" in brackets so that it doesn't appear 
     as $31.
$3   Recall register 3 (text before the 9th character)
$4   Contains the contents of register 4. Not recalled, so this text is "replaced" 
     when the full substitution occurs.
1    Literal output the number 1.

对您的问题的字面回答将Find what用以下内容替换字段的内容:

(.*juice.*\r\n)(.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n)(.{33})(.)

我建议学习正则表达式。这不是 NotePad++ 特定的资源,但它并不像我读过的一些正则表达式教程那么枯燥:http://www.grymoire.com/Unix/Regular.html

相关内容