I\'d like to be able to fix a Text File\'s tabs/spaces indentation.
Currently each line has spaces in random locations for some reason.
For example:
You could probably do this in one step, but I'm more partial to simple approaches.
Translate the 4 spaces to tabs first. First line is the match, second is the replace.
^(\s*)[ ]{4}(\s*)
$1\t$2
Then replace all remaining single spaces with nothing.
^(\t*)[ ]+
$1
You don't need the square brackets in this case, but it's a little hard to be sure that there's a space, even with SO's code formatting.
The first line searches for the start of the line ^
, then finds any amount of whitespace (including tabs) and puts them in a matching group later named $1
with (\s*)
. The middle finds exactly four spaces [ ]{4}
. The last part repeats the matching group in case there are tabs or more spaces on that side, too.
Since the second match is supposed to be finding all the remaining spaces, the second just looks for 0 or more tabs, puts them in a capture group, and then finds any remaining spaces left. Since it finds and replaces as it goes along, it gobbles up all spaces and replaces with tabs.