开发者

Removing all whitespace characters except for " "

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 woul开发者_运维百科d 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.


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.


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.


There's no subtraction of character classes in Java, otherwise you could use [\s--[ ]], note the double dash. You can always simulate set subtraction using intersection with the complement, so

[\s&&[^ ]]

should work. It's no better than [^\S ]+ from the first answer, but the principle is different and it's good to know both.


I solved it with this:

anyString.replace(/[\f\t\n\v\r]*/g, '');

It is just a collection of all possible white space characters excluding blank (so actually \s without blanks). It includes tab, carriage return, new line, vertical tab and form feed characters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜