Perl正则表达式将一个字符串的一部分替换为另一个字符串的一部分

时间:2015-10-23 04:33:44

标签: regex perl

我需要在Perl中用另一个大多数替换一个字符串的一部分。 :-)这需要对多对字符串进行。

例如,我需要替换

"/root_vdm_2/fs_clsnymigration"

/root_vdm_2/fs_clsnymigration/CLSNYMIGRATION/NY_HQ_S1

rfsn_clsnymigration

所以我最终得到了

/rfsn_clsnymigration/CLSNYMIGRATION/NY_HQ_S1

(没有领先的“/ root_vdm_2”部分)...但是我已经完全没有睡眠了,因此忽视了如何实现这一目标。

帮助?

3 个答案:

答案 0 :(得分:0)

试试这个正则表达式:

git rm –cached `git ls-files -i –exclude-standard`

代替:

^\/root_vdm_2\/fs_clsnymigration

示例:

\/rfsn_clsnymigration

输出:

$string = "/root_vdm_2/fs_clsnymigration/CLSNYMIGRATION/NY_HQ_S1";
$string=~s/^\/root_vdm_2\/fs_clsnymigration/\/rfsn_clsnymigration/;
print $string;

编辑1

/rfsn_clsnymigration/CLSNYMIGRATION/NY_HQ_S1

输出:

$string = "/root_vdm_2/fs_clsnymigration/CLSNYMIGRATION/NY_HQ_S1/LISU,rfsn_clsnymigration
/root_vdm_2/fs_users/users/Marketing,rfsw_users
/root_vdm_3/fs_sandi/sandi_users,rfsw_sandi
/root_vdm_3/fs_pci/Analytics,rfsw_pci
/root_vdm_4/fs_camnt01/camnt01/AV,rfsw_camnt01
/root_vdm_1/fs_stcloud01/sfa,rfss_stcloud01
/root_vdm_3/fs_stcloud03/data4,rfss_stcloud03
/root_vdm_2/fs_stcloud02/depart1,rfss_stcloud02";
$string=~s/^\/root_vdm_.\/fs_[^\/]*/\/rfsn_clsnymigration/gm;
print $string;

答案 1 :(得分:0)

use strict;
use warnings;

while (<DATA>) {
    chomp;
    my ($lhs, $rhs) = split(/,/, $_, 2);
    my @parts = split(/\//, $lhs);
    splice(@parts, 1, 2, $rhs);
    print join('/', @parts) . "\n";
}

__DATA__
/root_vdm_2/fs_clsnymigration/CLSNYMIGRATION/NY_HQ_S1/LISU,rfsn_clsnymigration
/root_vdm_2/fs_users/users/Marketing,rfsw_users
/root_vdm_3/fs_sandi/sandi_users,rfsw_sandi
/root_vdm_3/fs_pci/Analytics,rfsw_pci
/root_vdm_4/fs_camnt01/camnt01/AV,rfsw_camnt01
/root_vdm_1/fs_stcloud01/sfa,rfss_stcloud01
/root_vdm_3/fs_stcloud03/data4,rfss_stcloud03
/root_vdm_2/fs_stcloud02/depart1,rfss_stcloud02

答案 2 :(得分:-1)

我的挑战是将$ string1的 part 替换为所有$ string2,用逗号分隔。

/root_vdm_2/fs_clsnymigration/CLSNYMIGRATION/NY_HQ_S1/LISU,rfsn_clsnymigration
/root_vdm_2/fs_users/users/Marketing,rfsw_users
/root_vdm_3/fs_sandi/sandi_users,rfsw_sandi
/root_vdm_3/fs_pci/Analytics,rfsw_pci
/root_vdm_4/fs_camnt01/camnt01/AV,rfsw_camnt01
/root_vdm_1/fs_stcloud01/sfa,rfss_stcloud01
/root_vdm_3/fs_stcloud03/data4,rfss_stcloud03
/root_vdm_2/fs_stcloud02/depart1,rfss_stcloud02

我最初看到的困难是如何用/root_vdm_2/fs_clsnymigration替换rfsn_clsnymigration,我允许自己认为正则表达式是最好的方法。

虽然不那么雄辩,但这可以完成工作:

foreach $line (@lines) {
        chop $line;
        ($orig,$replica) = split /\,/, $line;
        chop substr $orig, 0, 1;
        @pathparts = split /\//, $orig;
        $rootvdm = shift @pathparts;
        @pathparts[0] = $replica;
        $newpath = "/" . join ('/', @pathparts);
        print "     here's \$newpath:$newpath\n";
        }

......产生类似这样的东西:

     here's $newpath:/rfsn_clsnymigration/CLSNYMIGRATION/NY_HQ_S1/LISU
     here's $newpath:/rfsw_users/users/Marketing
     here's $newpath:/rfsw_sandi/sandi_users
     here's $newpath:/rfsw_pci/Analytics
     here's $newpath:/rfsw_camnt01/camnt01/AV
     here's $newpath:/rfss_stcloud01/sfa
     here's $newpath:/rfss_stcloud03/data4
     here's $newpath:/rfss_stcloud02/depart1