在 RegEx 中将空格转换为制表符 [英] Convert spaces to tabs in RegEx

查看:45
本文介绍了在 RegEx 中将空格转换为制表符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的正则表达式怎么说:

How do do you say the following in regex:

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

这是我目前所拥有的:

/^ +/\t/g

但是,这会将每个空格转换为 1 个制表符

However, this converts every space to 1 tab

任何帮助将不胜感激.

推荐答案

使用 Perl:

perl -pe '1 while s/\G {3}/\t/gc' input.txt >output.txt

例如,使用以下输入

nada
   three spaces
    four spaces
   three   in the middle
      six space

输出(由 \t 替换的 TAB)是

the output (TABs replaced by \t) is

$ 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

这篇关于在 RegEx 中将空格转换为制表符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆