上下文:我找到了一个解决方案,用于匹配三个由" $"字符分开的整数,如下所示:
String toMatch = "123$53$12"; //Returns true
String toMatch2 = "123$0$12"; //Returns false
String toMatch3 = "0$53$12"; //Returns false
String toMatch4 = "123$53$0"; //Returns false
System.out.println(toMatch.matches("\d+.*\d+.*\d+") && !toMatch.matches([^0-9]*0[^0-9]*"));
问题:我想实现的目标是:
String toMatch = "123.43$.03$123.0"; //Returns true
String toMatch2 = "123$000000$12"; //Returns false
String toMatch3 = "0.0000$53$12"; //Returns false
String toMatch4 = "123$53$.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+
-
-
$
:end
请参阅此处的演示
扩展表达式(如果您不信任重复技巧(是:
^(?=[^$]*[1-9])(\d+(\.\d*)?|(\.\d*)?\d+)\$(?=[^$]*[1-9])(\d+(\.\d*)?|(\.\d*)?\d+)\$(?=[^$]*[1-9])(\d+(\.\d*)?|(\.\d*)?\d+)$