Java判断字符串是否为数字(正则表达式)

package com.walker;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tester {
    public static void main(String[] args) {
        String str = "123"; //true
        System.out.println(isNumeric(str));
            
        str = "-123";   //true
        System.out.println(isNumeric(str));
            
        str = "abc";    //false
        System.out.println(isNumeric(str));
    }
        
    //@ author: walker
    //用正则表达式判断字符串是否为数字(含负数)
    public static boolean isNumeric(String str) {
        String regEx = "^-?[0-9]+$";
        Pattern pat = Pattern.compile(regEx);
        Matcher mat = pat.matcher(str);
        if (mat.find()) {
            return true;
        }
        else {
            return false;
        }
    }
}


*** walker ***


你可能感兴趣的:(java,字符串,正则表达式,数字)