Android正则匹配实例

来源:互联网 发布:人工智能的上市公司 编辑:程序博客网 时间:2024/05/18 14:45

匹配车辆牌照的前位,如”浙B”

/** * 检查车牌前两位 * @param licensePlate * @return true:符合;false:不符合 */public static boolean checkCarLicensePlate(String licensePlate) {    if (!TextUtils.isEmpty(licensePlate)) {        Pattern p = Pattern.compile("(京|沪|津|渝|黑|吉|辽|蒙|冀|新|甘|青|陕|宁|豫|鲁|晋|皖|鄂|湘|苏|川|黔|滇|桂|藏|浙|赣|粤|闽|台|琼|港|澳){1}[A-Z]{1}");        Matcher m = p.matcher(licensePlate);        if (m.matches()) {            return true;        }else {            return false;        }    }    return false;}

匹配特殊的字符串

/** * 过滤特殊字符 * @param str * @return */public static String replaceBlank(String str) {    String dest = "";    if (str!=null) {        Pattern p = Pattern.compile("\\s*|\t|\r|\n");        Matcher m = p.matcher(str);        dest = m.replaceAll("");    }    return dest;}
原创粉丝点击