用正则弦 - 打印$ 2两次
我有代码:
#!/bin/env perl
use warnings;
$line = 'GFHFHDSH4567 FHGFF 687686';
print $line, "\n";
$line =~ s/ +/ /g;
$line =~ s/^(.+)(?=\s+(\d+)$)/1:$1\t2:$2/g;
print $line, "\n";
我得到:
GFHFHDSH4567 FHGFF 687686
1:GFHFHDSH4567 FHGFF 2:687686 687686
为什么要打印两次?
I have the code:
#!/bin/env perl
use warnings;
$line = 'GFHFHDSH4567 FHGFF 687686';
print $line, "\n";
$line =~ s/ +/ /g;
$line =~ s/^(.+)(?=\s+(\d+)$)/1:$1\t2:$2/g;
print $line, "\n";
I get:
GFHFHDSH4567 FHGFF 687686
1:GFHFHDSH4567 FHGFF 2:687686 687686
Why it printed twice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的正则态度包括一个看上去的断言。这并不构成最终匹配的字符串的一部分,因此,$行的第一部分'gfhfhdsh4567'被替换为替换字符串'1:gfhfhdsh4567 fhgff 2:687686''原始'687686'的原始'687686'被剩下无关。
Your regex includes a look-ahead assertion. This doesn't form part of the final matched string, so only the first part of $line, 'GFHFHDSH4567 ', is replaced with the replacement string '1:GFHFHDSH4567 FHGFF 2:687686' The original '687686' is left unchanged.