Java Regex Number Validate isNumber(String value)

Here you can find the source of isNumber(String value)

Description

is Number

License

Open Source License

Declaration

public static boolean isNumber(String value) 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.util.regex.Pattern;

public class Main {
    private static final Pattern patternInteger = Pattern.compile("^(\\+|-)?\\d+$");
    private static final Pattern patternDouble = Pattern.compile("^[-+]?"// Positive/Negative sign
            + "("// BEGIN Decimal part
            + "[0-9]+([,\\.][0-9]+)?|"// Alternative I (w/o grouped integer part)
            + "(" // BEGIN Alternative II (with grouped integer part)
            + "[0-9]{1,3}" // starting digits
            + "(" // BEGIN grouped part
            + "((,[0-9]{3})*"// US integer part
            + "(\\.[0-9]+)?"// US float part
            + "|" // OR
            + "((\\.[0-9]{3})*|([ \u00A0\u2007\u202F][0-9]{3})*)"// EU integer part
            + "(,[0-9]+)?)"// EU float part
            + ")"// END grouped part
            + ")" // END Alternative II
            + ")" // END Decimal part
            + "([ ]?[eE][-+]?[0-9]+)?$");

    public static boolean isNumber(String value) {
        return isDouble(value) || isInteger(value);

    }/*  ww w. j ava2  s  .  co m*/

    /**
     * Detect if the given value is a double type.
     * 
     * <p>
     * Note:<br>
     * 1. This method support only English locale.<br>
     * e.g. {@code TypeInferenceUtils.isDouble("3.4")} returns {@code true}.<br>
     * e.g. {@code TypeInferenceUtils.isDouble("3,4")} returns {@code false}.<br>
     * 2. Exponential notation can be detected as a valid double.<br>
     * e.g. {@code TypeInferenceUtils.isDouble("1.0E+4")} returns {@code true}.<br>
     * e.g. {@code TypeInferenceUtils.isDouble("1.0e-4")} returns {@code true}.<br>
     * e.g. {@code TypeInferenceUtils.isDouble("1.0e-04")} returns {@code true}.<br>
     * 3. Numbers marked with a type is invalid.<br>
     * e.g. {@code TypeInferenceUtils.isDouble("3.4d")} returns {@code false}.<br>
     * e.g. {@code TypeInferenceUtils.isDouble("123L")} returns {@code false}.<br>
     * 4. White space is invalid.<br>
     * e.g. {@code TypeInferenceUtils.isDouble(" 3.4")} returns {@code false}.<br>
     * e.g. {@code TypeInferenceUtils.isDouble("3.4 ")} returns {@code false}.<br>
     * 5. "." is not obligatory.<br>
     * e.g. {@code TypeInferenceUtils.isDouble("100")} returns {@code true}.
     * <P>
     * 
     * @param value the value to be detected.
     * @return true if the value is a double type, false otherwise.
     */
    public static boolean isDouble(String value) {
        if (!isEmpty(value) && patternDouble.matcher(value).matches()) {
            return true;
        }
        return false;
    }

    /**
     * Detect if the given value is a integer type.
     * 
     * @param value the value to be detected.
     * @return true if the value is a integer type, false otherwise.
     */
    public static boolean isInteger(String value) {
        if (!isEmpty(value) && patternInteger.matcher(value).matches()) {
            return true;
        }
        return false;
    }

    /**
     * Detect if the given value is blank or null.
     * 
     * @param value the value to be detected.
     * @return true if the value is blank or null, false otherwise.
     */
    public static boolean isEmpty(String value) {
        return value == null || value.trim().length() == 0;
    }
}

Related

  1. isNumber(String str)
  2. isNumber(String str)
  3. isNumber(String str)
  4. isNumber(String str, String sign)
  5. isNumber(String value)
  6. isNumber(String value)
  7. isNumber_Lowerletter_Underline(String str)
  8. isNumberForLength(String numStr, int length)
  9. isNumberLiteral(String s)