5.12. Trim Leading and Trailing Whitespace

Problem

You want to remove leading and trailing whitespace from a string.

Solution

To keep things simple and fast, the best all-around solution is to use two substitutions—one to remove leading whitespace, and another to remove trailing whitespace:

^\s+
Regex options: None (“^ and $ match at line breaks” must not be set)
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby
\s+$
Regex options: None (“^ and $ match at line breaks” must not be set)
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Simply replace matches found using both of these regular expressions with the empty string. Recipe 3.14 shows you how. With both regular expressions, you only need to replace the first match found since they match all leading or trailing whitespace in one go.

Discussion

Removing leading and trailing whitespace is a simple but common task. The two regular expressions just shown each contain three parts: an anchor to assert position at the beginning or end of the string (^ and $, respectively), the shorthand character class to match any whitespace character (\s), and the quantifier to repeat the class one or more times (+).

Many programming languages provide a function, usually called trim or strip, that can remove leading and trailing whitespace for you. Table 5-2 shows how to use this built-in function or method in a variety of programming languages.

Table 5-2. Standard functions to remove leading and trailing whitespace

Programming ...

Get Regular Expressions Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.