Notepad++ regex backreference doesn't seem to work

余生颓废 提交于 2020-01-30 04:51:34

问题


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

I'm using this in Notepad++:

  • Search for: [^:\{\}\)]$
  • Replace with: \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:


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

Find What: [^:{})]$
Replace with: $0;

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:

Find What: $(?<![:{})])
Replace with: ;

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.



来源:https://stackoverflow.com/questions/51993893/notepad-regex-backreference-doesnt-seem-to-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!