正则表达式 – 从Perl中的字符串中删除文件扩展名和路径
发布时间:2020-12-14 06:38:23 所属栏目:百科 来源:网络整理
导读:我想获取一个没有其路径的文件名(如果它是字符串的一部分)和扩展名。 例如: /path/to/file/fileName.txt # results in "fileName"fileName.txt # results in "fileName"/path/to/file/file.with.periods.txt # results in "file.with.periods" 所以基本上,
我想获取一个没有其路径的文件名(如果它是字符串的一部分)和扩展名。
例如: /path/to/file/fileName.txt # results in "fileName" fileName.txt # results in "fileName" /path/to/file/file.with.periods.txt # results in "file.with.periods" 所以基本上,我想删除之前的任何东西,包括最后的“/”,如果存在,还有最后一个“”。以及之后的任何元字符。 对于这样的新手问题抱歉,但我是新来的perl。
为了方便地获取一个文件的基本名称给一个完整的路径,我建议
File::Basename 模块,这是核心的一部分。
要做文件扩展名的启发式,我会去一个正则表达式 (my $without_extension = $basename) =~ s/.[^.]+$//; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |