从文件名中提取文件版本

时间:2013-08-29 08:18:47

标签: regex perl

我的文件名如下:

first_config.txt.1.1
second_config.2.1.1.1

我想分开文件名&使用perl从完整文件名中获取版本。

first_config.txt & 1.1 
second_config & 2.1.1.1

我尝试了以下代码,但它没有按预期工作。

to extract the version from the complete file name string:
perl -e '$n = "first_config.txt.1.1"; $n =~s/[^\.\d.]//g; print "$n\n";'

to extract the name from the complete file name string:
perl -e '$n = "first_config.txt.1.1"; $n =~s/[\.^\d.]//g; print "$n\n";'

2 个答案:

答案 0 :(得分:3)

.上分成2,然后是数字:

my ($name, $version) = split /\.(?=\d)/, $filename, 2;

答案 1 :(得分:1)

您还可以使用正则表达式从字符串末尾提取数字和.

my ($name, $version) = $filename =~ /(.*?)\.([.\d]*)$/;
相关问题