用于文件搜索的正则表达式过滤器 Java
Regex filter for file search Java
我对使用正则表达式还很陌生,所以我当前的代码有问题。我创建了一个 returns 文件列表的抽象文件搜索。我希望这个搜索器被正则表达式过滤(例如,它根据正则表达式过滤器查找的扩展名)。
我的摘要搜索器的代码:
public abstract class AbstractFileDiscoverer implements IDiscoverer {
private final Path rootPath;
AbstractFileDiscoverer(final Path rootPath) {
super();
this.rootPath = rootPath;
}
protected List<File> findFiles() throws IOException {
if (!Files.isDirectory(this.rootPath)) {
throw new IllegalArgumentException("Path must be a directory");
}
List<File> result;
try (Stream<Path> walk = Files.walk(this.rootPath)) {
result = walk.filter(p -> !Files.isDirectory(p)).map(p -> p.toFile())
.filter(f -> f.toString().toLowerCase().endsWith("")).collect(Collectors.toList());
}
return result;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return null;
}
}
我希望正则表达式过滤以下部分,以便仅收集正则表达式 returns 为真的文件(对于 .bat 和 .sql 文件)。
result = walk.filter(p -> !Files.isDirectory(p)).map(p -> p.toFile())
.filter(f -> f.toString().toLowerCase().endsWith("")).collect(Collectors.toList());
谁能帮我实现它?
第一次编辑:
我知道 toString().toLowerCase().endsWith("")
总是 returns 是真的,我实际上需要那里的正则表达式而不是带有扩展名的字符串。我忘记说了。
试试这个网站:https://regexr.com/ 并粘贴正则表达式 .+(?:.sql|.bat)$
以获得解释。
在代码中它看起来像这样:
Stream.of("file1.json", "init.bat", "init.sql", "file2.txt")
.filter(filename -> filename.matches(".+(?:.sql|.bat)$"))
.forEach(System.out::println);
杰米·扎温斯基 (Jamie Zawinski) 有一句名言,是关于在更简单的 non-regex 代码就可以的情况下使用正则表达式。
在你的情况下,我会避免使用正则表达式而只写一个私有方法:
private static boolean hasMatchingExtension(Path path) {
String filename = path.toString().toLowerCase();
return filename.endsWith(".bat") || filename.endsWith(".sql");
}
然后您就可以在您的信息流中使用它了:
result = walk.filter(p -> !Files.isDirectory(p)).
.filter(p -> hasMatchingExtension(p))
.map(p -> p.toFile())
.collect(Collectors.toList());
(考虑返回 List<Path>
。路径 class 是文件 class 的现代替代品,其中一些实际操作文件的方法存在设计问题。)
我对使用正则表达式还很陌生,所以我当前的代码有问题。我创建了一个 returns 文件列表的抽象文件搜索。我希望这个搜索器被正则表达式过滤(例如,它根据正则表达式过滤器查找的扩展名)。
我的摘要搜索器的代码:
public abstract class AbstractFileDiscoverer implements IDiscoverer {
private final Path rootPath;
AbstractFileDiscoverer(final Path rootPath) {
super();
this.rootPath = rootPath;
}
protected List<File> findFiles() throws IOException {
if (!Files.isDirectory(this.rootPath)) {
throw new IllegalArgumentException("Path must be a directory");
}
List<File> result;
try (Stream<Path> walk = Files.walk(this.rootPath)) {
result = walk.filter(p -> !Files.isDirectory(p)).map(p -> p.toFile())
.filter(f -> f.toString().toLowerCase().endsWith("")).collect(Collectors.toList());
}
return result;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return null;
}
}
我希望正则表达式过滤以下部分,以便仅收集正则表达式 returns 为真的文件(对于 .bat 和 .sql 文件)。
result = walk.filter(p -> !Files.isDirectory(p)).map(p -> p.toFile())
.filter(f -> f.toString().toLowerCase().endsWith("")).collect(Collectors.toList());
谁能帮我实现它?
第一次编辑:
我知道 toString().toLowerCase().endsWith("")
总是 returns 是真的,我实际上需要那里的正则表达式而不是带有扩展名的字符串。我忘记说了。
试试这个网站:https://regexr.com/ 并粘贴正则表达式 .+(?:.sql|.bat)$
以获得解释。
在代码中它看起来像这样:
Stream.of("file1.json", "init.bat", "init.sql", "file2.txt")
.filter(filename -> filename.matches(".+(?:.sql|.bat)$"))
.forEach(System.out::println);
杰米·扎温斯基 (Jamie Zawinski) 有一句名言,是关于在更简单的 non-regex 代码就可以的情况下使用正则表达式。
在你的情况下,我会避免使用正则表达式而只写一个私有方法:
private static boolean hasMatchingExtension(Path path) {
String filename = path.toString().toLowerCase();
return filename.endsWith(".bat") || filename.endsWith(".sql");
}
然后您就可以在您的信息流中使用它了:
result = walk.filter(p -> !Files.isDirectory(p)).
.filter(p -> hasMatchingExtension(p))
.map(p -> p.toFile())
.collect(Collectors.toList());
(考虑返回 List<Path>
。路径 class 是文件 class 的现代替代品,其中一些实际操作文件的方法存在设计问题。)