用于匹配由“$”字符分隔的正非零双精度字符串的正则表达式

Regex for matching a string of positive non-zero doubles seperated by the "$" character

上下文:我找到了匹配三个由“$”字符分隔的整数的解决方案,如下所示:

String toMatch = "123"; //Returns true
String toMatch2 = "123[=10=]"; //Returns false
String toMatch3 = "0";  //Returns false
String toMatch4 = "123[=10=]"; //Returns false
System.out.println(toMatch.matches("\d+.*\d+.*\d+") && !toMatch.matches([^0-9]*0[^0-9]*"));

问题:我要实现的是:

String toMatch = "123.43$.033.0"; //Returns true
String toMatch2 = "123[=11=]0000";   //Returns false
String toMatch3 = "0.0000";    //Returns false
String toMatch4 = "123$.000";     //Returns false

本质上我想要的是一个正则表达式匹配 3 个由“$”字符分隔的数字,如果通过 Double.parseDouble() 方法解析,每个数字都是正的非零双精度数。

如果我理解正确,我认为这会起作用:

^(?!\$)((^|\$)(?=[^$]*[1-9])(\d+(\.\d*)?|(\.\d*)?\d+)){3}$

遵循解释:

  • ^(?!\$): 匹配开始后不能跟'$'
  • {3}:下面的模式必须重复3次
    • (^|\$):模式以字符串开头或以“$”开头(不是两者,如上所述)
    • (?=[^$]*[1-9]): 在下一个最终的 '$' 之前必须有一个非 0 数字
    • (\d+(\.\d*)?|(\.\d*)?\d+):数字的允许格式为 \d+(\.\d*)?(\.\d*)?\d+
  • $: 结束

有关演示,请参阅 here

扩展表达式(如果您不相信重复技巧)是:

^(?=[^$]*[1-9])(\d+(\.\d*)?|(\.\d*)?\d+)\$(?=[^$]*[1-9])(\d+(\.\d*)?|(\.\d*)?\d+)\$(?=[^$]*[1-9])(\d+(\.\d*)?|(\.\d*)?\d+)$