我有以下正则表达式:
Completable
这个正则表达式可以在Windows上正常工作,但不能在Linux上正常工作。
如何使此正则表达式在Windows和Linux上均可工作
整个代码是
".*(?=\\b)"+ Pattern.quote(key)+ "(?<=\\b).*"
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());
}
上的正则表达式在Linux上不起作用。
我的示例字符串是'{{LOGGER}}'
答案 0 :(得分:0)
我正在寻找
{
和}
的界限
那你应该使用
Pattern.compile("(?<!\\{)" + Pattern.quote(key) + "(?!\\})").matcher(str).find()
而不是matches
带有\b
字边界。
如果当前位置前面有(?<!\{)
,则{
否定向后匹配失败,如果紧随其后的是(?!\})
,则}
否定前向匹配失败当前位置。