19

Let's say you have some lines that look like this

1  int some_function() {
2    int x = 3;  // Some silly comment

And so on. The indentation is done with spaces, and each indent is two spaces.

You want to change each indent to be three spaces. The simple regex

s/ {2}/   /g

Doesn't work for you, because that changes some non-indent spaces; in this case it changes the two spaces before // Some silly comment into three spaces, which is not desired. (This gets far worse if there are tables or comments aligned at the back end of the line.)

You can't simply use

/^( {2})+/

Because what would you replace it with? I don't know of an easy way to find out how many times a + was matched in a regex, so we have no idea how many altered indents to insert.

You could always go line-by-line and cut off the indents, measure them, build a new indent string, and tack it onto the line, but it would be oh so much simpler if there was a regex.

Is there a regular expression to replace indent levels as described above?

5
  • In perl, you can put the matched expression in the regex, so you could: s/^( {2})+/ $1/g;
    – Benj
    Commented Oct 9, 2012 at 20:38
  • @Benj right, but wouldn't that always output the indent with one more space? If you'd had twelve spaces you'd now have thirteen. I'd like each indent to grow to three spaces, so if you had twelve you should end up with eighteen.
    – So8res
    Commented Oct 9, 2012 at 20:40
  • I meant: what should happen to spaces in nested brackets? should they grow by 6 or by 3?
    – Gabber
    Commented Oct 9, 2012 at 20:45
  • @Gabber -- Each indent level should grow by the same amount. If something is three indent levels in then it is currently 6 spaces and afterwards should be 9 spaces.
    – So8res
    Commented Oct 9, 2012 at 20:47
  • Aha, now I understand, +1, good question!
    – Gabber
    Commented Oct 9, 2012 at 20:49

4 Answers 4

11

In some regex flavors, you can use a lookbehind:

s/(?<=^ *)  /   /g

In all other flavors, you can reverse the string, use a lookahead (which all flavors support) and reverse again:

 s/  (?= *$)/   /g
7
  • I can't get the replacing part, could you explain?
    – Gabber
    Commented Oct 9, 2012 at 21:01
  • 2
    Unlimited length lookbehind will work only in .NET as I know, but there it will work. +1 nice solution.
    – stema
    Commented Oct 9, 2012 at 21:01
  • @Gabber I'm replacing all pairs of spaces preceded only by spaces into triples of spaces. Note that matches may not overlap. Commented Oct 9, 2012 at 21:03
  • 2
    Works like a charm. Thanks! (@stema, vimscript has it too :D)
    – So8res
    Commented Oct 9, 2012 at 21:11
  • 1
    Good call. Shouldn't it be (?<=^ *) instead of +, though, to match the first indent level?
    – So8res
    Commented Oct 9, 2012 at 21:20
11

I needed to halve the amount of spaces on indentation. That is, if indentation was 4 spaces, I needed to change it to 2 spaces. I couldn't come up with a regex. But, thankfully, someone else did:

//search for
^( +)\1 
//replace with (or \1, in some programs, like geany)
$1 

From source: "^( +)\1 means "any nonzero-length sequence of spaces at the start of the line, followed by the same sequence of spaces. The \1 in the pattern, and the $1 in the replacement, are both back-references to the initial sequence of spaces. Result: indentation halved."

0
10

Here's another one, instead utilizing \G which has NET, PCRE (C, PHP, R…), Java, Perl and Ruby support:

s/(^|\G) {2}/   /g

\G [...] can match at one of two positions:
✽ The beginning of the string,
✽ The position that immediately follows the end of the previous match.

Source: http://www.rexegg.com/regex-anchors.html#G

We utilize its ability to match at the position that immediately follows the end of the previous match, which in this case will be at the start of a line, followed by 2 whitespaces (OR a previous match following the aforementioned rule).

See example: https://regex101.com/r/qY6dS0/1

0

You can try this:

^(\s{2})|((?<=\n(\s)+))(\s{2})

Breakdown:

^(\s{2})  = Searches for two spaces at the beginning of the line
((?<=\n(\s)+))(\s{2}) = Searches for two spaces
    but only if a new line followed by any number of spaces is in front of it.
    (This prevents two spaces within the line being replaced)

I'm not completely familiar with perl, but I would try this to see if it work:

s/^(\s{2})|((?<=\n(\s)+))(\s{2})/\s\s\s/g

As @Jan pointed out, there can be other non-space whitespace characters. If that is an issue, try this:

s/^( {2})|((?<=\n( )+))( {2})/   /g
2
  • \s is any whitespace, not just the space. Commented Oct 9, 2012 at 21:07
  • @JanDvorak I adjusted answer to account for that. I always have used \s, but I only use regex if I have complete control over what is inputted.
    – Nick
    Commented Oct 9, 2012 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.