Q:

RegEx or PowerShell to remove repeat characters in sequence

Writing a script to convert a Windows host file into a CSV.  Used RegEx to get to this stage:
1.1.1.1,,server1,,,
2.2.2.2,,server2
3.3.3.3,,server3
4.5.6.7,,server4,,server5,,server6,,server7,,
8.8.8.8,,server8
9.9.9.9,server9

I need some RegEx that can remove the duplicate commas (in sequence) so it would look like this:
1.1.1.1,server1,
2.2.2.2,server2
3.3.3.3,server3
4.5.6.7,server4,server5,server6,server7,
8.8.8.8,server8
9.9.9.9,server9

Will also need to remove the comma at the end of each line (if there is one) but think this will be simpler to do.

A:

The regex for your first task of removing duplicate commas was already provided in the comments above, but if you also want to remove trailing commas at the end of the line, you can use this to solve both problems at once:
(?m),(?=,|$)

Explanation:
(?m)  # turn on multiline mode ($ matches end-of-line, not just end-of-string)
,     # Match a comma
(?=   # only if followed by
 ,    # another comma
|     # or
 $    # the end of the string.
)     # End of lookahead assertion

Test it live on regex101.com.

