sadfsadf! Ghfgh% fgh65 %%
真正的任务是替换正则表达式中正确的前3个字符,因此将其替换为'o'sadfsadfoghfghofgh65o%
#!/usr/bin/perl -w
@list=<>;
chomp(@list);
foreach(@list) {
if($_ =~ m/\W/) {
# here is the problem because all the characters you find it
# overwrite it, but I only need to translate 3 characters from it
$_ =~ s/\W/o/g;
print $_."\n";
}
else {
print "->\n";
}
}
#start string => sadfsadf!ghfgh%fgh65%%
#result my program => sadfsadfoghfghofgh65oo
#and I need it => sadfsadfoghfghofgh65o%
仅将前3个结果更改为“ o”
答案 0 :(得分:3)
这是一条路:
use feature 'say';
my $in = 'sadfsadf!ghfgh%fgh65%%';
$in =~ s/\W/o/ for 1..3;
say $in;
输出:
sadfsadfoghfghofgh65o%
答案 1 :(得分:0)
解决了问题 代码:
#!/usr/bin/perl -w
while ($be=<STDIN>)
{
chomp $be;
push (@list, $be);
}
foreach $a (@list)
{
$count=0;
if ($a=~/\W/)
{
while (($a=~m/\W/g) && ($count < 3))
{
$count++;
$a=~s/\W/o/;
}
print "$a\n";
}
else {print "->\n";}
}
答案 2 :(得分:0)
您在“列表”文件中的数据
perl -pe 'for $i(1..3){s/\W/o/} ' list
如果您将list='sadfsadf!ghfgh%fgh65%%'
这样的数据放在bash上
for((i=1;i<=3;i++)){ list=`echo $list|sed -E "s/\W/Y/"`; }