在RegEx中将空格转换为制表符

sim*_*son 5 regex string

你怎么说正则表达式中的以下内容:

foreach line
   look at the beginning of the string and convert every group of 3 spaces to a tab
   Stop once a character other than a space is found
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止:

/^ +/\t/g
Run Code Online (Sandbox Code Playgroud)

但是,这会将每个空间转换为1个选项卡

任何帮助,将不胜感激.

Gre*_*con 7

使用Perl:

perl -pe '1 while s/\G {3}/\t/gc' input.txt >output.txt
Run Code Online (Sandbox Code Playgroud)

例如,使用以下输入

nada
   three spaces
    four spaces
   three   in the middle
      six space
Run Code Online (Sandbox Code Playgroud)

输出(替换为TAB \t)是

$ perl -pe '1 while s/\G {3}/\t/gc' input | perl -pe 's/\t/\\t/g'
nada
\tthree spaces
\t four spaces
\tthree   in the middle
\t\tsix spaces
Run Code Online (Sandbox Code Playgroud)