gpt4 book ai didi

java - 使正则表达式在 Windows 和 Linux 上兼容

转载 作者:太空宇宙 更新时间:2023-11-04 05:14:41 24 4
gpt4 key购买 nike

我有以下正则表达式: ".*(?=\\b)"+ Pattern.quote(key)+ "(?<=\\b).*"这个正则表达式在 Windows 上工作正常,但在 Linux 上不行。我怎样才能让这个正则表达式在 Windows 和 Linux 上都工作

整个代码是

    List<String> mapKeyList = new ArrayList<String>(myPropMapKeys.keySet());
List<String> matchFileList = new ArrayList<>();

Predicate<String> p = (str) -> mapKeyList.stream().anyMatch(key -> str.matches(".*\\b" + Pattern.quote(key) + "\\b.*")); //To make this regex work on linux

if (listOfIncludedFiles.length > 0) {
System.out.println("Files that contains key are:: ");
for (String myFile : listOfIncludedFiles) {
File in = new File(myFile);
InputStreamReader r = new InputStreamReader(new FileInputStream(in));
String mycharset = r.getEncoding();
Charset toBePassedCharset = Charset.forName(mycharset);
try (Stream<String> stream = Files.lines(Paths.get(myFile), toBePassedCharset)) {
// try (Stream<String> stream =
// Files.lines(Paths.get(myFile))) {
boolean foundAKey = stream.anyMatch(p);
if (foundAKey) {
matchFileList.add(myFile);

// Listing the files that have match
System.out.println("**" + ROOT_PATH + File.separator + myFile);
}
}

catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("Total Number of files with matches:: " + matchFileList.size());
}

正则表达式 Predicate<String> p = (str) -> mapKeyList.stream().anyMatch(key -> str.matches(".*(?=\b)" + Pattern.quote(key) + "(?<=\b).*"));无法在 Linux 上运行。

我的示例字符串是“{{LOGGER}}”

最佳答案

i am looking for boundaries of { and }

那么你应该使用

Pattern.compile("(?<!\\{)" + Pattern.quote(key) + "(?!\\})").matcher(str).find()

而不是matches\b单词边界。

(?<!\{)如果当前位置前面带有{,则负向查找失败匹配失败和 (?!\})如果存在 } 则负向前瞻将导致匹配失败就在当前位置之后。

关于java - 使正则表达式在 Windows 和 Linux 上兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50962306/

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