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!");
java regex
add a comment |
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 regex
4
the dash inshould be escaped, it has special meaning there.
– MightyPork
Aug 5 '13 at 12:21
7
¿¡ So you think that the only special characters that exist are the ones on your keyboard !? :-)
– xanatos
Aug 5 '13 at 12:22
2
Exactly. It would be better to define all "non-special" charactes and make that negative.
– NeplatnyUdaj
Aug 5 '13 at 12:22
yes maybe it would be wiser to assert the use of only those characters you want to allow.
– d'alar'cop
Aug 5 '13 at 12:23
14
Just like children, all characters are special, each in its own way. :)
– tchrist
Aug 5 '13 at 13:15
add a comment |
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 regex
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 regex
java regex
edited Nov 12 at 11:19
Suraj Rao
22.6k75469
22.6k75469
asked Aug 5 '13 at 12:18
Piotr Sagalara
78511122
78511122
4
the dash inshould be escaped, it has special meaning there.
– MightyPork
Aug 5 '13 at 12:21
7
¿¡ So you think that the only special characters that exist are the ones on your keyboard !? :-)
– xanatos
Aug 5 '13 at 12:22
2
Exactly. It would be better to define all "non-special" charactes and make that negative.
– NeplatnyUdaj
Aug 5 '13 at 12:22
yes maybe it would be wiser to assert the use of only those characters you want to allow.
– d'alar'cop
Aug 5 '13 at 12:23
14
Just like children, all characters are special, each in its own way. :)
– tchrist
Aug 5 '13 at 13:15
add a comment |
4
the dash inshould be escaped, it has special meaning there.
– MightyPork
Aug 5 '13 at 12:21
7
¿¡ So you think that the only special characters that exist are the ones on your keyboard !? :-)
– xanatos
Aug 5 '13 at 12:22
2
Exactly. It would be better to define all "non-special" charactes and make that negative.
– NeplatnyUdaj
Aug 5 '13 at 12:22
yes maybe it would be wiser to assert the use of only those characters you want to allow.
– d'alar'cop
Aug 5 '13 at 12:23
14
Just like children, all characters are special, each in its own way. :)
– tchrist
Aug 5 '13 at 13:15
4
4
the dash in
should be escaped, it has special meaning there.– MightyPork
Aug 5 '13 at 12:21
the dash in
should be escaped, it has special meaning there.– MightyPork
Aug 5 '13 at 12:21
7
7
¿¡ So you think that the only special characters that exist are the ones on your keyboard !? :-)
– xanatos
Aug 5 '13 at 12:22
¿¡ So you think that the only special characters that exist are the ones on your keyboard !? :-)
– xanatos
Aug 5 '13 at 12:22
2
2
Exactly. It would be better to define all "non-special" charactes and make that negative.
– NeplatnyUdaj
Aug 5 '13 at 12:22
Exactly. It would be better to define all "non-special" charactes and make that negative.
– NeplatnyUdaj
Aug 5 '13 at 12:22
yes maybe it would be wiser to assert the use of only those characters you want to allow.
– d'alar'cop
Aug 5 '13 at 12:23
yes maybe it would be wiser to assert the use of only those characters you want to allow.
– d'alar'cop
Aug 5 '13 at 12:23
14
14
Just like children, all characters are special, each in its own way. :)
– tchrist
Aug 5 '13 at 13:15
Just like children, all characters are special, each in its own way. :)
– tchrist
Aug 5 '13 at 13:15
add a comment |
13 Answers
13
active
oldest
votes
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-2Try a negative match:
Pattern regex = Pattern.compile("[^A-Za-z0-9]");
(this will ok only A-Z "standard" letters and "standard" digits.
3
Will this work for non-english characters? such as à 蔿
– Abdullah Shoaib
Dec 9 '13 at 12:44
2
@AbdullahShoaib Clearly not :) You'll need to do a full list of what you consider "special" and/or what you consider "good".
– xanatos
Mar 9 '15 at 9:58
I notice many people use[A-Za-z0-9]to represent any letter or digit, both lowercase and upper, but is it not better to just do[0-z]?
– Abraham Murciano Benzadon
Jun 25 '17 at 16:09
3
@AbrahamMurcianoBenzadon: The decimal digits, the upper case roman letters, and the lower case roman letters occupy three disjoint ranges of character code space.
– Solomon Slow
Jun 25 '17 at 17:31
1
@AbrahamMurcianoBenzadon You can see what James wrote in the handy screenshot of Character Map posted by Sina in another response: your regex would accept :;<=>?@^_` (other than 0-9, a-z, A-Z)
– xanatos
Jun 26 '17 at 6:15
|
show 3 more comments
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:
[$&+,:;=?@#|'<>.^*()%!-]
add a comment |
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:

If by special characters, you mean punctuation and symbols use:
[pPpS]
which contains all unicode punctuation and symbols.
add a comment |
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("[^ws]");
Which means match everything other than [A-Za-z0-9s_]
Unicode version:
Pattern regex = Pattern.compile("[^pLds_]");
add a comment |
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class detectspecial
public int getSpecialCharacterCount(String s)
this code always return 0
– En NuNYet de Can CalÇadA
May 7 '17 at 16:36
add a comment |
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.
This would be a better answer if it included an explanation along with the code.
– John Hascall
Feb 17 '16 at 17:04
@JohnHascall, I just added an explanation.
– cdaiga
Feb 17 '16 at 17:31
Inside a character class, none of those characters need to be escaped except` and-`. Many of them never need to be escaped at all. "Better safe than sorry" is a fine philosophy, but readability is important, too.
– Alan Moore
Feb 17 '16 at 18:46
add a comment |
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-x2Fx3A-x40x5B-x60x7B-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-9s]+
Hower this will not catch _ ^ and probably others.
I finally used(?i)^([[a-z][^a-z0-9\s\(\)\[\]\\\\^\$\|\?\*\+\.\<\>\-\=\!\_]]*)$to match any character.
– cdaiga
Feb 17 '16 at 11:25
Whatever works best for you!
– Serguei Fedorov
Feb 17 '16 at 18:37
1
Never use[A-z]in a regex. It matches all uppercase and lowercase ASCII letters as you would expect. but it also matches several punctuation characters whose code points lie betweenZanda. Use[A-Za-z]instead, or[a-z]in case-insensitive mode.
– Alan Moore
Feb 17 '16 at 18:37
@AlanMoore, good to know! I'll make the change to the answer.
– Serguei Fedorov
Feb 17 '16 at 18:39
how about '.' dot character . It supporsed to match any character except new line. In python re.DOTALL matches all including newline. Check out the regular expression faq in the python tutorial docs.python.org/2/howto/regex.html
– Dr Deo
Sep 6 at 15:01
add a comment |
Use this regular expression pattern ("^[a-zA-Z0-9]*$") .It validates alphanumeric string excluding the special characters
add a comment |
Here is my regex variant of a special character:
String regExp = "^[^<>"/|;:.,~!?@#$%^=&*\]\\()\[¿§«»ω⊙¤°℃℉€¥£¢¡®©0-9_+]*$";
(Java code)
2
you missed •☺○♣♥☻☺ and more..
– Aks4125
Dec 30 '17 at 4:58
add a comment |
Try using this for the same things - StringUtils.isAlphanumeric(value)
space/blank is also a special char if you use this method. Better to replace the space and tabs chars before calling this method.
– Deepu Sahni
Mar 21 at 1:14
add a comment |
Here is my regular expression, that I used for removing all the special characters from any string :
String regex = ("[ \\s@ [\"]\\[\\]\\\-9|^#%'*/<()>:`;,!& .?_$+-]+")
add a comment |
(^W$)
^ - start of the string,
W - match any non-word character [^a-zA-Z0-9_],
$ - end of the string
add a comment |
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();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18057962%2fregex-pattern-including-all-special-characters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
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-2Try a negative match:
Pattern regex = Pattern.compile("[^A-Za-z0-9]");
(this will ok only A-Z "standard" letters and "standard" digits.
3
Will this work for non-english characters? such as à 蔿
– Abdullah Shoaib
Dec 9 '13 at 12:44
2
@AbdullahShoaib Clearly not :) You'll need to do a full list of what you consider "special" and/or what you consider "good".
– xanatos
Mar 9 '15 at 9:58
I notice many people use[A-Za-z0-9]to represent any letter or digit, both lowercase and upper, but is it not better to just do[0-z]?
– Abraham Murciano Benzadon
Jun 25 '17 at 16:09
3
@AbrahamMurcianoBenzadon: The decimal digits, the upper case roman letters, and the lower case roman letters occupy three disjoint ranges of character code space.
– Solomon Slow
Jun 25 '17 at 17:31
1
@AbrahamMurcianoBenzadon You can see what James wrote in the handy screenshot of Character Map posted by Sina in another response: your regex would accept :;<=>?@^_` (other than 0-9, a-z, A-Z)
– xanatos
Jun 26 '17 at 6:15
|
show 3 more comments
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-2Try a negative match:
Pattern regex = Pattern.compile("[^A-Za-z0-9]");
(this will ok only A-Z "standard" letters and "standard" digits.
3
Will this work for non-english characters? such as à 蔿
– Abdullah Shoaib
Dec 9 '13 at 12:44
2
@AbdullahShoaib Clearly not :) You'll need to do a full list of what you consider "special" and/or what you consider "good".
– xanatos
Mar 9 '15 at 9:58
I notice many people use[A-Za-z0-9]to represent any letter or digit, both lowercase and upper, but is it not better to just do[0-z]?
– Abraham Murciano Benzadon
Jun 25 '17 at 16:09
3
@AbrahamMurcianoBenzadon: The decimal digits, the upper case roman letters, and the lower case roman letters occupy three disjoint ranges of character code space.
– Solomon Slow
Jun 25 '17 at 17:31
1
@AbrahamMurcianoBenzadon You can see what James wrote in the handy screenshot of Character Map posted by Sina in another response: your regex would accept :;<=>?@^_` (other than 0-9, a-z, A-Z)
– xanatos
Jun 26 '17 at 6:15
|
show 3 more comments
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-2Try a negative match:
Pattern regex = Pattern.compile("[^A-Za-z0-9]");
(this will ok only A-Z "standard" letters and "standard" digits.
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-2Try a negative match:
Pattern regex = Pattern.compile("[^A-Za-z0-9]");
(this will ok only A-Z "standard" letters and "standard" digits.
edited Oct 2 '15 at 12:05
answered Aug 5 '13 at 12:23
xanatos
88.6k7139196
88.6k7139196
3
Will this work for non-english characters? such as à 蔿
– Abdullah Shoaib
Dec 9 '13 at 12:44
2
@AbdullahShoaib Clearly not :) You'll need to do a full list of what you consider "special" and/or what you consider "good".
– xanatos
Mar 9 '15 at 9:58
I notice many people use[A-Za-z0-9]to represent any letter or digit, both lowercase and upper, but is it not better to just do[0-z]?
– Abraham Murciano Benzadon
Jun 25 '17 at 16:09
3
@AbrahamMurcianoBenzadon: The decimal digits, the upper case roman letters, and the lower case roman letters occupy three disjoint ranges of character code space.
– Solomon Slow
Jun 25 '17 at 17:31
1
@AbrahamMurcianoBenzadon You can see what James wrote in the handy screenshot of Character Map posted by Sina in another response: your regex would accept :;<=>?@^_` (other than 0-9, a-z, A-Z)
– xanatos
Jun 26 '17 at 6:15
|
show 3 more comments
3
Will this work for non-english characters? such as à 蔿
– Abdullah Shoaib
Dec 9 '13 at 12:44
2
@AbdullahShoaib Clearly not :) You'll need to do a full list of what you consider "special" and/or what you consider "good".
– xanatos
Mar 9 '15 at 9:58
I notice many people use[A-Za-z0-9]to represent any letter or digit, both lowercase and upper, but is it not better to just do[0-z]?
– Abraham Murciano Benzadon
Jun 25 '17 at 16:09
3
@AbrahamMurcianoBenzadon: The decimal digits, the upper case roman letters, and the lower case roman letters occupy three disjoint ranges of character code space.
– Solomon Slow
Jun 25 '17 at 17:31
1
@AbrahamMurcianoBenzadon You can see what James wrote in the handy screenshot of Character Map posted by Sina in another response: your regex would accept :;<=>?@^_` (other than 0-9, a-z, A-Z)
– xanatos
Jun 26 '17 at 6:15
3
3
Will this work for non-english characters? such as à 蔿
– Abdullah Shoaib
Dec 9 '13 at 12:44
Will this work for non-english characters? such as à 蔿
– Abdullah Shoaib
Dec 9 '13 at 12:44
2
2
@AbdullahShoaib Clearly not :) You'll need to do a full list of what you consider "special" and/or what you consider "good".
– xanatos
Mar 9 '15 at 9:58
@AbdullahShoaib Clearly not :) You'll need to do a full list of what you consider "special" and/or what you consider "good".
– xanatos
Mar 9 '15 at 9:58
I notice many people use
[A-Za-z0-9] to represent any letter or digit, both lowercase and upper, but is it not better to just do [0-z]?– Abraham Murciano Benzadon
Jun 25 '17 at 16:09
I notice many people use
[A-Za-z0-9] to represent any letter or digit, both lowercase and upper, but is it not better to just do [0-z]?– Abraham Murciano Benzadon
Jun 25 '17 at 16:09
3
3
@AbrahamMurcianoBenzadon: The decimal digits, the upper case roman letters, and the lower case roman letters occupy three disjoint ranges of character code space.
– Solomon Slow
Jun 25 '17 at 17:31
@AbrahamMurcianoBenzadon: The decimal digits, the upper case roman letters, and the lower case roman letters occupy three disjoint ranges of character code space.
– Solomon Slow
Jun 25 '17 at 17:31
1
1
@AbrahamMurcianoBenzadon You can see what James wrote in the handy screenshot of Character Map posted by Sina in another response: your regex would accept :;<=>?@^_` (other than 0-9, a-z, A-Z)
– xanatos
Jun 26 '17 at 6:15
@AbrahamMurcianoBenzadon You can see what James wrote in the handy screenshot of Character Map posted by Sina in another response: your regex would accept :;<=>?@^_` (other than 0-9, a-z, A-Z)
– xanatos
Jun 26 '17 at 6:15
|
show 3 more comments
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:
[$&+,:;=?@#|'<>.^*()%!-]
add a comment |
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:
[$&+,:;=?@#|'<>.^*()%!-]
add a comment |
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:
[$&+,:;=?@#|'<>.^*()%!-]
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:
[$&+,:;=?@#|'<>.^*()%!-]
answered Aug 5 '13 at 12:22
Jerry
57.7k1067101
57.7k1067101
add a comment |
add a comment |
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:

If by special characters, you mean punctuation and symbols use:
[pPpS]
which contains all unicode punctuation and symbols.
add a comment |
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:

If by special characters, you mean punctuation and symbols use:
[pPpS]
which contains all unicode punctuation and symbols.
add a comment |
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:

If by special characters, you mean punctuation and symbols use:
[pPpS]
which contains all unicode punctuation and symbols.
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:

If by special characters, you mean punctuation and symbols use:
[pPpS]
which contains all unicode punctuation and symbols.
answered Aug 5 '13 at 12:31
Sina Iravanian
12.8k32241
12.8k32241
add a comment |
add a comment |
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("[^ws]");
Which means match everything other than [A-Za-z0-9s_]
Unicode version:
Pattern regex = Pattern.compile("[^pLds_]");
add a comment |
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("[^ws]");
Which means match everything other than [A-Za-z0-9s_]
Unicode version:
Pattern regex = Pattern.compile("[^pLds_]");
add a comment |
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("[^ws]");
Which means match everything other than [A-Za-z0-9s_]
Unicode version:
Pattern regex = Pattern.compile("[^pLds_]");
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("[^ws]");
Which means match everything other than [A-Za-z0-9s_]
Unicode version:
Pattern regex = Pattern.compile("[^pLds_]");
answered Aug 5 '13 at 12:27
anubhava
519k46315389
519k46315389
add a comment |
add a comment |
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class detectspecial
public int getSpecialCharacterCount(String s)
this code always return 0
– En NuNYet de Can CalÇadA
May 7 '17 at 16:36
add a comment |
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class detectspecial
public int getSpecialCharacterCount(String s)
this code always return 0
– En NuNYet de Can CalÇadA
May 7 '17 at 16:36
add a comment |
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class detectspecial
public int getSpecialCharacterCount(String s)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class detectspecial
public int getSpecialCharacterCount(String s)
answered Jun 26 '14 at 8:23
Ashish Sharma
21224
21224
this code always return 0
– En NuNYet de Can CalÇadA
May 7 '17 at 16:36
add a comment |
this code always return 0
– En NuNYet de Can CalÇadA
May 7 '17 at 16:36
this code always return 0
– En NuNYet de Can CalÇadA
May 7 '17 at 16:36
this code always return 0
– En NuNYet de Can CalÇadA
May 7 '17 at 16:36
add a comment |
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.
This would be a better answer if it included an explanation along with the code.
– John Hascall
Feb 17 '16 at 17:04
@JohnHascall, I just added an explanation.
– cdaiga
Feb 17 '16 at 17:31
Inside a character class, none of those characters need to be escaped except` and-`. Many of them never need to be escaped at all. "Better safe than sorry" is a fine philosophy, but readability is important, too.
– Alan Moore
Feb 17 '16 at 18:46
add a comment |
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.
This would be a better answer if it included an explanation along with the code.
– John Hascall
Feb 17 '16 at 17:04
@JohnHascall, I just added an explanation.
– cdaiga
Feb 17 '16 at 17:31
Inside a character class, none of those characters need to be escaped except` and-`. Many of them never need to be escaped at all. "Better safe than sorry" is a fine philosophy, but readability is important, too.
– Alan Moore
Feb 17 '16 at 18:46
add a comment |
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.
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.
edited Feb 17 '16 at 18:42
Alan Moore
60.5k978132
60.5k978132
answered Feb 17 '16 at 11:26
cdaiga
3,75611222
3,75611222
This would be a better answer if it included an explanation along with the code.
– John Hascall
Feb 17 '16 at 17:04
@JohnHascall, I just added an explanation.
– cdaiga
Feb 17 '16 at 17:31
Inside a character class, none of those characters need to be escaped except` and-`. Many of them never need to be escaped at all. "Better safe than sorry" is a fine philosophy, but readability is important, too.
– Alan Moore
Feb 17 '16 at 18:46
add a comment |
This would be a better answer if it included an explanation along with the code.
– John Hascall
Feb 17 '16 at 17:04
@JohnHascall, I just added an explanation.
– cdaiga
Feb 17 '16 at 17:31
Inside a character class, none of those characters need to be escaped except` and-`. Many of them never need to be escaped at all. "Better safe than sorry" is a fine philosophy, but readability is important, too.
– Alan Moore
Feb 17 '16 at 18:46
This would be a better answer if it included an explanation along with the code.
– John Hascall
Feb 17 '16 at 17:04
This would be a better answer if it included an explanation along with the code.
– John Hascall
Feb 17 '16 at 17:04
@JohnHascall, I just added an explanation.
– cdaiga
Feb 17 '16 at 17:31
@JohnHascall, I just added an explanation.
– cdaiga
Feb 17 '16 at 17:31
Inside a character class, none of those characters need to be escaped except
` and -`. Many of them never need to be escaped at all. "Better safe than sorry" is a fine philosophy, but readability is important, too.– Alan Moore
Feb 17 '16 at 18:46
Inside a character class, none of those characters need to be escaped except
` and -`. Many of them never need to be escaped at all. "Better safe than sorry" is a fine philosophy, but readability is important, too.– Alan Moore
Feb 17 '16 at 18:46
add a comment |
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-x2Fx3A-x40x5B-x60x7B-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-9s]+
Hower this will not catch _ ^ and probably others.
I finally used(?i)^([[a-z][^a-z0-9\s\(\)\[\]\\\\^\$\|\?\*\+\.\<\>\-\=\!\_]]*)$to match any character.
– cdaiga
Feb 17 '16 at 11:25
Whatever works best for you!
– Serguei Fedorov
Feb 17 '16 at 18:37
1
Never use[A-z]in a regex. It matches all uppercase and lowercase ASCII letters as you would expect. but it also matches several punctuation characters whose code points lie betweenZanda. Use[A-Za-z]instead, or[a-z]in case-insensitive mode.
– Alan Moore
Feb 17 '16 at 18:37
@AlanMoore, good to know! I'll make the change to the answer.
– Serguei Fedorov
Feb 17 '16 at 18:39
how about '.' dot character . It supporsed to match any character except new line. In python re.DOTALL matches all including newline. Check out the regular expression faq in the python tutorial docs.python.org/2/howto/regex.html
– Dr Deo
Sep 6 at 15:01
add a comment |
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-x2Fx3A-x40x5B-x60x7B-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-9s]+
Hower this will not catch _ ^ and probably others.
I finally used(?i)^([[a-z][^a-z0-9\s\(\)\[\]\\\\^\$\|\?\*\+\.\<\>\-\=\!\_]]*)$to match any character.
– cdaiga
Feb 17 '16 at 11:25
Whatever works best for you!
– Serguei Fedorov
Feb 17 '16 at 18:37
1
Never use[A-z]in a regex. It matches all uppercase and lowercase ASCII letters as you would expect. but it also matches several punctuation characters whose code points lie betweenZanda. Use[A-Za-z]instead, or[a-z]in case-insensitive mode.
– Alan Moore
Feb 17 '16 at 18:37
@AlanMoore, good to know! I'll make the change to the answer.
– Serguei Fedorov
Feb 17 '16 at 18:39
how about '.' dot character . It supporsed to match any character except new line. In python re.DOTALL matches all including newline. Check out the regular expression faq in the python tutorial docs.python.org/2/howto/regex.html
– Dr Deo
Sep 6 at 15:01
add a comment |
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-x2Fx3A-x40x5B-x60x7B-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-9s]+
Hower this will not catch _ ^ and probably others.
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-x2Fx3A-x40x5B-x60x7B-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-9s]+
Hower this will not catch _ ^ and probably others.
edited Feb 17 '16 at 18:40
answered Feb 3 '16 at 22:06
Serguei Fedorov
4,17394379
4,17394379
I finally used(?i)^([[a-z][^a-z0-9\s\(\)\[\]\\\\^\$\|\?\*\+\.\<\>\-\=\!\_]]*)$to match any character.
– cdaiga
Feb 17 '16 at 11:25
Whatever works best for you!
– Serguei Fedorov
Feb 17 '16 at 18:37
1
Never use[A-z]in a regex. It matches all uppercase and lowercase ASCII letters as you would expect. but it also matches several punctuation characters whose code points lie betweenZanda. Use[A-Za-z]instead, or[a-z]in case-insensitive mode.
– Alan Moore
Feb 17 '16 at 18:37
@AlanMoore, good to know! I'll make the change to the answer.
– Serguei Fedorov
Feb 17 '16 at 18:39
how about '.' dot character . It supporsed to match any character except new line. In python re.DOTALL matches all including newline. Check out the regular expression faq in the python tutorial docs.python.org/2/howto/regex.html
– Dr Deo
Sep 6 at 15:01
add a comment |
I finally used(?i)^([[a-z][^a-z0-9\s\(\)\[\]\\\\^\$\|\?\*\+\.\<\>\-\=\!\_]]*)$to match any character.
– cdaiga
Feb 17 '16 at 11:25
Whatever works best for you!
– Serguei Fedorov
Feb 17 '16 at 18:37
1
Never use[A-z]in a regex. It matches all uppercase and lowercase ASCII letters as you would expect. but it also matches several punctuation characters whose code points lie betweenZanda. Use[A-Za-z]instead, or[a-z]in case-insensitive mode.
– Alan Moore
Feb 17 '16 at 18:37
@AlanMoore, good to know! I'll make the change to the answer.
– Serguei Fedorov
Feb 17 '16 at 18:39
how about '.' dot character . It supporsed to match any character except new line. In python re.DOTALL matches all including newline. Check out the regular expression faq in the python tutorial docs.python.org/2/howto/regex.html
– Dr Deo
Sep 6 at 15:01
I finally used
(?i)^([[a-z][^a-z0-9\s\(\)\[\]\\\\^\$\|\?\*\+\.\<\>\-\=\!\_]]*)$ to match any character.– cdaiga
Feb 17 '16 at 11:25
I finally used
(?i)^([[a-z][^a-z0-9\s\(\)\[\]\\\\^\$\|\?\*\+\.\<\>\-\=\!\_]]*)$ to match any character.– cdaiga
Feb 17 '16 at 11:25
Whatever works best for you!
– Serguei Fedorov
Feb 17 '16 at 18:37
Whatever works best for you!
– Serguei Fedorov
Feb 17 '16 at 18:37
1
1
Never use
[A-z] in a regex. It matches all uppercase and lowercase ASCII letters as you would expect. but it also matches several punctuation characters whose code points lie between Z and a. Use [A-Za-z] instead, or [a-z] in case-insensitive mode.– Alan Moore
Feb 17 '16 at 18:37
Never use
[A-z] in a regex. It matches all uppercase and lowercase ASCII letters as you would expect. but it also matches several punctuation characters whose code points lie between Z and a. Use [A-Za-z] instead, or [a-z] in case-insensitive mode.– Alan Moore
Feb 17 '16 at 18:37
@AlanMoore, good to know! I'll make the change to the answer.
– Serguei Fedorov
Feb 17 '16 at 18:39
@AlanMoore, good to know! I'll make the change to the answer.
– Serguei Fedorov
Feb 17 '16 at 18:39
how about '.' dot character . It supporsed to match any character except new line. In python re.DOTALL matches all including newline. Check out the regular expression faq in the python tutorial docs.python.org/2/howto/regex.html
– Dr Deo
Sep 6 at 15:01
how about '.' dot character . It supporsed to match any character except new line. In python re.DOTALL matches all including newline. Check out the regular expression faq in the python tutorial docs.python.org/2/howto/regex.html
– Dr Deo
Sep 6 at 15:01
add a comment |
Use this regular expression pattern ("^[a-zA-Z0-9]*$") .It validates alphanumeric string excluding the special characters
add a comment |
Use this regular expression pattern ("^[a-zA-Z0-9]*$") .It validates alphanumeric string excluding the special characters
add a comment |
Use this regular expression pattern ("^[a-zA-Z0-9]*$") .It validates alphanumeric string excluding the special characters
Use this regular expression pattern ("^[a-zA-Z0-9]*$") .It validates alphanumeric string excluding the special characters
answered Jul 4 '16 at 6:41
sam
438
438
add a comment |
add a comment |
Here is my regex variant of a special character:
String regExp = "^[^<>"/|;:.,~!?@#$%^=&*\]\\()\[¿§«»ω⊙¤°℃℉€¥£¢¡®©0-9_+]*$";
(Java code)
2
you missed •☺○♣♥☻☺ and more..
– Aks4125
Dec 30 '17 at 4:58
add a comment |
Here is my regex variant of a special character:
String regExp = "^[^<>"/|;:.,~!?@#$%^=&*\]\\()\[¿§«»ω⊙¤°℃℉€¥£¢¡®©0-9_+]*$";
(Java code)
2
you missed •☺○♣♥☻☺ and more..
– Aks4125
Dec 30 '17 at 4:58
add a comment |
Here is my regex variant of a special character:
String regExp = "^[^<>"/|;:.,~!?@#$%^=&*\]\\()\[¿§«»ω⊙¤°℃℉€¥£¢¡®©0-9_+]*$";
(Java code)
Here is my regex variant of a special character:
String regExp = "^[^<>"/|;:.,~!?@#$%^=&*\]\\()\[¿§«»ω⊙¤°℃℉€¥£¢¡®©0-9_+]*$";
(Java code)
answered Mar 20 '17 at 14:52
Chuck
548711
548711
2
you missed •☺○♣♥☻☺ and more..
– Aks4125
Dec 30 '17 at 4:58
add a comment |
2
you missed •☺○♣♥☻☺ and more..
– Aks4125
Dec 30 '17 at 4:58
2
2
you missed •☺○♣♥☻☺ and more..
– Aks4125
Dec 30 '17 at 4:58
you missed •☺○♣♥☻☺ and more..
– Aks4125
Dec 30 '17 at 4:58
add a comment |
Try using this for the same things - StringUtils.isAlphanumeric(value)
space/blank is also a special char if you use this method. Better to replace the space and tabs chars before calling this method.
– Deepu Sahni
Mar 21 at 1:14
add a comment |
Try using this for the same things - StringUtils.isAlphanumeric(value)
space/blank is also a special char if you use this method. Better to replace the space and tabs chars before calling this method.
– Deepu Sahni
Mar 21 at 1:14
add a comment |
Try using this for the same things - StringUtils.isAlphanumeric(value)
Try using this for the same things - StringUtils.isAlphanumeric(value)
edited Dec 7 '16 at 23:54
Buddy
8,56952951
8,56952951
answered Dec 7 '16 at 23:52
Ash
28839
28839
space/blank is also a special char if you use this method. Better to replace the space and tabs chars before calling this method.
– Deepu Sahni
Mar 21 at 1:14
add a comment |
space/blank is also a special char if you use this method. Better to replace the space and tabs chars before calling this method.
– Deepu Sahni
Mar 21 at 1:14
space/blank is also a special char if you use this method. Better to replace the space and tabs chars before calling this method.
– Deepu Sahni
Mar 21 at 1:14
space/blank is also a special char if you use this method. Better to replace the space and tabs chars before calling this method.
– Deepu Sahni
Mar 21 at 1:14
add a comment |
Here is my regular expression, that I used for removing all the special characters from any string :
String regex = ("[ \\s@ [\"]\\[\\]\\\-9|^#%'*/<()>:`;,!& .?_$+-]+")
add a comment |
Here is my regular expression, that I used for removing all the special characters from any string :
String regex = ("[ \\s@ [\"]\\[\\]\\\-9|^#%'*/<()>:`;,!& .?_$+-]+")
add a comment |
Here is my regular expression, that I used for removing all the special characters from any string :
String regex = ("[ \\s@ [\"]\\[\\]\\\-9|^#%'*/<()>:`;,!& .?_$+-]+")
Here is my regular expression, that I used for removing all the special characters from any string :
String regex = ("[ \\s@ [\"]\\[\\]\\\-9|^#%'*/<()>:`;,!& .?_$+-]+")
edited Nov 12 at 12:05
Jan Černý
8121622
8121622
answered Nov 12 at 10:56
Suchismita Barik
112
112
add a comment |
add a comment |
(^W$)
^ - start of the string,
W - match any non-word character [^a-zA-Z0-9_],
$ - end of the string
add a comment |
(^W$)
^ - start of the string,
W - match any non-word character [^a-zA-Z0-9_],
$ - end of the string
add a comment |
(^W$)
^ - start of the string,
W - match any non-word character [^a-zA-Z0-9_],
$ - end of the string
(^W$)
^ - start of the string,
W - match any non-word character [^a-zA-Z0-9_],
$ - end of the string
answered Aug 26 '16 at 9:18
Sareesh Krishnan
20626
20626
add a comment |
add a comment |
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();
add a comment |
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();
add a comment |
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();
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();
answered Sep 20 at 12:18
KayV
3,65122054
3,65122054
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18057962%2fregex-pattern-including-all-special-characters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
the dash in
should be escaped, it has special meaning there.– MightyPork
Aug 5 '13 at 12:21
7
¿¡ So you think that the only special characters that exist are the ones on your keyboard !? :-)
– xanatos
Aug 5 '13 at 12:22
2
Exactly. It would be better to define all "non-special" charactes and make that negative.
– NeplatnyUdaj
Aug 5 '13 at 12:22
yes maybe it would be wiser to assert the use of only those characters you want to allow.
– d'alar'cop
Aug 5 '13 at 12:23
14
Just like children, all characters are special, each in its own way. :)
– tchrist
Aug 5 '13 at 13:15