Reputation: 62549
i am searching for a regular expression i can use to check if a user input contains special characters in a specified list.
Here are the special characters not allowed by using a regular expression i tried to write: ^[`~!@#$%^&*()_+={}\[\]|\\:;“’<,>.?๐฿]*$
i went to https://regex101.com/ and i was expecting the following input to match but did it not why:
127 elmer road ??<>()
so in android java (but an be any ) i wrote the following function but it also always returns true . how can i filter all these special characters . I want a function that returns true if a given string does NOT match.
public boolean isValid( EditText et) {
String string = et.getText().toString();
boolean isValid = true;
final Pattern sPattern
= Pattern.compile("^[`~!@#$%^&*()_+={}\\[\\]|\\\\:;“’<,>.?๐฿]*$");
isValid= !sPattern.matcher(string).matches();
return isValid;
}
update: i tried the following also:
Upvotes: 2
Views: 15308
Reputation: 656
I want a function that returns true if a given string does NOT match.
You can negate the character set. (Note the ^
symbol within the square brackets). This will return true for strings that don't contain any of these special characters.
^[^`~!@#$%^&*()_+={}\[\]|\\:;“’<,>.?๐฿]*$
https://regex101.com/r/CqtqoK/1
Upvotes: 5