删除字符串中不在白名单上的所有字符 [英] Remove all characters from string which are not on whitelist

查看:150
本文介绍了删除字符串中不在白名单上的所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写的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 the CharMatcher you can or it with CharMatcher.inRange('A', 'Z').

这篇关于删除字符串中不在白名单上的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆