zoukankan      html  css  js  c++  java
  • Java正则表达式

    // 正则匹配
        public Boolean regex(String str, String regEx) {
            // 编辑正则表达式
            Pattern pattern = Pattern.compile(regEx);
            // 忽略大小写
            Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
            Matcher mat = pattern.matcher(str);
            // 判断字符串是否与正则表达式匹配,matches()方法
            boolean rs = mat.matches();
            // 查找字符串中是否有匹配正则表达式的字符/字符串,find()方法
            boolean rs2 = mat.find();
            return rs;
        }
    mat.group(0); //  0组是整个表达式
    mat.group(1); //  正则中,匹配的第一个括号中的字符串
    // 从正则表达式左侧开始,每出现一个左括号"("记做一个分组,分组编号从 1 开始。0 代表整个表达式。
    

     对于时间字符串:2017-04-25,表达式如下:

    (\d{4})-((\d{2})-(\d{2}))
    

     四个分组:

    有 4 个左括号,所以有 4 个分组:

    编号捕获组匹配
    0 (d{4})-((d{2})-(d{2})) 2017-04-25
    1 (d{4}) 2017
    2 ((d{2})-(d{2})) 04-25
    3 (d{2}) 04
    4 (d{2}) 25

     main方法中调用:

    public static void main(String[] args) {
            SortDemo sortDemo = new SortDemo();
            String regEx = "(0+)";
            String str = "000111";
            System.out.println(sortDemo.regex(str, regEx));
    }
  • 相关阅读:
    ActionForm补充
    ActionForward
    struts模式匹配
    ActionMapping
    struts1.x的国际化
    DispatchAction
    ActionForm作为类型转换
    struts自定义异常
    hibernate核心接口
    Visual C# 2008+SQL Server 2005 数据库与网络开发 9.5 小结
  • 原文地址:https://www.cnblogs.com/starstarstar/p/11228162.html
Copyright © 2011-2022 走看看