Remove all characters from string which are not on

2020-03-11 03:53发布

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.

2条回答
叼着烟拽天下
2楼-- · 2020-03-11 04:14

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').

查看更多
叼着烟拽天下
3楼-- · 2020-03-11 04:15

You could try and match everything that is not in your whitelist and replace it with an empty string:

String in = "asng $%& 123";
//this assumes your whitelist contains word characters and whitespaces, adapt as needed
System.out.println(in.replaceAll( "[^\\w\\s]+", "" )); 
查看更多
登录 后发表回答