So we have a database column that can contain just about anything. Unicode, numbers, words, etc.
However, we need to send that data to an external service and they are very strict on what they will accept. Basically, English only, words, numbers, etc.
We can’t change the requirement to this service so we need to filter what the users send us before we send to them.
My RegEx skills are weak. Here is what we need:
Characters [a-zA-Z]
Numbers [0-9]
The following symbols: !@#$%^&*()-_=+;:’,./?\
And that’s it. Of course, it can be in any combination. We just want to make sure nothing slips by that isn’t listed there.
Any help on how to construct this filter in Java would be greatly appreciated.
BTW, I’m assuming the same RegEx pattern would work for JavaScript?
EDIT
This is the example I have (using Edmastermind29):
public static void main(String[] args) {
String pattern = "^[A-Za-z0-9!@#$%^&*()-_=+;:',./?\\]";
String text = "Hello, I need everything in this string except the { or }";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);
// here, I need everything but the {}.
// so I should have: Hello, I need everything in this string except the or
}
OK, I figured out how to escape that string. But how do I get everything that isn’t filtered out?
You need to iterate of each matching subsequence and concatenate the strings.
With the given example the solution would look like this: