多重搜索&替换 - 记事本++(正则表达式)

时间:2015-05-31 11:18:30

标签: regex replace find notepad++

我有一个单词列表,例如:

 Good -> Bad
 Sky -> Blue
 Gray -> Black

等...

在notepad ++中查找和替换的最佳原因是什么? 我试过这个:

FIND: (Good)|(Sky)|(Gray)

Replace: (?1Bad)(?2Blue)(?3Black)

但它不起作用:( 任何的想法?还是建议?

1 个答案:

答案 0 :(得分:0)

如果你在文字的最后添加这个换行符(它必须是最后一行,所以不要在最后按Enter键)

#Good:Bad#Sky:Blue#Gray:Black#

如果您使用此模式:

(Good|Blue|Black)(?=(?:.*\R)++#(?>[^#]+#)*?\1:([^#]+))|\R.++(?!\R)

有了这个替代品:

$2

模式细节:

(Good|Blue|Black)  # this part capture the word in group 1
(?=                # then we reach the last line in a lookakead
    (?:.*\R)++     # match all the lines until the last line
    #(?>[^#]+#)*?  # advance until the good value is found 
    \1             # the good value (backreference to the capture group 1)
    : ([^#]+)      # capture the replacement in group 2
)                  # close the lookbehind

|                  # OR

\R.++(?!\R)        # match the last line (to remove it)

注意:为了提高模式的效率,你可以把它放在一个非捕获组中,并在开头添加一个前瞻用所有第一个可能的字符来快速丢弃字符串中无用的位置:

(?=[GB\r\n])(?:\b(Good|Blue|Black)\b(?=(?:.*\R)++#(?>[^#]+#)*?\1:([^#]+))|\R.++(?!\R))
相关问题