5.12. Trim Leading and Trailing Whitespace
Problem
You want to remove leading and trailing whitespace from a string. For instance, you might need to do this to clean up data submitted by users in a web form before passing their input to one of the validation regexes in Chapter 4.
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.
Leading whitespace:
\A\s+
Regex options: None |
Regex flavors: .NET, Java, 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 |
Trailing whitespace:
\s+\Z
Regex options: None |
Regex flavors: .NET, Java, 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 |
Simply replace matches found using one of the “leading whitespace” regexes and one of the “trailing whitespace” regexes with the empty string. Follow the code in Recipe 3.14 to perform replacements. With both the leading and trailing whitespace regular expressions, you only need to replace the first match found since the regexes match all leading or trailing whitespace in one go.
Discussion
Removing leading and trailing whitespace is a simple but common
task. The regular expressions just shown contain three parts each: the
shorthand character class to match any whitespace character (‹\s
›), a quantifier ...
Get Regular Expressions Cookbook, 2nd Edition 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.