nowox
nowox

Reputation: 29096

Replace space-indent with tabs (PCRE)

I would like to demonstrate what I can replace spaces indent with tabs, but only the leading spaces of each lines.

I guess I can achieve this with a variable width look-behind. Unfortunately this is not allowed in PCRE.

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

Is there another way to do it?

Of course, the general idea is not to do a loop by repeating the substitution. This would be too easy...

Here is an example of a random input where one must converts 2-spaces into a tab:

  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;
    }
  }
}

Upvotes: 2

Views: 264

Answers (1)

melpomene
melpomene

Reputation: 85767

s/(?:\G|^) {2}/\t/mg

Replace 2 by 4 for 4 spaces, etc.

\G matches where the previous match stopped (if there is no previous match, it matches at the beginning of the string).

Upvotes: 7

Related Questions