XML technologies / Regular expressions / Practical regex techniques / A few short examples / Removing leading and trailing whitespace
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Removing whitespace from the beginning and end of a string is hardly a major challenge, but the problem keeps recurring. By far the best solution is the simple, obvious one:
s/^\s+//;
s/\s+$//;As a small optimization, ˹+˼ is used here instead of ˹*˼, since it is better not to perform the substitution at all when there is nothing to remove.
For some reason, finding a solution that does both in one step seems to be the challenge. I do not recommend this, but examining how such approaches work and why they are inadvisable is instructive.
s/\s*(.*?)\s*$/$1/sWhen non-greedy quantifiers were new, this was the showcase example; since then, people have realized that this solution is much slower than the simple one (in Perl about five times slower). This is because each character requires testing whether what follows
*?can match. This requires a great many Backtracking operations.s/^\s*((?:.*\S)?)\s*$/$1/sThis looks much more complicated than the preceding solution, but matching proceeds much more directly and is now only about twice as slow as the canonical solution.
˹^\s*˼skips the whitespace at the beginning, and the˹.*˼in the middle matches all characters through the end of the string.˹\S˼forces backtracking to the last non-whitespace character. The trailing whitespace is matched by˹\s*$˼outside the capturing parentheses.The question mark is necessary for the expression to work on lines containing only whitespace. Without it, the regex would fail, leaving the blank lines intact.
s/^\s*|\s*$//gThis is a frequently suggested solution. It is not wrong (none of the solutions presented here is wrong), but alternation prevents several optimizations that would otherwise be possible.
The
/gmodifier is used so that both alternatives can match, but/gseems excessive when we know that at most two matches, one for each alternative, are possible. This solution is also quite slow, about four times slower than the simple one.
The speed of these methods often depends on the data. In rare cases, with very long strings and little whitespace at either end, the penultimate method may be slightly faster than the simple one. In my programs, however, I use
s/^\s+//;
s/\s+$//;or an equivalent formulation in the programming language being used, because it is almost always faster and unquestionably expresses the intention much more clearly.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln
back to top