Notepad ++ 正则表达式查找/替换使用 Look behind 不工作 [英] Notepad++ Regex Find/Replace Using Look Behind not Working
问题描述
我有以下 CSS 标记.
I have the following CSS markup.
.previous-container{
float:left;
}
.primary-commands {
float:right;
}
使用正则表达式语法搜索 (?<=[\s,;])([a-zA-Z\-]+):
它按预期突出显示 CSS 属性名称,但是,点击替换后,没有任何内容被替换.我曾尝试在替换行中使用组令牌语法,例如$[nth group]
和任何纯文字字符串替换.无论我如何尝试,它都不会用任何东西替换匹配的字符串.我使用的是记事本++ 6.7.5 版.也许我在这里遗漏了一些明显的东西?
Using the regex syntax search (?<=[\s,;])([a-zA-Z\-]+):
it highlights the CSS property name as expected, however, upon clicking replace nothing is replaced. I have tried using group token syntax in replace line e.g. $[nth group]
and any plain literal string replacement. No matter my attempts it will not replace the matched string with anything. I am using notepad++ version 6.7.5. Perhaps there is something obvious I am missing here?
推荐答案
基于对原始问题的评论,这里有一些解决我的问题的变通方法.
Based on comments to the original question here are some work-arounds that solved my problem.
选项#1
将正则表达式语句 (?<=[\s,;])
的后视部分替换为一个简单的非后视组匹配语句,例如 ([\s,;])
.这将继续将搜索结果限制为以回溯中的指定字符开头的字符串.唯一需要注意的是,在我的替换字符串中,例如$1 $2
我需要去掉不应该是替换字符串一部分的不需要的匹配字符.
Replace the lookbehind portion of the regex statement (?<=[\s,;])
with a simple non-lookbehind group matching statement such as ([\s,;])
. This will continue to limit search results to strings beginning with the specified characters in the lookbehind. The only caveat is that in my replacement string e.g. $1 $2
I would need to leave out the undesired matched characters that should not be a part of the replacement string.
选项#2
使用全部替换"按钮.与使用替换"按钮进行单个替换相比,在正则表达式语句中使用正向后视时,它将正确执行替换.
Use the "Replace All" button. It will perform replacements correctly when using a positive lookbehind in your regex statement as opposed to using the "Replace" button for single replacement.
我选择选项 #1 只是因为它实现了我的需要,同时允许我一次执行一个替换.对于较大的文档,在彻底测试我的正则表达式之前,我不想使用全部替换".
I went with Options #1 only because it achieves what I need while allowing me to still perform a single replacement at a time. With larger documents I don't want to use "Replace All" until I have thoroughly tested my regex expression.
这篇关于Notepad ++ 正则表达式查找/替换使用 Look behind 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!