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:
^.*[~!@#$%^&()_+={}[\]|\:;“’<,>.?๐฿].*$
Note, you need to escape the]
.]
^
and$
in your regex?