Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想替换字符串中某个数字的所有实例,但前提是该数字位于列以空格分隔的第 n 列中。
这是我到目前为止所拥有的:
$_ =~ s/\s+([^\s]+\s+){$numcols}$i(.*)\n/$rep/;
基本上,会有几个空格,然后会有:(非空格(列)后跟空格)为 $numcols 次。然后,会有$i,其中$i 是我要替换的数字,后跟一些我不关心的字符和一个换行符。但是,我不想用 $rep 替换 ALL OF THAT,而只想替换 $i。我怎么做?
像这样的东西:
$_ =~ s/^(\s+(?:[^\s]+\s+){$numcols})$i/$1$rep/;
我们将之前的所有内容$i作为一个组进行捕获(其中一个非捕获组表示为?:)。我们保留所有这些,替换 just $i,并单独留下字符串的其余部分。
$i
?: