Reputation: 4614
My definition of Custom Decimal => The number can be represented as (p,s) where p is the precision and s is the scale and then describe everything with respect to precision and scale. Use Custom Decimal if you need to store decimals that have more than eight digits in the fractional part. Can have a maximum scale of 29 and a maximum precision of 30. The non-fractional part of the number can have a maximum of 29 digits.
P and S may vary...
Assume P= 18 S=12 i.e/ (P,S) = (18,12)
intLength = 12
fractionalLength=18
Piece of code
String regExprCD = "^-?\\d{1,"+intLength+"}(?>\\.\\d{0,"+fractionalLength+"})?$";
info("regExprCD = "+regExprCD );
if (value.matches(regExprCD ) || value.isEmpty()){
Window.alert("Valid CUSTOM_DECIMAL value");
}else{
Window.alert("Valid CUSTOM_DECIMAL value");
}
I tried following regular Expression but error comes on page "Syntax error in regular expression"
^-?\\d{1,"+intLength+"}(?>\\.\\d{0,"+fractionalLength+"})?$
Which is perfect regular expression to allow following cases :
Regular expression should accept following Custom Decimal:
123456789012345678
1234567.1234567890
1.123456789012
1234567890.1234567
12345678901234567.
12345.
Regular expression should not accept following Custom Decimal:
12345678901234567890 : Reason P > 18 :: P should not be greater than 18
1234567.1234567890123 : Reason s>12 : :: S should not be greater than 12
.1 or .1232 :: Invalid
I used ^-?\\d{1,"+intLength+"}(?>\\.\\d{0,"+fractionalLength+"})?$
this regular expression.
This regular expression working fine in Sample java program. Not working in GWT
Throwing error on web page (developed in GWT) :: "Syntax error in regular expression"
what should be data type of intLength and fractionalLength ? int or string ?
Which is perfect regular expression ?
Any help or guidance in this matter would be appreciated.
Upvotes: 2
Views: 1759
Reputation: 1
Try : ^[\d]{1,3}[.]{1}[\d]{1,1}|^[\d]{1,3}
public class RegexTestStrings {
public static void main(String[] args) {
String[] EXAMPLE_TEST = {"123", "0", "123.4", ".1", "123.", "12a", "a12"};
int P= 4, S=1;
for(int i = 0; i< EXAMPLE_TEST.length; i++){
System.out.println(EXAMPLE_TEST[i]+" -- "+EXAMPLE_TEST[i].matches("^[\\d]{1,"+(P-S)+"}[.]{1}[\\d]{1,"+(S)+"}|^[\\d]{1,"+(P-1)+"}"));
}
}
}
Result :
123 -- true 0 -- true 123.4 -- true .1 -- false 123. -- false 12a -- false a12 -- false
Upvotes: 0
Reputation: 88707
You could try this expression: ^-?\d{0,x}(?>\.\d{0,y})?$
which should match any number starting with an optional minus, then up to x
digits, followed by a dot and up to y
digits.
x
and y
would be your intLength
and fractionalLength
.
If you want to make the integer or fraction part a requirement (such that .1
or 1.
don't match), just replace the zero in the number interval with a 1, i.e. ^-?\d{1,x}(?>\.\d{1,y})?$
.
Update:
Here's a SSCCE with fixed lengths:
String expression = "^-?\\d{1,18}(?>\\.\\d{0,12})?$";
//prints true
System.out.println("12345678".matches(expression));
//prints true
System.out.println("1234.5678".matches(expression));
//prints false -> integer part too long
System.out.println("1234567890123456789.5678".matches(expression));
//prints false -> fraction part too long
System.out.println("1234.1234567890123".matches(expression));
//prints false -> integer part missing
System.out.println(".5678".matches(expression));
Upvotes: 2