Java 正则表达式 NPE Matcher.Find()

Java Regex NPE On Matcher.Find()

我快要发疯了,想弄清楚为什么这不起作用。

我有一个更改日志文件,我想在其中使用内容并解析版本号。我有一个可用的现有正则表达式,我已将其用作新正则表达式的基线以仅去除版本号并可以看到它可以正常工作。但是,当我尝试使用 .find() 方法遍历新的 Matcher 结果以获取版本号时,我遇到了 Null Pointer Exception 并且没有找到任何结果。

正则表达式、编译方式或其他问题是否存在问题?

FWIW,当我在一些在线正则表达式编辑器(基于Java)上尝试时,正则表达式模式完全符合我的要求。

这就是我 运行。

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HelloWorld{

    public static final String changelog="## Current Version"+
            "\n\n"+
            "- None"+
            "\n\n"+
            "## 5.18.72 (2020-01-17)"+
            "\n\n"+
            "- #### [US12345]"+
            "\n\n"+
            "## 5.18.71 (2020-01-15)"+
            "\n\n"+
            "- #### [US123456]"+
            "\n\n"+
            "## 5.18.70 (2020-01-09)";

    private static final String RAW_CHANGELOG_PATTERN = "(?ms)(##[\w\s\-]*.%s.*\n\n.*)(\n\n##[\w\s\-]*.%s)";
    private static final String RAW_CHANGELOG_VERSION_PATTERN="(?ms)(\d{1,3}\.\d{1,3}\.\d{1,3})";

     public static void main(String []args){


        System.out.println(extractKnownVersions(changelog));

        System.out.println("Next\n");

        System.out.println(extractVersions(changelog.toString()));
     }

    private static String extractKnownVersions(String changelog){
        String patternString = String.format(RAW_CHANGELOG_PATTERN,"5.18.72","5.18.70");
        Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(changelog);
        String result = null;
        if (matcher.find()) {
            result = matcher.group();
        } else {
            System.out.println("No diff found between versions 5.18.72 and 5.18.70 Check that both versions exist in Changelog.");
        }
        return result;
    }

    private static List<String> extractVersions(String changelog) {
        // TODO: Fix the regex so that it actually grabs what we want
        String patternString = RAW_CHANGELOG_VERSION_PATTERN;
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(changelog);

        List<String> result = null;
        int count = 0;

        try {
            while (matcher.find()) {
                result.add(matcher.group());
                System.out.println("result: " + result.get(count));
                count++;
            }
        } catch (NullPointerException npe) {
            System.out.println("NPE: Could not extract versions from Changelog");
        } catch (Exception e) {
            System.out.println("Exception extracting versions from Changelog");
        }

        System.out.println("match count:" + count);
        return result;
    }
}

这是 SOP 的输出——注意原始正则表达式 RAW_CHANGELOG_PATTERN 工作并产生我期望的输出(两个版本号之间的所有内容),但是第二种方法 extractVersions使用新的正则表达式 RAW_CHANGELOG_VERSION_PATTERN 抛出 NPE。

## 5.18.72 (2020-01-17)

- #### [US12345]

## 5.18.71 (2020-01-15)

- #### [US123456]

## 5.18.70
Next

NPE: Could not extract versions from Changelog
match count:0
null

我希望这是我遗漏的一些愚蠢的语法?

您的 NPE 来自未初始化的列表:

List<String> result = null;

改为

List<String> result = new ArrayList<>();

result: 5.18.72
result: 5.18.71
result: 5.18.70
match count:3
[5.18.72, 5.18.71, 5.18.70]