use pattern check negative number java

正则表达式判断字符串是数字,可以为正数,可以为负数,不能含有字符。

Java代码  收藏代码
  1. public static boolean isNumeric(String str)  
  2.     {  
  3.     Pattern pattern = Pattern.compile(“-?[0-9]*”);  
  4.     Matcher isNum = pattern.matcher(str);  
  5.     if( !isNum.matches() )  
  6.     {  
  7.     return false;  
  8.     }  
  9.     return true;  
  10.     }   

 如果判断可以含有小数点,那么条件改为:

Java代码  收藏代码
  1. Pattern pattern = Pattern.compile(“-?[0-9]*.?[0-9]*”);  

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s