[java] Regex pattern including all special characters

I want to write a simple regular expression to check if in given string exist any special character. My regex works but I don't know why it also includes all numbers, so when I put some number it returns an error.

My code:

//pattern to find if there is any special character in string
Pattern regex = Pattern.compile("[$&+,:;=?@#|'<>.-^*()%!]");
//matcher to find if there is any special character in string
Matcher matcher = regex.matcher(searchQuery.getSearchFor());

if(matcher.find())
{
    errors.rejectValue("searchFor", "wrong_pattern.SearchQuery.searchForSpecialCharacters","Special characters are not allowed!");
}

This question is related to java regex

The answer is


Here is my regex variant of a special character:

String regExp = "^[^<>{}\"/|;:.,~!?@#$%^=&*\\]\\\\()\\[¿§«»??¤°??€¥£¢¡®©0-9_+]*$";

(Java code)


That's because your pattern contains a .-^ which is all characters between and including . and ^, which included digits and several other characters as shown below:

enter image description here

If by special characters, you mean punctuation and symbols use:

[\p{P}\p{S}]

which contains all unicode punctuation and symbols.


You have a dash in the middle of the character class, which will mean a character range. Put the dash at the end of the class like so:

[$&+,:;=?@#|'<>.^*()%!-]

Use this regular expression pattern ("^[a-zA-Z0-9]*$") .It validates alphanumeric string excluding the special characters


If you only rely on ASCII characters, you can rely on using the hex ranges on the ASCII table. Here is a regex that will grab all special characters in the range of 33-47, 58-64, 91-96, 123-126

[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]

However you can think of special characters as not normal characters. If we take that approach, you can simply do this

^[A-Za-z0-9\s]+

Hower this will not catch _ ^ and probably others.


Please use this.. it is simplest.

\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[]^_`{|}~

https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

    StringBuilder builder = new StringBuilder(checkstring);
    String regex = "\\p{Punct}"; //Special character : `~!@#$%^&*()-_+=\|}{]["';:/?.,><
    //change your all special characters to "" 
    Pattern  pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(builder.toString());
    checkstring=matcher.replaceAll("");

Try:

(?i)^([[a-z][^a-z0-9\\s\\(\\)\\[\\]\\{\\}\\\\^\\$\\|\\?\\*\\+\\.\\<\\>\\-\\=\\!\\_]]*)$

(?i)^(A)$: indicates that the regular expression A is case insensitive.

[a-z]: represents any alphabetic character from a to z.

[^a-z0-9\\s\\(\\)\\[\\]\\{\\}\\\\^\\$\\|\\?\\*\\+\\.\\<\\>\\-\\=\\!\\_]: represents any alphabetic character except a to z, digits, and special characters i.e. accented characters.

[[a-z][^a-z0-9\\s\\(\\)\\[\\]\\{\\}\\\\^\\$\\|\\?\\*\\+\\.\\<\\>\\-\\=\\!\\_]]: represents any alphabetic(accented or unaccented) character only characters.

*: one or more occurrence of the regex that precedes it.


We can achieve this using Pattern and Matcher as follows:

Pattern pattern = Pattern.compile("[^A-Za-z0-9 ]");
Matcher matcher = pattern.matcher(trString);
boolean hasSpecialChars = matcher.find();

I have defined one pattern to look for any of the ASCII Special Characters ranging between 032 to 126 except the alpha-numeric. You may use something like the one below:

To find any Special Character:

[ -\/:-@\[-\`{-~]

To find minimum of 1 and maximum of any count:

(?=.*[ -\/:-@\[-\`{-~]{1,})

These patterns have Special Characters ranging between 032 to 047, 058 to 064, 091 to 096, and 123 to 126.


For people (like me) looking for an answer for special characters like Ä etc. just use this pattern:

  • Only text (or a space): "[A-Za-zÀ-? ]"

  • Text and numbers: "[A-Za-zÀ-?0-9 ]"

  • Text, numbers and some special chars: "[A-Za-zÀ-?0-9(),-_., ]"

Regex just starts at the ascii index and checks if a character of the string is in within both indexes [startindex-endindex].

So you can add any range.

Eventually you can play around with a handy tool: https://regexr.com/

Good luck;)


To find any number of special characters use the following regex pattern: ([^(A-Za-z0-9 )]{1,})

[^(A-Za-z0-9 )] this means any character except the alphabets, numbers, and space. {1,0} this means one or more characters of the previous block.


Here is my regular expression, that I used for removing all the special characters from any string :

String regex = ("[ \\\\s@  [\\\"]\\\\[\\\\]\\\\\\\0-9|^{#%'*/<()>}:`;,!& .?_$+-]+")

SInce you don't have white-space and underscore in your character class I think following regex will be better for you:

Pattern regex = Pattern.compile("[^\w\s]");

Which means match everything other than [A-Za-z0-9\s_]

Unicode version:

Pattern regex = Pattern.compile("[^\p{L}\d\s_]");

You can use a negative match:

Pattern regex = Pattern.compile("([a-zA-Z0-9])*"); (For zero or more characters)

or

Pattern regex = Pattern.compile("([a-zA-Z0-9])+"); (For one or more characters)


(^\W$)

^ - start of the string, \W - match any non-word character [^a-zA-Z0-9_], $ - end of the string


Try using this for the same things - StringUtils.isAlphanumeric(value)


Try this. It works on C# it should work on java also. If you want to exclude spaces just add \s in there @"[^\p{L}\p{Nd}]+"