我知道我在这里做了一些愚蠢的事情,但我很累而且我显然只是没有看到它。我有以下脚本:
#!/usr/bin/perl
use strict;
use warnings;
my @names = (
"John Q. Public",
"James K Polk"
);
foreach (@names)
{
print "Before: $_\n";
s/\b[A-Z]\.?\b//;
print "After: $_\n";
}
当我运行这个脚本时,我得到以下输出:
Before: John Q. Public
After: John . Public <== Why is the period still here?
Before: James K Polk
After: James Polk
请注意,在John Q. Public示例中,保留了句点。可选的匹配参数 ( ?
) 不是贪婪的吗?根据perlre 文档:
? 匹配 1 或 0 次
句号不应该和中间的首字母一起消失吗?我在这里想念什么?