如何在 Perl 中用递增的数字替换令牌? [英] How do I replace a token with increasing numbers in Perl?
本文介绍了如何在 Perl 中用递增的数字替换令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用数字替换文本文件中的标记.令牌是
",我希望将其替换为到目前为止的计数数.例如:
I want to replace a token in a text file with a number. The token is "<count>
" and I want it to be replaced with the number of counts so far. For example:
This <count> is a <count> count.
The <count> count increases <count><count><count>.
<count><count><count><count><count><count>
变成:
This 1 is a 2 count.
The 3 count increases 456.
789101112
我不确定如何执行此操作,也许需要一些循环?
I'm not really sure how to do this, maybe with some loop?
my $text = (the input from file, already taken care of in my script);
my $count = 1;
while( regex not found? )
{
$text =~ s/<count>/($count);
$count ++;
}
推荐答案
my $text = "whatever";
my $count = 1;
$text =~ s/<count>/$count++/ge;
应该为你做.替换末尾的/e 使一切变得不同.
should do it for you. The /e at the end of the substitution makes all the difference.
这篇关于如何在 Perl 中用递增的数字替换令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文