正则使用以及常用正则

  1. 正则将 浏览器路径 拆分
public static HttpClientModel httpPattern(String path) {
        Pattern pattern = Pattern.compile("(http|https)://(.*?):(.*?)/(.*)");
        Matcher matcher = pattern.matcher(path);
        matcher.matches(); // 执行
        HttpClientModel sy = new HttpClientModel();
        sy.setScheme( matcher.group(1)); //  http
        sy.setHost( matcher.group(2));    // 地址
        sy.setPort(Integer.parseInt( matcher.group(3)));  // 端口
        sy.setPath("/"+matcher.group(4));    //项目路径
        return sy;
    }


  1. 用于 英文单词拆分并且拼接
 public static String pattern(String str) {

//        String regEx = "[A-Za-z]+"; // 匹配 英文单词
        String regEx =   "[a-zA-Z][a-zA-Z0-9_]+";
        Pattern pattern = Pattern.compile(regEx);
        Matcher ma = pattern.matcher(str);
        Set<String> set = new LinkedHashSet();
        StringBuilder sb = new StringBuilder();
        while (ma.find()) {
            String group = ma.group();
            set.add(group);
        }
        for (String s : set) {
            if(s.equals("true")|| s.equals("false")){

            }else{
                sb.append(s);
                sb.append(",");
            }
        }
        String result = sb.toString().substring(0, sb.length() - 1);
        return result;
    }
  1. 常用正则
/* 英文单词 */
    public static final String REGEX_WORD_EN = "^\\w+$";
    /** 中文 */
    public static final String REGEX_WORD_CN = "[\u4E00-\u9FA5]+";
    /** 6到30位 数字 + 字符 + _,常用于用户名注册 */
    public static final String REGEX_WORD_EN_6_30 = "^[a-zA-Z][a-zA-Z0-9_]{6,29}$";
    /** 5到20位 数字 + 字符 + _,常用于用户名注册 */
    public static final String REGEX_WORD_EN_5_20 = "^[a-zA-Z][a-zA-Z0-9_]{5,19}$";
    /** 数字 + 字符 + _,且必须已字母开头 */
    public static final String REGEX_CHAR_NUM_UL = "^[a-zA-Z][a-zA-Z0-9_]+$";
    /** 数字 + 字母 组合 */
    public static final String REGEX_CHAR_NUM = "^[A-Za-z0-9]+$"
    /** 字母组合 */
    public static final String REGEX_CHAR = "^[A-Za-z]+$";
    /** 大写字母组合 */
    public static final String REGEX_CHAR_UC = "^[A-Z]+$";
    /** 小写字母组合 */
    public static final String REGEX_CHAR_LC = "^[a-z]+$";
    /** 双字节 */
    public static final String REGEX_DBCHAR = "[^x00-xff]+";
    /** URL地址: 包括 http, ftp 等 */
    public static final String REGEX_URL = "[a-zA-z]+://[^s]*";
    /** HTTP URL */
    public static final String REGEX_URL_HTTP = "http(s)?://[^s]+";
    /** Internet URL */
    public static final String REGEX_URL_INTERNET = "^http(s)?://([\\w-]+.)+[\\w-]+(/[\\w-./?%&=]*)?$";
    /** HTML 标签 */
    public static final String REGEX_HTML_TAG = "<(\\S*?)[^>]*>.*?|<.*? />";
    /** Email 地址 */
    public static final String REGEX_EMAIL = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*.\\w+([-.]\\w+)*";
    /** 中国电话号码 */
    public static final String REGEX_PHONE_CHINA = "0\\d{2,3}-\\d{7,8}";
    /** 中国移动电话 */
    public static final String REGEX_MOBILE_CHINA = "1[3|5|8][0-9]-\\d{8}";
    /** 中国身份证号 */
    public static final String REGEX_IDCARD_CHINA = "\\d{17}[0-9|x|X]|\\d{15}";
    /** 中国邮政编码 */
    public static final String REGEX_ZIPCODE_CHINA = "\\d{6}";
    /** QQ号 */
    public static final String REGEX_QQ = "[1-9][0-9]{4,}";
    /** 数字 */
    public static final String REGEX_CNUMBER = "^[0-9]+$";
    /** 正数 */
    public static final String REGEX_INT_POSITIVE = "^[1-9]\\d*$";
    /** 非正数 */
    public static final String REGEX_INT_NOT_POSITIVE = "^-[1-9]\\d*|0$";
    /** 负数 */
    public static final String REGEX_INT_NEGATIVE = "^-[1-9]\\d*$";
    /** 非负数 */
    public static final String REGEX_INT_NOT_NEGATIVE = "^[1-9]\\d*|0$";
    /** 整数 */
    public static final String REGEX_CINT = "^-?[1-9]\\d*$";
    /** 浮点数 */
    public static final String REGEX_CFLOAT = "^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$";
    /** 正浮点数 */
    public static final String REGEX_FLOAT_POSITIVE = "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$";
    /** 非正浮点数 */
    public static final String REGEX_FLOAT_NOT_POSITIVE = "^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$";
    /** 负浮点数 */
    public static final String REGEX_FLOAT_NEGATIVE = "^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$";
    /** 非负浮点数 */
    public static final String REGEX_FLOAT_NOT_NEGATIVE = "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$";
    /** 空行 */
    public static final String REGEX_SPACE_LINE = "\n\\s*\r";
    /** 空字符 */
    public static final String REGEX_SPACE_CHAR = "^\\s*|\\s*$";

  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值