2017-04-03 88 views
-1

我想串perl腳本將字符串EXP501.1-01.0轉換爲EXP501_01

EXP501.1-01.0

EXP501_01

轉換

這意味着-01,-02,-03 ...是增量式的,我想追加第二個字符串(將根據第一個字符串進行分析)的_旁邊的第一個字符串中的相同數字。

+0

我改變了一些文本格式,突出顯示了一些數字,並添加了兩個相關的標籤。 – zx485

回答

1
my $string = "EXP501.1-01.0"; 
    $short = substr($string, 0, -7); 
    $new = substr($string,8,-2); 
    my $str = $short . $new; 
    $str =~ s/ - /_/g; # Replace all " - " with "_" 
    $str =~ s/[^A-Za-z0-9]/_/g; # Replace all non-alphanumericals with "_" 

    print $str . "\n"; 
+0

其工作:)謝謝abhi –

+1

@ManjunathS:就這樣,如果你發現答案對你有幫助,接受它會被認爲是禮貌的。 – zx485