Bug when regex matches start and replacing with a blank
-
The behaviour is as expected when replacing with something, but when replacing the match is from start of input and the replacement is blank another match is allowed as if there’s a new start of input.
To replicate, use text “aaa,bbb,ccc,—”, search: “^\w+,”, replace: “” (all without quotes)
Expected result: “bbb,ccc,—”. Actual result: “—”If you replace with “#”, it works as expected yielding “bbb,ccc,—”
-
If you use “replace” it replaces just one occurrence. If you use “replace all” then it works in the way you describe as a bug. I am not sure that this a bug in Notepad++. I think it is more an issue of understanding regular expressions.
Given the line “|aaa,bbb,ccc,–” where the “|” denotes the cursor position. The first find matches “aaa,” and replaces it with nothing, leaving “|bbb,ccc,–”. The next find within the “replace all” finds “bbb,” and the replace leaves “|ccc,–”. And it repeats.
One way around this is to change the search string to be “^\w+,([^\r\n])” and the replacement to be “\1”.
-
@AdrianHHH I understand regex very, very well. It seems that, unlike every other tool/language with which I am familiar (which is quite a few), after every replacement Notepad++ “resets” the input as if the input for next match is result of the current replacement. This is non-standard behaviour.
In my example, after “aaa,” is replaced with blank, start of input is “refound” just before “bbb”. AFAIK all other tools would not match this - there is only one start of input.
-
I had to get used to Notepad++ regex behavior when I first started using it, too. If you care to read something about that, have a look at “point #3” references in this thread:
https://notepad-plus-plus.org/community/topic/137/select-by-columns