简体   繁体   English

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

[英]Regex for filename in path - macos

I am trying to read a file in my Download folder (I have a mac system) using the below line of code: 我正在尝试使用以下代码行读取我的“下载”文件夹(我有一个Mac系统)中的文件:

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

The problem is, the numeral "1963" will keep on changing but text "_Automation_BigTurnip_290618.pdf" will remain the same. 问题是,数字“ 1963”将保持不变,但文本“ _Automation_BigTurnip_290618.pdf”将保持不变。

Can I use any regex that will pick up any file that has "_Automation_BigTurnip_290618.pdf" ? 我可以使用任何正则表达式提取包含“ _Automation_BigTurnip_290618.pdf”的任何文件吗?

I did this using : 我这样做是使用:

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

You could use a regular expression like this: 您可以使用如下正则表达式:

^(\d+)_Automation_BigTurnip_290618\.pdf$

This would match any file starting with at least one digit and ending in the pattern you specified. 这将匹配任何以至少一位数字开头并以您指定的模式结尾的文件。

No need for regex. 无需正则表达式。 Just use endsWith : 只需使用endsWith

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM