[^,]
matches one character, that can be anything except a comma. For example: M
.
[^,]+
matches 1 or more times any character except a comma. For example: Milind
([^,]+)
same, but capture the result, so it can be referenced later with \1
.
(,\1)+
matches a comma followed by the previous capture, one or more times, and capture the result as \2
. If \1
is Milind
, it can match: ,Milind
, or ,Milind,Milind
or ,Milind,Milind,Milind
, etc.
(,|$)
matches either a comma or the end of the line, and captures it as \3
.
\1\3
This is the replacement pattern: we are only keeping \1
and \3
, so everything matched in capture \2
is effectively removed.
The second statement is showing a small difference that introduces a bug:
(,\1+)
matches a comma followed by one or more occurences of the previous capture, as in: ,Milind
, or ,MilindMilind
or ,MilindMilindMilind
. As a result, it fails to remove several comma separated occurences.