包含由逗号分隔的小数的字符串的Java正则表达式

包含由逗号分隔的小数的字符串的Java正则表达式,java,regex,Java,Regex,我正在制作一个Java应用程序,其中用户输入一个以逗号分隔的小数点,如:-343.123、0.32或3,-321用于纬度和经度,如: 纬度,经度,其中纬度和经度都是double 有效字符串:43,-23.123或32.312,32.33 我尝试过这个正则表达式,但它不起作用: str.matches("^[-0-9.0-9][,][-0-9.0-9]$") 不确定哪个lat或long对负值有效,但 这是一个不带指数的双精度正则表达式 ^\s*([+-]?(?:\d+(?:\。\d*)?\124

我正在制作一个Java应用程序,其中用户输入一个以逗号分隔的小数点,如:-343.123、0.32或3,-321用于纬度和经度,如: 纬度,经度,其中纬度和经度都是
double

有效字符串:
43,-23.123
32.312,32.33

我尝试过这个正则表达式,但它不起作用:

str.matches("^[-0-9.0-9][,][-0-9.0-9]$")

不确定哪个lat或long对负值有效,但
这是一个不带指数的双精度正则表达式

^\s*([+-]?(?:\d+(?:\。\d*)?\124;\。\ d+)\s*,\s*([+-](?:\。\d*)?\124;\。\ d+))\s*$

扩大

 ^ 
 \s* 
 (                             # (1 start)
      [+-]? 
      (?:
           \d+ 
           (?: \. \d* )?
        |  \. \d+ 
      )
 )                             # (1 end)
 \s* , \s* 
 (                             # (2 start)
      [+-]? 
      (?:
           \d+ 
           (?: \. \d* )?
        |  \. \d+ 
      )
 )                             # (2 end)
 \s* 
 $
草率匹配(允许多个和.字符):

更确切地说:

"^[-+]?[0-9]+\.?[0-9]*,[-+]?[0-9]+\.?[0-9]*$"
特殊字符

? : zero or more matches of preceding
+ : one or more matches of preceding (except when in character set)
* : zero or more matches of preceding

希望有帮助。如果您希望lat/long不带整数(不太可能),请将数字整数部分的匹配字符更改为*。

您希望哪种输出?
? : zero or more matches of preceding
+ : one or more matches of preceding (except when in character set)
* : zero or more matches of preceding