2013-08-29 157 views
2

文件版本我有如下的文件名:從提取的文件名

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";' 

回答

1

你也可以使用正則表達式從字符串的結尾提取數字和.

my ($name, $version) = $filename =~ /(.*?)\.([.\d]*)$/; 
3

分成2個在.後面跟着一個數字:

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