Here you can find the source of isNumberLiteral(String s)
Parameter | Description |
---|---|
s | a parameter |
public static boolean isNumberLiteral(String s)
//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(); } }