Java正则表达式判断

时间:2022-11-17 18:48:34
    /* 判断是否为数字 */
    public static boolean isNumeric(String str)
    {
     if(str == null || str.isEmpty()){
      return false;
     }
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches())
        {
            return false;
        }
        return true;
    }
    /* 判断是否为数字或字母 */
    public static boolean isNumOrLetter(String str)
{
    Pattern pattern = Pattern.compile("^[a-zA-Z0-9]+$");
    Matcher isRight = pattern.matcher(str);
    if (!isRight.matches())
    {
        return false;
    }
    return true;
}
/* 判断是否有字母 */
public static boolean isLetter(String str)
{
    Pattern pattern = Pattern.compile(".*[a-zA-Z]+.*");
    Matcher islett = pattern.matcher(str);
    if (!islett.matches())
    {
        return false;
    }
    return true;
  }
/* 判断是否为+号数字或字母 */
 public static boolean isPlusNumOrLetter(String str)
{
 Pattern pattern = Pattern.compile("^\\+?[a-zA-Z0-9]+$"); ///^\+?[0-9a-zA-Z]+$/
 Matcher isRight = pattern.matcher(str);
 if (!isRight.matches())
 {
     return false;
 }
 return true;
}