【问题标题】:Pattern matching a file in a specific directory that may be nested inside that directory模式匹配特定目录中可能嵌套在该目录中的文件
【发布时间】:2021-06-21 14:56:14
【问题描述】:

我正在使用reflections 库。

尝试在名为 demo 的目录中匹配 ID 为 MyPage 的页面,例如:

com/org/apps/demo/view/{any}/{number}/{of}/{dirs}/MyPage.xml
or
com/org/apps/demo/view/{dir}/MyPage.xml
or
com/org/apps/demo/view/MyPage.xml

这是我认为与路径匹配的模式:

reflections.getResources(Pattern.compile(".*demo.*MyPage.xml"))

但它返回 0 个结果。

反射的初始化:

Reflections reflections =
        new Reflections(
            new ConfigurationBuilder()
                .setUrls(ClasspathHelper.forPackage("com.org"))
                .setScanners(new ResourcesScanner()));

String pageId = "MyPage";

Set<String> pages = reflections.getResources(Pattern.compile(".*demo.*MyPage.xml"))

pages 则包含 0 个元素。

而如果我使用类似的东西。 (单个通配符):

Set<String> pages = reflections.getResources(Pattern.compile(".*MyPage.xml"))

pages 然后包含 1 个元素,正如预期的那样。

com/org/apps/demo/view/subdir/MyPage.xml

【问题讨论】:

  • 我不知道反射库有什么问题,但我确信你的正则表达式太松散了。你会想要(?:^|.*\/)demo\/(?:.+\/)?MyPage\.xml,否则com/org/apps/demonstration/view/MyPage.xmlcom/org/apps/demo/view/MyPage-xml 会匹配

标签: java regex reflections


【解决方案1】:

您可以尝试此模式,以匹配完整路径:

^com/org/apps/demo/view/(?:[^/]+/)*MyPage[.]xml$

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    相关资源
    最近更新 更多