简体   繁体   English

删除除“ ”之外的所有空白字符

[英]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 ' ' .在 Java 中,我尝试过的正则表达式是: [\\s-[ ]] ,但这个也去掉了' '

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.编辑此帖子时,它们显示为一个长空格,但在查看模式下查看为单个空格。

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, ie either a space or anything that isn't a whitespace.正则表达式[\\S ]匹配您要保留的字符,即空格或任何不是空格的字符。 The negated character class [^\\S ] therefore must match all the characters you want to remove.因此,否定字符类[^\\S ]必须匹配您要删除的所有字符。

Using a Guava CharMatcher :使用番石榴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 .如果您实际上只是希望从字符串的开头和结尾进行修剪(例如String.trim() ),您可以使用trimFrom而不是removeFrom

There's no subtraction of character classes in Java, otherwise you could use [\\s--[ ]] , note the double dash. Java 中没有减去字符类,否则您可以使用[\\s--[ ]] ,注意双破折号。 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.它并不比第一个答案中的[^\\S ]+ ,但原理不同,两者都知道是好的。

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).它只是不包括空格的所有可能的空白字符的集合(所以实际上 \\s 没有空格)。 It includes tab, carriage return, new line, vertical tab and form feed characters.它包括制表符、回车符、换行符、垂直制表符和换页符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM