regex - 为什么 Perl 用 "1"替换我的字符串?

标签 regex perl string replace

我有以下代码:

#$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,这是替换的项目数。见 perlfaq4How 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/

相关文章:

java - 使用正则表达式模式的电话号码类别

regex - Makefile 'if' 条件与正则表达式

c# - SOAP 响应大小

java - 如何删除字符串中的括号字符(java)

用于用户名验证的 JavaScript 正则表达式

angularjs - 输入类型时间的正则表达式模式匹配

regex - 如何在正则表达式中使用不同的分隔符(/,|)

perl - 如何修剪文件 - 删除具有相同值的列

python - 转换日期格式保持其类型python

c - 在 C 中修剪字符串