How do I remove trailing whitespace using a regular expression?
Asked Answered
F

12

94

I want to remove trailing white spaces and tabs from my code without removing empty lines.

I tried:

\s+$

and:

([^\n]*)\s+\r\n

But they all removed empty lines too. I guess \s matches end-of-line characters too.


UPDATE (2016):

Nowadays I automate such code cleaning by using Sublime's TrailingSpaces package, with custom/user setting:

"trailing_spaces_trim_on_save": true

It highlights trailing white spaces and automatically trims them on save.

Fredkin answered 2/3, 2012 at 11:12 Comment(4)
Your guess is correct. \s matches all kinds of whitespace characters.Cavit
The corresponding Vi and Vim question is What's the simplest way to strip trailing whitespace from all lines in a file?Larousse
Here is a doc entry on topic.Absolution
@Filipp W: For VimLarousse
C
173

Try just removing trailing spaces and tabs:

[ \t]+$
Churchly answered 2/3, 2012 at 11:15 Comment(8)
How would I exclude lines which contain only whitespaces, tabs or a mixture of them?Autocatalysis
@DanielF. ([^ \t])[ \t]+$, but you'd have to replace with \1 instead of an empty string.Valenevalenka
@Koviko Thanks, but what I meant was that lines which contain only whitespaces or tabs, like indented functions in Python, where indentation is important, should be left as they are. I don't see the suggested regex doing that.Autocatalysis
@DanielF. The regex I posted looks for non-space characters preceding the space characters, and preserves the non-space characters with the \1 replacement. Lines that consist of only space characters will not match this regex and will be skipped.Valenevalenka
@Koviko This should showcase the issue: jsfiddle.net/s7vW5/2 .The whitespace-only line between foo and bar should contain two spaces. Python's flavor of regex behaves the same.Autocatalysis
@DanielF. In my case, I was using a text editor (Notepad++) that counts each line as a string, not the entire document as a string. As such, newline characters are ignored. To handle the case where newline characters are not ignored, simply add them to the regex: ([^ \t\r\n])[ \t]+$Valenevalenka
@Koviko: Since your comment is the correct solution to the problem and the answer you've commented on is not, I suggest you post that regex as a separate answer. Your comment is valuable, but easy to miss.Pilsudski
For Atom, it's [ \t]+$Expurgatory
V
39

To remove trailing whitespace while also preserving whitespace-only lines, you want the regex to only remove trailing whitespace after non-whitespace characters. So you need to first check for a non-whitespace character. This means that the non-whitespace character will be included in the match, so you need to include it in the replacement.

Regex: ([^ \t\r\n])[ \t]+$

Replacement: \1 or $1, depending on the IDE

Valenevalenka answered 29/1, 2014 at 4:13 Comment(7)
Awesome. Thank you. Also, the same regex technique modified to use a lookbehind assertion: (?<![ \t\r\n])[ \t]+$ (replacement pattern should just be blank).Pilsudski
What platform uses "\1" as replacement?Larousse
@PeterMortensen Notepad++, for one.Valenevalenka
@PeterMortensen sed too.Locomotive
This is a built-in function in Notepad++: Menu EditBlank OperationsTrim Trailing SpaceLarousse
@PeterMortensen That will not preserve whitespace-only lines, as in the first line of my answer.Valenevalenka
None of the answers here appear to handle when the text before the trailing spaces contains spaces itself, e.g. "E:\Data\Shared\SCANNED DOCUMENTS "Pahl
L
19

The platform is not specified, but in C# (.NET) it would be:

Regular expression (presumes the multiline option - the example below uses it):

    [ \t]+(\r?$)

Replacement:

    $1

For an explanation of "\r?$", see Regular Expression Options, Multiline Mode (MSDN).

Code example

This will remove all trailing spaces and all trailing TABs in all lines:

string inputText = "     Hello, World!  \r\n" +
                   "  Some other line\r\n" +
                   "     The last line  ";
string cleanedUpText = Regex.Replace(inputText,
                                     @"[ \t]+(\r?$)", @"$1",
                                     RegexOptions.Multiline);
Larousse answered 31/5, 2015 at 15:22 Comment(2)
The regular expressions in the other two answers do not work in .NET (they don't result in any replacement - the text is left unchanged).Larousse
This also works in Xcode and Swift. Thumbs way up. Thank you.Zacharie
E
4

Regex to find trailing and leading whitespaces:

^[ \t]+|[ \t]+$
Edette answered 18/1, 2018 at 6:6 Comment(1)
Can you provide some context? Where and how did you test it?Larousse
L
3

If using Visual Studio 2012 and later (which uses .NET regular expressions), you can remove trailing whitespace without removing blank lines by using the following regex

Replace (?([^\r\n])\s)+(\r?\n)

With $1

Enter image description here


Some explanation

The reason you need the rather complicated expression is that the character class \s matches spaces, tabs and newline characters, so \s+ will match a group of lines containing only whitespace. It doesn't help adding a $ termination to this regex, because this will still match a group of lines containing only whitespace and newline characters.

You may also want to know (as I did) exactly what the (?([^\r\n])\s) expression means. This is an Alternation Construct, which effectively means match to the whitespace character class if it is not a carriage return or linefeed.

Alternation constructs normally have a true and false part,

(?( expression ) yes | no )

but in this case the false part is not specified.

Ligula answered 13/7, 2018 at 10:46 Comment(0)
C
3

[ |\t]+$ with an empty replace works.

\s+($) with a $1 replace also works, at least in Visual Studio Code...

Contempt answered 10/12, 2018 at 21:36 Comment(0)
B
1

If you want to remove trailing whitespace from lines that have some text, but not just blank lines, here is regex to find: (\S+)(\s+)$ and replace with $1. It finds trailing text as first group, then trailing whitespace just after text as second group. Then replaces both groups by just the first group.

Benighted answered 16/2 at 12:59 Comment(0)
K
0

To remove trailing white space while ignoring empty lines I use positive look-behind:

(?<=\S)\s+$

The look-behind is the way go to exclude the non-whitespace (\S) from the match.

Koski answered 16/4, 2016 at 23:46 Comment(2)
This matches empty lines. The match will start at the end of a non-empty line, but it will include any empty lines that follow.Catena
What platform did you use? Perl?Larousse
K
0

To remove any blank trailing spaces use this:

\n|^\s+\n

I tested in the Atom and Xcode editors.

Krone answered 9/4, 2019 at 10:46 Comment(2)
What replacement character did you use? $1? Or something else?Larousse
An empty string as the replacement?Larousse
T
0

Strangely, I could not get the /[\t ]+$/m to work in PHP. I tried variants of the other answers here, but still failed.

So, I went with this replacement instead:

<?php
$contents = preg_replace('/[\t ]+(\v)/', '$1', $contents);
Triangulation answered 28/3, 2023 at 14:0 Comment(0)
A
-1

In Java:



String str = "    hello world  ";

// prints "hello world" 
System.out.println(str.replaceAll("^(\\s+)|(\\s+)$", ""));


Aviator answered 29/1, 2019 at 6:33 Comment(1)
The OP didn't want to remove leading spaces.Northernmost
C
-4

You can simply use it like this:

var regex = /( )/g;

Sample: click here

Comitative answered 12/2, 2019 at 5:17 Comment(2)
Hi @PeterMortensen, it will remove all white spacesComitative
And what was the question?Larousse

© 2022 - 2024 — McMap. All rights reserved.