Java Regular Expression match digits only

Description

Java Regular Expression match digits only


public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "123123";
        System.out.println(isNumber(str));
    }// www .  jav a2  s .  com

    public static Boolean isNumber(String str) {
        Boolean isNumber = false;
        String expr = "^[0-9]+$";
        if (str.matches(expr)) {
            isNumber = true;
        }
        return isNumber;
    }
}

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "";
        System.out.println(isNumeric(str));
    }//www  .  j  a va  2 s  . c om

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (isNum.matches())
            return true;
        else {
            return false;
        }
    }
}



PreviousNext

Related