路径中文件名的正则表达式 - macos

Regex for filename in path - macos

我正在尝试使用以下代码行读取我的下载文件夹(我有一个 mac 系统)中的文件:

    PDDocument doc = PDDocument.load(new File("/Users/mohand/Downloads/1963_Automation_BigTurnip_290618.pdf"));

问题是,数字“1963”会不断变化,但文本“_Automation_BigTurnip_290618.pdf”会保持不变。

我可以使用任何正则表达式来提取任何包含“_Automation_BigTurnip_290618.pdf”的文件吗?

您可以使用这样的正则表达式:

^(\d+)_Automation_BigTurnip_290618\.pdf$

这将匹配任何以至少一位数字开头并以您指定的模式结尾的文件。

不需要正则表达式。只需使用 endsWith:

if (name.endsWith("_Automation_BigTurnip_290618.pdf"))

我使用了:

File dir = new File("/Users/mohand/Downloads/");
File[] files = dir.listFiles((dir1, name) ->  name.endsWith("Automation_BigTurnip_290618.pdf"));