Java Regex Number Validate isNumeric(String as_argument)

Here you can find the source of isNumeric(String as_argument)

Description

check if given argument is numeric.

License

Open Source License

Parameter

Parameter Description
as_argument given argument

Return

if given argument is numeric,return true, otherwise false

Declaration

public static final boolean isNumeric(String as_argument) 

Method Source Code

//package com.java2s;

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

public class Main {
    /** only contains digital chars. */
    public static final String sNUMERIC_PATTERNS[] = { "^(\\d)*$" };

    /**//from w  ww  . ja v a  2 s . co m
     * check if given argument is numeric.
     * 
     * @param as_argument
     *            given argument
     * @return if given argument is numeric,return true, otherwise false
     */
    public static final boolean isNumeric(String as_argument) {

        boolean return_value = isOneMatched(sNUMERIC_PATTERNS, as_argument);

        return return_value;
    }

    /**
     * check if the "as_searchedChars" argument matches one of all regular
     * expression "as_regExp_patterns".
     * 
     * @param as_regExp_patterns
     *            all regular expressions
     * @param as_searchedChars
     *            chars to be searched.
     * @return if the "as_searchedChars" argument matches one of all regular
     *         expression "as_regExp_patterns", return true, otherwise false
     */
    public static final boolean isOneMatched(String[] as_regExp_patterns, String as_searchedChars) {
        boolean return_value = false;

        Pattern apattern[] = new Pattern[as_regExp_patterns.length];
        Matcher amatcher[] = new Matcher[as_regExp_patterns.length];
        for (int i = 0; i < as_regExp_patterns.length; i++) {
            apattern[i] = Pattern.compile(as_regExp_patterns[i]);
            amatcher[i] = apattern[i].matcher(as_searchedChars);
        }

        for (int i = 0; i < amatcher.length; i++) {
            if (amatcher[i].find()) {
                return_value = true;
                break;
            }
        }

        return return_value;
    }
}

Related

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