写了个工具类:

电脑技术 电脑技术 376 人阅读 | 1 人回复 | 2023-10-25

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
spring boot不推荐工具类自己写

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

代码如下:


  1. public class PatternUtil {

  2.     /**
  3.      * 匹配邮箱正则
  4.      */
  5.     private static final Pattern VALID_EMAIL_ADDRESS_REGEX =
  6.             Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);

  7.     /**
  8.      * 验证只包含中英文和数字的字符串
  9.      *
  10.      * @param keyword
  11.      * @return
  12.      */
  13.     public static Boolean validKeyword(String keyword) {
  14.         String regex = "^[a-zA-Z0-9\u4E00-\u9FA5]+$";
  15.         Pattern pattern = Pattern.compile(regex);
  16.         Matcher match = pattern.matcher(keyword);
  17.         return match.matches();
  18.     }


  19.     /**
  20.      * 判断是否是邮箱
  21.      *
  22.      * @param emailStr
  23.      * @return
  24.      */
  25.     public static boolean isEmail(String emailStr) {
  26.         Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr);
  27.         return matcher.find();
  28.     }

  29.     /**
  30.      * 判断是否是网址
  31.      *
  32.      * @param urlString
  33.      * @return
  34.      */
  35.     public static boolean isURL(String urlString) {
  36.         String regex = "^([hH][tT]{2}[pP]:/*|[hH][tT]{2}[pP][sS]:/*|[fF][tT][pP]:/*)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+(\\?{0,1}(([A-Za-z0-9-~]+\\={0,1})([A-Za-z0-9-~]*)\\&{0,1})*)$";
  37.         Pattern pattern = Pattern.compile(regex);
  38.         if (pattern.matcher(urlString).matches()) {
  39.             return true;
  40.         } else {
  41.             return false;
  42.         }
  43.     }

  44. }
复制代码

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

回答|共 1 个

孤星3 发表于 2023-10-25 20:18:22| 字数 39 | 显示全部楼层

\u4E00 -- \u9FA5

真棒,学习了,原来汉字只在这些Codepoint.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热门推荐