Piotr Sagalara
Piotr Sagalara

Reputation: 2485

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

Upvotes: 150

Views: 896640

Answers (26)

anubhava
anubhava

Reputation: 784918

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

Upvotes: 34

Earth Spacon
Earth Spacon

Reputation: 21

None of the answers helped for me. Found this regex /^[^a-zA-Z0-9]+$/ from this article and it worked!

Upvotes: 1

Wasim A.
Wasim A.

Reputation: 9890

what about [ -~] This will match all ASCII characters from the space to tilde

Upvotes: 0

Mostafa Wael
Mostafa Wael

Reputation: 3828

To match the common Ascii special characters you can simply use this [!-\/].

So, it will be Pattern regex = Pattern.compile("[!-\/]");

Upvotes: 0

You have to escape some symbols

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

OR

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

Upvotes: 0

jas99
jas99

Reputation: 311

Shout out to Mohamed Yusuff 's solution!

We can match all 32 special characters using range.

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

1st Group

[!-\/]

  • Match ASCII code from 33 to 47:
  • !"#$%&'()*+,-./

-- 15 out of 32 characters matched

2nd Group

[:-@]

  • Match ASCII code from 58 to 64:
  • :;<=>?@

-- 7 out of 32 characters matched

3rd Group

[[-`]

  • Match ASCII code from 91 to 96:
  • [\]^_`

-- 6 out of 32 characters matched

4th Group

[{-~]

  • Match ASCII code from 123 to 126:
  • {|}~

-- 4 out of 32 characters matched

In total matched back all 32 chars (15+7+6+4)

Reference

Special Character table_Arranged

Extended ASCII table

Upvotes: 20

Andries van Weeren
Andries van Weeren

Reputation: 11

A small addition to include all special characters like: ū and Ā:

An example:

Pattern regex = Pattern.compile("[A-Za-zÀ-ÖØ-öø-ū]");

Upvotes: 1

I use reg below for find special character in string

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

Upvotes: -1

Ehsan Kazi
Ehsan Kazi

Reputation: 429

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 é, í ).

Upvotes: 17

xanatos
xanatos

Reputation: 111820

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

Upvotes: 295

mlnbhargav
mlnbhargav

Reputation: 21

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.

Upvotes: 1

danvasiloiu
danvasiloiu

Reputation: 751

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

Upvotes: 1

Anirudh Sharma
Anirudh Sharma

Reputation: 21

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)

Upvotes: 2

Mohamed Yusuff
Mohamed Yusuff

Reputation: 121

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.

Upvotes: 12

John Schellingerhout
John Schellingerhout

Reputation: 201

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

Upvotes: 19

Energy
Energy

Reputation: 958

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

Upvotes: 3

Suchismita Barik
Suchismita Barik

Reputation: 31

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

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

Upvotes: 3

KayV
KayV

Reputation: 13835

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

Upvotes: 3

VKostenc
VKostenc

Reputation: 1285

Here is my regex variant of a special character:

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

(Java code)

Upvotes: 9

Ash
Ash

Reputation: 1290

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

Upvotes: 3

Sareesh Krishnan
Sareesh Krishnan

Reputation: 720

(^\W$)

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

Upvotes: 0

sam
sam

Reputation: 113

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

Upvotes: 6

cdaiga
cdaiga

Reputation: 4939

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.

Upvotes: 4

Serguei Fedorov
Serguei Fedorov

Reputation: 7913

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.

Upvotes: 7

Sina Iravanian
Sina Iravanian

Reputation: 16286

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.

Upvotes: 35

Jerry
Jerry

Reputation: 71538

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:

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

Upvotes: 53

Related Questions