regex Java 21与Java 17模式匹配器

1hdlvixo  于 11个月前  发布在  Java
关注(0)|答案(1)|浏览(142)

我目前正在尝试使用Java 21运行一个项目,该项目目前使用Java 17运行,没有任何问题。
对于我们的一些正则表达式模式,有一些与Java21匹配的模式在Java17中不匹配,反之亦然。
它可以用这个简单的代码复制:

public static void main(String[] args) {
        //english
        test(
                "...their sample variance, and σ2N their population variance.",
                "(?<![A-Z\\$€£¥฿฿=]-?[0-9\\.]{0,5})((\\b|\\-)[0-9]{1,5}[0-9,.]{0,5}(€|¥|฿|฿|°C|°F|°De?|°R[éeøa]?|(Z|E|P|T|G|M|k|h|da|d|c|m|µ|n|f|z|y)[ΩΩm]|[ΩΩ]|(Z|E|P|T|G|M|k|h|da|d|c|m|µ|n|p|f|a|z|y)?N|[kKMGTPEZY]i?B|[kmµnp]g|[Mk]t|kWh|GWa|MWd|MWh)(?!\\w))",
                true,
                null);
        //french
        test(
                "Il a été mis au banc de la société.",
                "\\bau (banc) (?:des nations|de la (?:société|ville|communauté|France)|de l['´‘’′](?:Europe|empire|église|islam))\\b",
                false,
                "au banc de la société");
    }

    private static void test(String text, String regex, boolean caseSensitive, String expected) {
        int flags = caseSensitive ? 0 : Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
        Pattern pattern = Pattern.compile(regex, flags);
        Matcher matcher = pattern.matcher(text);
        int start = 0;
        String match = null;
        while (matcher.find(start)) {
            match = text.substring(matcher.start(), matcher.end());
            start = matcher.end();
        }
        System.out.println("Expected: " + expected);
        System.out.println("Got: " + match);
    }

字符串
使用Java 17:

Expected: null
Got: null
Expected: au banc de la société
Got: au banc de la société


使用Java 21:

Expected: null
Got: 2N
Expected: au banc de la société
Got: null


在Java 21中也会出现与Java 17相同的行为。

wvt8vs2t

wvt8vs2t1#

maybe“Regex \b Character Class Now Matches ASCII Characters only by Default(JDK-8264160)"; less probable“Support Unicode 14.0(JDK-8268081)”(both from Release Notes Java 19); or“Support Unicode 15.0(JDK-8284842)”(Release Notes Java 20)- user85421 2天前

相关问题