legs+ 发表于 2023-10-25 19:01:53

写了个工具类:

spring boot不推荐工具类自己写

我就霸王硬上弓
写了一个正则表达式

代码如下:


public class PatternUtil {

    /**
   * 匹配邮箱正则
   */
    private static final Pattern VALID_EMAIL_ADDRESS_REGEX =
            Pattern.compile("^+@+\\.{2,6}$", Pattern.CASE_INSENSITIVE);

    /**
   * 验证只包含中英文和数字的字符串
   *
   * @param keyword
   * @return
   */
    public static Boolean validKeyword(String keyword) {
      String regex = "^+$";
      Pattern pattern = Pattern.compile(regex);
      Matcher match = pattern.matcher(keyword);
      return match.matches();
    }


    /**
   * 判断是否是邮箱
   *
   * @param emailStr
   * @return
   */
    public static boolean isEmail(String emailStr) {
      Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr);
      return matcher.find();
    }

    /**
   * 判断是否是网址
   *
   * @param urlString
   * @return
   */
    public static boolean isURL(String urlString) {
      String regex = "^({2}:/*|{2}:/*|:/*)((+).)+()+(\\?{0,1}((+\\={0,1})(*)\\&{0,1})*)$";
      Pattern pattern = Pattern.compile(regex);
      if (pattern.matcher(urlString).matches()) {
            return true;
      } else {
            return false;
      }
    }

}

注意关键字:private指包内能见;public全局

孤星3 发表于 2023-10-25 20:18:22

\u4E00 -- \u9FA5

真棒,学习了,原来汉字只在这些Codepoint. :victory:
页: [1]
查看完整版本: 写了个工具类: