这实际上是CSV解析器的工作,但如果它必须是正则表达式,并且您从未在引用的CSV条目中有选项卡,则可以搜索
(^|\t)(?=\t|$)
Run Code Online (Sandbox Code Playgroud)
并替换为
$10
Run Code Online (Sandbox Code Playgroud)
所以,在Perl中:
(ResultString = $subject) =~
s/( # Match either...
^ # the start of the line (preferably)
| # or
\t # a tab character
) # remember the match in backreference no. 1
(?= # Then assert that the next character is either
\t # a(nother) tab character
| # or
$ # the end of the line
) # End of lookahead assertion
/${1}0/xg;
Run Code Online (Sandbox Code Playgroud)
这将改变
1 2 4 7 8
2 3 5 6 7
Run Code Online (Sandbox Code Playgroud)
成
1 2 0 4 0 0 7 8
0 2 3 0 5 6 7 0
Run Code Online (Sandbox Code Playgroud)
对于制表符分隔的文件,此AWK代码段可以解决这个问题:
BEGIN { FS = "\t"; OFS="\t" }
{
for(i = 1; i <= NF; i++) {
if(!$i) { $i = 0 }
}
print $0
}
Run Code Online (Sandbox Code Playgroud)