gpt4 book ai didi

Java 正则表达式适用于 Linux,但不适用于 Windows

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:51 28 4
gpt4 key购买 nike

我构建了一个java类名Scanner。此类打开文件并对其应用三个正则表达式。

在我的 Linux 系统上,所有三种模式都工作正常。在 Windows 上只有两个,几个小时后我真的不知道为什么......

这是我的三种模式

private static final Pattern TILE_PATTERN = Pattern.compile("<xyz:tile\\sfilename=\"(\\S+)\"");
private static final Pattern VALUE_PATTERN = Pattern.compile("<xyz:value\\sxyz:name=\"(\\S+)\"[\\sxyz:type=\"(\\S)\"]*>");
private static final Pattern IF_PATTERN = Pattern.compile("<xyz:if-visible\\sxyz:name=\"(\\S+)\">");

VALUE_PATTERNIF_PATTERN 没问题。TILE_PATTERN 不起作用...

这是我的方法:

public static List<String> scanForTiles(TemplateModel template) {
try (Stream<String> stream = Files.lines(template.getPath())) {
List<String> includes = new ArrayList<>();
stream.map(TILE_PATTERN::matcher)
.flatMap(matcher -> {
List<String> list = new ArrayList<>();
while (matcher.find()) {
list.add(matcher.group(1));
}
return list.stream();
})
.forEach(includes::add);
return includes;
}catch (IOException e) {
e.printStackTrace();
}
return null;
}

我的模板的示例代码:

<!-- NOT ok -->
<xyz:tile filename="containers/mainLogin.html">
<!-- OK -->
<xyz:if-visible xyz:name=".variable">

最佳答案

Linux 和 Windows 之间的显着区别是新行。在linux中,新行的字符是\n(换行),在windows中,新行由两个字符\r\n(回车和换行)表示。
此外,默认情况下,正则表达式不会在多行上匹配,并且通常换行符充当 $ 或结束行匹配器,这可能是正则表达式在两个操作系统中表现不同的原因之一。

关于Java 正则表达式适用于 Linux,但不适用于 Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35746860/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com