fanfanfys 2020-03-09 16:46 采纳率: 0%
浏览 1214
已结题

java,正则匹配域名问题

在网上找到匹配一级二级三级域名的正则是这样的
// 一级域名提取
private static final String RE_TOP1 = "(\w*\.?){1}\.(com.cn|net.cn|gov.cn|org\.nz|org.cn|com|net|org|gov|cc|biz|info|cn|co)$";

// 二级域名提取
private static final String RE_TOP2 = "(\w*\.?){2}\.(com.cn|net.cn|gov.cn|org\.nz|org.cn|com|net|org|gov|cc|biz|info|cn|co)$";

// 三级域名提取
private static final String RE_TOP3 = "(\w*\.?){3}\.(com.cn|net.cn|gov.cn|org\.nz|org.cn|com|net|org|gov|cc|biz|info|cn|co)$";

但是这要匹配三次才行,我现在要匹配一次就能区分出来一级二级三级域名,就是把上面的三个规则融合成一个规则

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-03-09 21:02
    关注
    private static final String RE_TOP1to3 = "(\w*\.?){1,3}\.(com.cn|net.cn|gov.cn|org\.nz|org.cn|com|net|org|gov|cc|biz|info|cn|co)$";
    
    评论

报告相同问题?