Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notepad++ regex backreference doesn't seem to work

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.

like image 680
Anton Akopov Avatar asked Aug 23 '18 20:08

Anton Akopov


1 Answers

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.

like image 192
Wiktor Stribiżew Avatar answered Nov 06 '22 13:11

Wiktor Stribiżew