【技能库】-- java.util.regex.Pattern 正则提取括号中的内容{} () [](249)

来源:互联网 发布:网络病毒软件下载 编辑:程序博客网 时间:2024/04/29 16:47

示例:

    public static Pattern pattern1 = Pattern.compile("(?<=\\{)[^\\}]+");    public static void main(String[] args) {        String content = "你好,你的班车从 ${dep.name} 到 ${arr.name} 发生XX,${yyy}您是否需要退票";        Matcher m = pattern1.matcher(content);        while (m.find()) {            System.out.println("Found value: " + m.group() );        }    }

结果:
Found value: dep.name
Found value: arr.name
Found value: yyy

参考:https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Special constructs (named-capturing and non-capturing)(?<name>X)                        X, as a named-capturing group(?:X)                                     X, as a non-capturing group(?idmsuxU-idmsuxU)       Nothing, but turns match flags i d m s u x U on - off(?idmsux-idmsux:X)            X, as a non-capturing group with the given flags i d m s u x on - off(?=X)                                 X, via zero-width positive lookahead(?!X)                                     X, via zero-width negative lookahead(?<=X)                                X, via zero-width positive lookbehind(?<!X)                                X, via zero-width negative lookbehind(?>X)                                 X, as an independent, non-capturing groupCharacter classes[abc]                    a, b, or c (simple class)[^abc]                  Any character except a, b, or c (negation)[a-zA-Z]                a through z or A through Z, inclusive (range)[a-d[m-p]]          a through d, or m through p: [a-dm-p] (union)[a-z&&[def]]        d, e, or f (intersection)[a-z&&[^bc]]        a through z, except for b and c: [ad-z] (subtraction)[a-z&&[^m-p]]   a through z, and not m through p: [a-lq-z](subtraction)
原创粉丝点击