cbmeeks
cbmeeks

Reputation: 11420

How do I filter out everything except words, numbers, and a few symbols in Java?

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?

Upvotes: 1

Views: 1964

Answers (3)

gumble42
gumble42

Reputation: 423

You need to iterate of each matching subsequence and concatenate the strings. With the given example the solution would look like this:

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 );
  StringBuilder sb = new StringBuilder();
  while( m.find() )
  {
     sb.append( m.group() );
  }
  String result = sb.toString();
  System.out.println( "Result: '" + result + "'" );
}

Upvotes: 3

Spyros Mandekis
Spyros Mandekis

Reputation: 1024

In Javascript, you can just construct an onSubmit function that matches the Regular Expression /^[A-Za-z0-9!@#$%^&*()-_=+;:',./?\\]$/ to the input and not allow the form to be submitted if it returns a false.

Note that you must escape the "\" character

Upvotes: 1

user5398447
user5398447

Reputation:

[A-Za-z0-9!@#$%^&*()\-_=+;:',./?\\]*

Accepts any number of uppercase and lowercase characters, any number of digits, and any number the following symbols !@#$%^&*()-_=+;:',./?\ as stated in your post.

Upvotes: 2

Related Questions