Context: I have found a solution for matching three Integers seperated by the "$" character as seen below:
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]*"));
Problem: What I want to achieve is:
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
Essentially what I want is a Regex matching 3 numbers separated by the "$" character, with each number being a positive non-zero double if parsed by the Double.parseDouble()
method.
If I've correctly understood, I think this will work:
^(?!\\$)((^|\\$)(?=[^$]*[1-9])(\\d+(\\.\\d*)?|(\\.\\d*)?\\d+)){3}$
Follows an explenation:
^(?!\\$)
: the begin of the match must not be followed by a '$'{3}
: the following pattern has to be repeated 3 times
(^|\\$)
: the pattern starts or with the begin of the string or with a '$' (not both, for what stated above)(?=[^$]*[1-9])
: before the next eventual '$' there must be a non-0 digit(\\d+(\\.\\d*)?|(\\.\\d*)?\\d+)
: the allowed format for the number is either \d+(\.\d*)?
or (\.\d*)?\d+
$
: endSee here for a demo
An extended expression (if you don't trust the repeat trick) is:
^(?=[^$]*[1-9])(\\d+(\\.\\d*)?|(\\.\\d*)?\\d+)\\$(?=[^$]*[1-9])(\\d+(\\.\\d*)?|(\\.\\d*)?\\d+)\\$(?=[^$]*[1-9])(\\d+(\\.\\d*)?|(\\.\\d*)?\\d+)$