Java Regex Number Validate isNumberLiteral(String s)

Here you can find the source of isNumberLiteral(String s)

Description

Check whether a string resembles a valid number literal.

License

Apache License

Parameter

Parameter Description
s a parameter

Declaration

public static boolean isNumberLiteral(String s) 

Method Source Code

//package com.java2s;
/**//from w ww  .j a v a2s.c om
 * Copyright 2012 Tobias Gierke <tobias.gierke@code-sourcery.de>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.regex.Pattern;

public class Main {
    private static final Pattern HEX_NUMBER = Pattern.compile("^0x([0-9a-fA-F]+)$");
    private static final Pattern BIN_NUMBER = Pattern.compile("^b([0-1]+)$");
    private static final Pattern DEC_NUMBER = Pattern.compile("^([\\-]{0,1}[0-9]+)$");

    /**
     * Check whether a string resembles a valid number literal.
     * 
     * @param s
     * @return
     */
    public static boolean isNumberLiteral(String s) {
        return isHexLiteral(s) || isBinaryLiteral(s) || isDecimalLiteral(s);
    }

    private static boolean isHexLiteral(String s) {
        return HEX_NUMBER.matcher(s).matches();
    }

    private static boolean isBinaryLiteral(String s) {
        return BIN_NUMBER.matcher(s).matches();
    }

    private static boolean isDecimalLiteral(String s) {
        return DEC_NUMBER.matcher(s).matches();
    }
}

Related

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