正则表达式与Windows上的文本不匹配,在Mac上按预期工作

时间:2017-01-27 20:16:21

标签: java regex windows

我有以下代码

String timeStampSentence = lastedEditedElement.getText();
LOG.info(timeStampSentence);
Pattern timestampPattern =
    Pattern.compile("Last edited by [a-zA-Z]* on ([a-zA-Z]* [0-9]*), ([0-9]*) at ([0-9:]*) ([amp]*)");
Matcher matcher = timestampPattern.matcher(timeStampSentence);
String day = matcher.group(1);

我尝试匹配的字符串(作为LOG的输出)是Last edited by admin on January 27, 2017 at 8:12 pm,在线测试是matched

抛出的异常是(在Windows上,就像在Mac上一样,它看起来像是在工作)

  

java.lang.IllegalStateException:找不到匹配项   java.util.regex.Matcher.group(Matcher.java:536)at   com.xxx.integration.test.notification.steps.wordpress.editor.WordPressEditorSteps.iShouldBeAbleToSeeTheLastEditedTimestampOnTheEditorPage(WordPressEditorSteps.java:249)   在✽。我应该能够看到最后编辑的时间戳   编辑   页(F:/content-stack-integration-tests/src/test/resources/features/wordpress/stories-dashboard.feature:24)

Windows 7上的jdk为1.8.0_111

1 个答案:

答案 0 :(得分:2)

在检索第一组之前,您应该致电Matcher#matches()

Matcher matcher = timestampPattern.matcher(timeStampSentence);
if(matcher.matches()) {
    String day = matcher.group(1);
    ...
}

group()的文档提及:

  

返回上一个匹配操作期间给定组捕获的输入子序列