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;<br>//英文,数字 开头,加上 中文  可选的英文
        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 @   wtx  阅读(374)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示