Regex pattern including all special characters

JavaRegex

Java Problem Overview


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!");
}

Java Solutions


Solution 1 - Java

Please don't do that... little Unicode BABY ANGELs like this one  are dying! ◕◡◕ (← these are not images) (nor is the arrow!)

And you are killing 20 years of DOS :-) (the last smiley is called WHITE SMILING FACE... Now it's at 263A... But in ancient times it was ALT-1)

and his friend

BLACK SMILING FACE... Now it's at 263B... But in ancient times it was ALT-2

Try a negative match:

Pattern regex = Pattern.compile("[^A-Za-z0-9]");

(this will ok only A-Z "standard" letters and "standard" 0-9 digits.)

Solution 2 - Java

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:

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

Solution 3 - Java

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.

Solution 4 - Java

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_]");

Solution 5 - Java

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;)

Solution 6 - Java

Use this to catch the common special characters excluding .-_.

/[!"`'#%&,:;<>=@{}~\$\(\)\*\+\/\\\?\[\]\^\|]+/

If you want to include .-_ as well, then use this:

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

If you want to filter strings that are URL friendly and do not contain any special characters or spaces, then use this:

/^[^ !"`'#%&,:;<>=@{}~\$\(\)\*\+\/\\\?\[\]\^\|]+$/

When you use patterns like /[^A-Za-z0-9]/, then you will start catching special alphabets like those of other languages and some European accented alphabets (like é, í ).

Solution 7 - Java

Here is my regex variant of a special character:

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

(Java code)

Solution 8 - Java

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.

Solution 9 - Java

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.

Solution 10 - Java

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.

Solution 11 - Java

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

Solution 12 - Java

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

Solution 13 - Java

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();

Solution 14 - Java

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

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

Solution 15 - Java

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("");

Solution 16 - Java

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)

Solution 17 - Java

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.

Solution 18 - Java

(^\W$)

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

Solution 19 - Java

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}]+"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPiotr SagalaraView Question on Stackoverflow
Solution 1 - JavaxanatosView Answer on Stackoverflow
Solution 2 - JavaJerryView Answer on Stackoverflow
Solution 3 - JavaSina IravanianView Answer on Stackoverflow
Solution 4 - JavaanubhavaView Answer on Stackoverflow
Solution 5 - JavaJohn SchellingerhoutView Answer on Stackoverflow
Solution 6 - JavaEhsan KaziView Answer on Stackoverflow
Solution 7 - JavaChuckView Answer on Stackoverflow
Solution 8 - JavaSerguei FedorovView Answer on Stackoverflow
Solution 9 - JavaMohamed YusuffView Answer on Stackoverflow
Solution 10 - JavacdaigaView Answer on Stackoverflow
Solution 11 - JavasamView Answer on Stackoverflow
Solution 12 - JavaAshView Answer on Stackoverflow
Solution 13 - JavaKayVView Answer on Stackoverflow
Solution 14 - JavaSuchismita BarikView Answer on Stackoverflow
Solution 15 - JavaEnergyView Answer on Stackoverflow
Solution 16 - JavaAnirudh SharmaView Answer on Stackoverflow
Solution 17 - JavamlnbhargavView Answer on Stackoverflow
Solution 18 - JavaSareesh KrishnanView Answer on Stackoverflow
Solution 19 - JavadanvasiloiuView Answer on Stackoverflow