java 正则表达式

public static String filter(String displayName){
        Pattern pen = null;
        String en_begin = "([A-Za-z ]+)";
        pen = Pattern.compile(en_begin);

        JSONArray jsonArray = new JSONArray();
        JSONObject jsonintr = new JSONObject();
        if (pen.matcher(displayName).matches()) {
            jsonintr.element("en_US",displayName);
        } else {
            jsonintr.element("zh_CN",displayName);
        }
        jsonArray.add(jsonintr);
        return jsonArray.toString();
    }

    public static String filterVersionNum(String displayName) {
        String versionNum = "(android|os)?( )*([0-9].)*([0-9])( (B|b)eta)?";
        String result = displayName;
        result = result.replaceAll(versionNum, "");
        return result;
    }

 

正则表达式:

 

 

public static void regPattern() {
		Pattern p = null;
		Matcher m = null;
		boolean b = false;
//英文,数字 开头,加上 中文  可选的英文 String reg_charset = "([a-z]*)([A-Z]*)([0-9]*)([\u4E00-\u9FA5]*)([a-z]*)([A-Z]*)([0-9]*)"; p = Pattern.compile(reg_charset); m = p.matcher("sdgf中文"); b = m.matches(); System.out.println(b);// true } public static void regPattern2() { Pattern p = null; Matcher m = null; boolean b = false; String reg_charset = "([\u4E00-\u9FA5 ]*)(([a-z]*)([A-Z]*))([\u4E00-\u9FA5]*)"; p = Pattern.compile(reg_charset); m = p.matcher("中文 fdgfdg中文"); b = m.matches(); System.out.println(b);// tree m = p.matcher("中文 ");// false b = m.matches(); System.out.println(b); } public static void regPattern3() { // (^s*)|(s*$) 空格 Pattern p = null; Matcher m = null; boolean b = false; String reg_charset = "([\u4E00-\u9FA5]*)((^s*)|(s*$))"; p = Pattern.compile(reg_charset); m = p.matcher("中文 "); b = m.matches(); System.out.println(b);// false } public static void regPattern4() { // 空格 Pattern p = null; Matcher m = null; boolean b = false; String reg_charset = "((^s*)|(s*$))"; p = Pattern.compile(reg_charset); m = p.matcher(" "); b = m.matches(); System.out.println(b);// false } public static void regPattern5() { Pattern p = null; Matcher m = null; boolean b = false; String reg_charset = "([\u4E00-\u9FA5]+)((([a-z]*)([A-Z]*)([0-9]*))*)([\u4E00-\u9FA5]*)"; p = Pattern.compile(reg_charset); m = p.matcher("新生代捕鱼2HD"); b = m.matches(); System.out.println(b);// true } public static void regPattern6() { Pattern p = null; Matcher m = null; boolean b = false; String reg_charset = "([A-Za-z]+)((([\u4E00-\u9FA5]*)([0-9]*))*)([A-Za-z]*)"; p = Pattern.compile(reg_charset); m = p.matcher("SDJFsdfg洗涤剂发FDGsd"); b = m.matches(); System.out.println(b);// true } // 以数字开头的 // 数字 中 英 // 数字 中 // 数字 英 // 数字 public static void regPattern7() { Pattern p = null; Matcher m = null; boolean b = false; String reg_charset = "([0-9]+)((([\u4E00-\u9FA5]*)([A-Z]*)([a-z]*))*)([0-9]*)"; p = Pattern.compile(reg_charset); m = p.matcher("123苏丹红发asd"); b = m.matches(); System.out.println(b);// true } public static void regParttern8() { Pattern p = null; Matcher m = null; boolean b = false; String versionNum = "(android|os)?( )*([0-9].)*([0-9])( (B|b)eta)?"; // String forStr = "(for )?((A|a)ndroid([0-9].)(0-9)|OS)"; p = Pattern.compile(versionNum); m = p.matcher("android 0.0.3.9 beta"); String str = "android 0.0.1.4 beta "; str = str.replaceAll(versionNum, ""); System.out.println(str); b = m.matches(); System.out.println(b);// true } public static void regParttern9() { Pattern p = null; Matcher m = null; boolean b = false; String versionNum = "([A-Za-z ]+)"; // String forStr = "(for )?((A|a)ndroid([0-9].)(0-9)|OS)"; p = Pattern.compile(versionNum); m = p.matcher("android 0.0.3.9 beta"); String str = "android "; str = str.replaceAll(versionNum, ""); System.out.println("str:" + str); b = m.matches(); System.out.println(b);// true }

 

 

posted @ 2012-04-24 18:30  wtx  阅读(388)  评论(0)    收藏  举报