Notepad++ 正则表达式反向引用似乎不起作用 [英] Notepad++ regex backreference doesn't seem to work

查看:111
本文介绍了Notepad++ 正则表达式反向引用似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在不以结尾的每一行末尾添加;:, {, }).

I need to add ; at the end of every line that does not end with :, {, } or ).

我在 Notepad++ 中使用这个:

I'm using this in Notepad++:

  • 搜索:[^:\{\}\)]$
  • 替换为:\1;

它找到的字符串没问题,但它用 ; 替换了在行尾找到的最后一个字符,而不是将其添加到其中.我尝试了 $1 而不是 \1 但它没有改变任何东西 - 找到的文本仍然被删除.

It finds the strings all right but it replaces the last character found before the end of line with ; instead of adding it to it. I tried $1 instead of \1 but it didn't change anything — the found text still gets deleted.

推荐答案

你的模式没有捕获组,因此 \1 是一个空字符串.改用 $0 来引用整个匹配项:

Your pattern has no capturing group, hence \1 is an empty string. Use $0 instead to refer to the whole match:

查找内容:[^:{})]$
替换为:$0;

然而,在某些边缘情况下它可能会失败([^:{})]$ 模式匹配除 :{ 之外的任何字符code>、}),因此在行结束前至少需要 1 个字符),也许,您最好在此处使用负向后视:

However, it might fail in some edge cases (the [^:{})]$ pattern matches any char other than :, {, } and ), so requires at least 1 char before a line end), perhaps, you should better use a negative lookbehind here:

查找内容:$(?<![:{})])
替换为:;

$(?<![:{})]) 模式匹配行尾(使用 $),然后是 (?<![:{})]) 负向后视确保没有 :{}>) 紧接在当前位置的左侧.

The $(?<![:{})]) pattern matches the end of line (with $) and then the (?<![:{})]) negative lookbehind makes sure that there is no :, {, } or ) immediately to the left of the current location.

这篇关于Notepad++ 正则表达式反向引用似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆