将 space-indent 替换为制表符 (PCRE)

Replace space-indent with tabs (PCRE)

我想演示一下我可以用制表符替换空格缩进,但只能替换每行的前导空格。

我想我可以通过可变宽度后视来实现这一点。不幸的是,这在 PCRE 中是不允许的。

s/(?<=^|(?:[ ]{3})+)[ ]{3}(?=\S|(?:[ ]{3})+\S)/\t/g

还有其他方法吗?

当然,大意是不要重复代入做循环。这太容易了...

这是一个随机输入的示例,其中必须将 2 个空格转换为制表符:

  if (c == 1&d == 2){
    for (uinc = 1;uinc< = p1;uinc++){  // for fixed u calculate various w's
      if (1.0-u<5e-6)u = 1.0; // fix up the u = 1 value because of float
      w = 0.0;
      for (winc = 1;winc< = p2;winc++){
        if (1.0-w<5e-6)w = 1.0; // fix up the w = 1 value because of float
        for (i = 0;i< = n;i++){
          jin = Basis(n,i,u); // Bernstein basis function in the u direction (see Eq.(5.2))
          if (jin! = 0.){ // don't bother no contribution
            jbas = 3*(m+1)*i; /* column index for lineal array*/
            for (j = 0;j< = m;j++){
              kjm = Basis(m,j,w);  // Bernstein basis function in the w direction (see Eq.(5.2))
              if (kjm! = 0.){ // don't bother no contribution
                j1 = jbas+3*j+1;
                q[icount] = q[icount]+b[j1]*jin*kjm; // calculate the surface points
                q[icount+1] = q[icount+1]+b[j1+1]*jin*kjm;
                q[icount+2] = q[icount+2]+b[j1+2]*jin*kjm;
              }
            }
          }
        }
        icount += 3;
        w = w+stepw;
      }
      u += stepu;
    }
  }
}
s/(?:\G|^) {2}/\t/mg

2替换为4 4个空格等

\G 匹配上一个匹配停止的地方(如果没有上一个匹配,则在字符串的开头匹配)。