Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing all whitespace characters except for " "

Tags:

java

regex

I consider myself pretty good with Regular Expressions, but this one is appearing to be surprisingly tricky: I want to trim all whitespace, except the space character: ' '.

In Java, the RegEx I have tried is: [\s-[ ]], but this one also strips out ' '.

UPDATE:

Here is the particular string that I am attempting to strip spaces from:

project team                manage key

Note: it would be the characters between "team" and "manage". They appear as a long space when editing this post but view as a single space in view mode.

like image 889
Ryan Delucchi Avatar asked Feb 04 '11 23:02

Ryan Delucchi


2 Answers

Try using this regular expression:

[^\S ]+

It's a bit confusing to read because of the double negative. The regular expression [\S ] matches the characters you want to keep, i.e. either a space or anything that isn't a whitespace. The negated character class [^\S ] therefore must match all the characters you want to remove.

like image 169
Mark Byers Avatar answered Oct 07 '22 01:10

Mark Byers


Using a Guava CharMatcher:

String text = ...
String stripped = CharMatcher.WHITESPACE.and(CharMatcher.isNot(' '))
    .removeFrom(text);

If you actually just want that trimmed from the start and end of the string (like String.trim()) you'd use trimFrom rather than removeFrom.

like image 33
ColinD Avatar answered Oct 07 '22 00:10

ColinD