Java Regex

Java.util.Regex

Pattern Matcher

正则表达式开始符号: `^`

结束符号:`$`

java转义: \\

Matcher中的group: (regexStr)

group name: (?<name>regexStr)

 

    String patternStr = "^(?<date>\\d{8})\\.(?<cluster>\\w*)\\.(?<index>\\w*)\\.(?<operation>create|update)\\.(?<ohter>.*)$";
    Pattern pattern = Pattern.compile(patternStr);

    String testStr1 = "20190317.cluster.index.create.yml";
    String testStr2 = "20190318.cluster.index.update.yml";
    String testStr3 = "20190319.cluster.index.delete.yml";

    List<String> testList = Arrays.asList(testStr1, testStr2, testStr3);

    testList.stream()
        .map(test -> pattern.matcher(test))
        .forEach(matcher -> {
          if (matcher.matches()) {
            System.out.println(matcher.group("date"));
            System.out.println(matcher.group("cluster"));
            System.out.println(matcher.group("operation"));
          }
        });

https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html

posted @ 2019-03-14 19:32  SEC.VIP_网络安全服务  阅读(79)  评论(0编辑  收藏  举报