我有以下代码:
#$domain = domainname.co.uk
#$root = public_html
#$webpage = domainname.co.uk/foo/bar/foobar.html
my $string = ($webpage =~ s/^$domain//g);
my $linkFromRoot = $dbh->quote($root . $string);
通常这工作正常,但由于某种原因,输出是“public_html 1”而不是“public_html/foo/bar/foobar.html”。
谁能看出为什么?
最佳答案
您没有得到正确的答案,因为替换返回 1,这是替换的项目数。见 perlfaq4对 How can I count the number of occurrences of a substring within a string? 的回答
$domain = "domainname.co.uk";
$root = "public_html";
$webpage = "domainname.co.uk/foo/bar/foobar.html";
my $string = ($webpage =~ s/^$domain//g);
print $string."\n";
删除
$string
就做 $webpage =~ s/^$domain//g;
然后用 $webpage
进行字符串连接.
关于regex - 为什么 Perl 用 "1"替换我的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2212599/