我是Perl的新手。我试图做一个简单的字符串替换,但不能成功。
#!/usr/bin/perl
$var = "M4S120_appscan";
$var1 = "SCANS";
$path =~ s/$var/$var1/;
print "Path is $path"
输出应为"Path is SCANS"
,但在“输出”
答案 0 :(得分:4)
将“M4S120_appscan”替换为字符串中的“SCANS”:
$str = "Path is M4S120_appscan";
$find = "M4S120_appscan";
$replace = "SCANS";
$str =~ s/$find/$replace/;
print $str;
如果这是你想要的。
答案 1 :(得分:0)
正则表达式operator =〜有三个参数:在我的例子中,我们要替换的字符串是$ path变量,搜索项($ var)和替换$ var1。你可以看到你试图替换" M4S120_appscan"到" SCANS"在一个空字符串内,因为$ path未初始化。您需要在替换之前初始化$ path,例如:
$path="M4S120_appscan";
答案 2 :(得分:0)
替换是一种注册搜索和替换。请关注@Thilo
$var = "M4S120_appscan";
$var =~ s/M.+\_.+can/SCANS/g; #/g replace all matches
print "path is $var";