繁体   English   中英

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

[英]Regexp not matching text on windows, working as expected on a Mac

我有以下代码

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 ,在线测试,匹配

抛出的异常是(在 Windows 上,就像在 Mac 上一样)

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

jdk 在 Windows 7 上是 1.8.0_111

您应该在检索第一组之前调用Matcher#matches()

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

group()的文档提到:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM