-1
("^\d{1,15}(\.\d{1,2})?$") 

This is the regex i'm trying to use but java gives syntax error.

^(?!(?:0|0\.0|0\.00)$)[+]?\d+(\.\d|\.\d[0-9])?$

This one works well for numbers like 00.00, 124.03, 0.13 but not for 0.0 and 0.

please modify the regex so that it accepts the following kind of numbers :

123456.00,
12415366.88,
0.23,
0,
0.00,
0.0,
432547,

i.e. only postive numbers which include zero and upto 2 places after decimal

Pandiyan Cool
  • 6,381
  • 8
  • 51
  • 87
NGB
  • 97
  • 1
  • 3
  • 13
  • Possible duplicate of [This](http://stackoverflow.com/questions/8609714/regex-greater-than-zero-with-2-decimal-places) question except you have to do some modification for 0. – Vimal Bera Aug 23 '13 at 04:57
  • 1
    The compilation error in your first example can be fixed by properly escaping the backslashes in `\d` to `\\d`. The second regex specifically excludes `0`, `0.0`, and `0.00`. You should attempt to understand your code before asking questions about it here. – FThompson Aug 23 '13 at 04:57
  • 1
    why do you need to use regex? try something like this http://stackoverflow.com/questions/50532/how-do-i-format-a-number-in-java – Sean F Aug 23 '13 at 04:58

3 Answers3

1

public class RegexTest {

public static void main(String[] args) {
    String regexExpression = "([0-9]+[.]?|[0-9]*[.][0-9]{0,2})";

    // True examples
    System.out.println("123456.00".matches(regexExpression));

    System.out.println("12415366.88".matches(regexExpression));

    System.out.println("0".matches(regexExpression));

    System.out.println("0.0".matches(regexExpression));
    System.out.println("0.00".matches(regexExpression));
    System.out.println("432547".matches(regexExpression));

    System.out.println("00.00".matches(regexExpression));

    System.out.println("124.03".matches(regexExpression));

    // False examples
    System.out.println("124.033".matches(regexExpression));
    System.out.println("-124.03".matches(regexExpression));
}

}

Mengjun
  • 3,159
  • 1
  • 15
  • 21
1

Your first regex is the best, but remember that in java you must use a double backslash to code a literal backslash, so:

str.matches("\\d{1,15}(\\.\\d{1,2})?") 

Note that with matches(), you don't need the leading ^ or trailing $, because the expression must match the whole string anyway to return true.

The syntax error was probably because \d is not a valid escape sequence, whereas \n etc is valid.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

I don't know why regex here. You can try in this way

  String num="253.65";
    try{
        double d=Double.parseDouble(num);
        if(d==0.0){
            System.out.println("valid");
        }else if(d>0&&(num.split("\\.")[1].length()==2)){
            System.out.println(num+" is valid");
        }else{
            System.out.println(num+" is invalid");
        }
    } catch (NumberFormatException e){
        System.out.println(num+"is not a valid number");
    }

Live Demo

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115