Java Regexp捕获组包含空间,为什么?

Java Regexp捕获组包含空间,为什么?,java,regex,Java,Regex,我试图解析这个字符串 "斬釘截鐵 斩钉截铁 [zhan3 ding1 jie2 tie3] /to chop the nail and slice the iron (idiom)/resolute and decisive/unhesitating/definitely/without any doubt/"; 使用此代码 private static final Pattern TRADITIONAL = Pattern.compile("(.*?) "); private St

我试图解析这个字符串

"斬釘截鐵 斩钉截铁 [zhan3 ding1 jie2 tie3] /to chop the nail and slice the iron (idiom)/resolute and decisive/unhesitating/definitely/without any doubt/";
使用此代码

private static final Pattern TRADITIONAL = Pattern.compile("(.*?) ");

    private String extractSinglePattern(String row, Pattern pattern) {
        Matcher matcher = pattern.matcher(row);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }
但是,由于某些原因,返回的字符串末尾包含一个空格

org.junit.ComparisonFailure: expected:<斬釘截鐵[]> but was:<斬釘截鐵[ ]>
但无济于事

我还尝试在模式的末尾匹配两个空格,但不匹配(只有一个空格)。

您正在使用的空格记录为:

返回与上一个匹配匹配的输入子序列

比赛包括场地。比赛中的捕获组没有,但你没有要求

如果您将返回声明更改为:

return matcher.group(1);

然后我相信它会满足您的需要。

使用这个正则表达式
(.+?)(?=\s+)

只是一个建议:为什么不使用
(.*)+
来覆盖多个空格?Marko,我相信格式固定在一个空格上。。所以我宁愿保持这种状态,除非我发现其他方面,非捕获组在这里是多余的。它只有一个成员。
return matcher.group(1);