There is a string like

123.0,234,5677, "34.56", 34.56,67.89

That is, all normal numbers have a period separator and are separated by commas, but some have a comma separator and for separation are placed in quotation marks, and a space as an order separator ("1,000.00") is possible inside.

How do I write Regexp which replaces all the wrong numbers with the correct ones, that is, with a dot with a separator?

  • This is a clear pattern, that if the separator is a fractional and integer part of a comma, then the number in quotes? Or is it possible that inside the quotes a dot separator? "12.34" - ReinRaus 8:49 PM
  • I think that is impossible at the moment. - KutaBeach

2 answers 2

Try this:

 str.replaceAll.(" ", "").replaceAll("\"(-?\\d+)(?:,(\\d*))?\"", "$1.$2") 

The possibility of numbers in which the integer part is zero like ",123" not taken into account

    JS, rewrite in Java should be easy)

     in_re = /(?=(?:(?:"[^"]+){2})*)"(\d*)[,.]*(\d*)?"/gm; out_re = '$1.$2\0'; in_str = '123.0,234,5677,"34,56",34.56,67.89,123.0,234,5677,"34,56",34.56,67.89'; out_str = in_str.replace(in_re, out_re); 

    Or so

     in_re = /(?:"(\d*)[,.]?(\d*)"|(\d*)\.?(\d*))(,|$)/gm; out_re = '$1$3.$2$40$5'; in_str = '123.0,234,5677,"34,56",34.56,67.89,123.0,234,5677,"34,56",34.56,67.89'; out_str = in_str.replace(in_re, out_re); 
    • But what about spaces as a separator of orders? - ReinRaus
    • For spaces, use your code)) str.replaceAll. ("", "") - timka_s