删除字符串中不在白名单上的所有字符 [英] Remove all characters from string which are not on whitelist
问题描述
我想写的java代码,它会删除所有不需要的字符,让只有白名单。
示例:
String [] whitelist = { ,b,c...z,0...9,[,],...}
pre>
我想只有字母(小写和大写)和数字+一些我添加的下一个字符。
然后我会为字符串中的每个字符开始for()
循环,如果不在白名单上,则用空字符串替换它。
但这不是好的解决方案。也许可以使用pattern(regex)以某种方式完成?感谢。
解决方案是的,您可以使用
String.replaceAll
它需要一个正则表达式:String input =BAD good {} [];
String output = input.replaceAll([^ a-z0-9\\ [\\]],);
System.out.println(output); // good []
或在 Guava ,您可以使用
CharMatcher
:CharMatcher matcher = CharMatcher .inRange('a','z')
.or(CharMatcher.inRange('0','9'))
.or(CharMatcher.anyOf([]));
String input =BAD good {} [];
String output = matcher.retainFrom(input);
这只是显示小写版本,使其更容易演示。要包含大写字母,请在正则表达式中使用
[^ A-Za-z0-9\\ [\\]]
你可以用CharMatcher.inRange(' c>)
A','Z')
。I am trying to write java code which would remove all unwanted characters and let there be only whitelisted ones.
Example:
String[] whitelist = {"a", "b", "c"..."z", "0"..."9", "[", "]",...}
I want there only letters (lower and uppercase) and numbers + some next characters I would add. Then I would start
for()
cycle for every character in the string, and replace it with empty string if it isn't on whitelist.But that isn't good solution. Maybe it could be done somehow using pattern (regex)? Thanks.
解决方案Yes, you can use
String.replaceAll
which takes a regex:String input = "BAD good {} []"; String output = input.replaceAll("[^a-z0-9\\[\\]]", ""); System.out.println(output); // good[]
Or in Guava you could use a
CharMatcher
:CharMatcher matcher = CharMatcher.inRange('a', 'z') .or(CharMatcher.inRange('0', '9')) .or(CharMatcher.anyOf("[]")); String input = "BAD good {} []"; String output = matcher.retainFrom(input);
That just shows the lower case version, making it easier to demonstrate. To include upper case letters, use
"[^A-Za-z0-9\\[\\]]"
in the regex (and any other symbols you want) - and for theCharMatcher
you canor
it withCharMatcher.inRange('A', 'Z')
.这篇关于删除字符串中不在白名单上的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!