Insert commas into numbers Write a program that reads a string, and inserts commas to divide large numbers in groups of 3. i.e. 10000 => 10,000. It should 'fix' incorrectly formatted numbers as described below. 


It must work for all numbers on the same line
It should not modify numbers after a decimal point. i.e. 1234.5678 => 1,234.5678
Leading zeros should be removed (or inserted where appropriate):


00000 => 0
00001234 => 1,234
.1234 => 0.1234
00.1234 => 0.1234
1000.1234 => 1,000.1234

All non-digit characters should not be altered.
It should correct, incorrectly placed "," characters only when followed before or after a digit:


10,,00 => 1,000
00,000 => 0
a,2 => a2
2,a => 2a
,a2 => ,a2
0,a2 => 0a2
,,, => ,,,
,,,. => ,,,.
,,,1 => 1
,.,,1 => 0.1

Numbers that contain multiple "." should not be treated as a number, and therefore no comma separation should be used:


1..3 => 1..3
1234.5678.9012 => 1234.5678.9012 
1234,,.5678abc.123 => 1,234.5678abc0.123
1234.,.5678abc.123 => 1234.,.5678abc0.123

Explanations to unusual cases:


,,,. => ,,,. (not a number, no special meanings to ",", ".")
,,,.0 => 0 (these characters are neighbouring a number and treated as such)
1,,, => 1
,,,1 => 1
,.,,1 => 0.1 (the "." indicates decimal number, remove incorrectly placed "," in number).
,.,,.1 => ,.,,.1 (not a number because multiple ".")
a,,,b1234 = > a,,,b1,234 (first few characters are not neighbouring a number and treated as text)



Example inputs:

10000
10,000
$1234.12
~$123456789.12345~
HELLO123456.99BYE
The_amount_€45678_is_$USD51805.84
5000000-1999999=3000001
!1234567.1234+1234567.1234=2469134.2468!
00000a00
0b0001234
.a.1234
00.1234
1000.1234
10,,00
00,000
a,2
2,a
,a2
0,a2
,,,
,,,.
,,,1
,.,,1
1..3
1234.5678.9012
1234,,.5678abc.123
1234.,.5678abc.123
,,,.
,,,.0
1,,,
,,,1
,.,,1
,.,,.1
a,,,b1234


Corresponding outputs:

10,000
10,000
$1,234.12
~$123,456,789.12345~
HELLO123,456.99BYE
The_amount_€45,678_is_$USD51,805.84
5,000,000-1,999,999=3,000,001
!1,234,567.1234+1,234,567.1234=2,469,134.2468!
0a0
0b1,234
.a0.1234
0.1234
1,000.1234
1,000
0
a2
2a
,a2
0a2
,,,
,,,.
1
0.1
1..3
1234.5678.9012 
1,234.5678abc0.123
1234.,.5678abc0.123
,,,.
0
1
1
0.1
,.,,.1 
a,,,b1,234


Code golf: shortest code wins.
Retina, 71 bytes

/[\d.,]+/_/\d/&amp;/\..*\./^&amp;(`,

^0+

^\B
0
'.&amp;`\.?0+$

/^\d+/_r`\B...
,$&amp;


Try it online! Link includes test cases. Explanation:

/[\d.,]+/_


Find all runs of digits, .s and ,s.

/\d/&amp;


Ignore those with no digits.

/\..*\./^&amp;


Ignore those with two .s.

(`


Process the rest of the script on each remaining match.

,


Delete all commas.

^0+


Delete all leading zeros.

^\B
0


Insert a leading zero if the number does not begin with a digit.

'.&amp;`


If the number contains a ....

\.?0+$


... delete trailing zeros and any preceding ..

/^\d+/_


On the part of the number before the ....

r`\B...
,$&amp;


Match groups of three starting from the end, but leave at least one digit at the beginning, and insert commas before each match.