Regex pattern including all special characters










36














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










share|improve this question



















  • 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















36














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










share|improve this question



















  • 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













36












36








36


10





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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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







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












13 Answers
13






active

oldest

votes


















105














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" digits.






share|improve this answer


















  • 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



















18














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:



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





share|improve this answer




























    15














    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:



    [pPpS]


    which contains all unicode punctuation and symbols.






    share|improve this answer




























      6














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





      share|improve this answer




























        3














        import java.util.regex.Matcher;
        import java.util.regex.Pattern;

        public class detectspecial
        public int getSpecialCharacterCount(String s)






        share|improve this answer




















        • this code always return 0
          – En NuNYet de Can CalÇadA
          May 7 '17 at 16:36


















        3














        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.






        share|improve this answer






















        • 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


















        2














        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.






        share|improve this answer






















        • 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 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










        • 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


















        2














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






        share|improve this answer




























          2














          Here is my regex variant of a special character:



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


          (Java code)






          share|improve this answer
















          • 2




            you missed •☺○♣♥☻☺ and more..
            – Aks4125
            Dec 30 '17 at 4:58



















          1














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






          share|improve this answer






















          • 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


















          1














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



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





          share|improve this answer






























            0














            (^W$)



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






            share|improve this answer




























              0














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





              share|improve this answer




















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



                );













                draft saved

                draft discarded


















                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









                105














                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" digits.






                share|improve this answer


















                • 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
















                105














                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" digits.






                share|improve this answer


















                • 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














                105












                105








                105






                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" digits.






                share|improve this answer














                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" digits.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                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













                • 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














                18














                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:



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





                share|improve this answer

























                  18














                  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:



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





                  share|improve this answer























                    18












                    18








                    18






                    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:



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





                    share|improve this answer












                    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:



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






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 5 '13 at 12:22









                    Jerry

                    57.7k1067101




                    57.7k1067101





















                        15














                        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:



                        [pPpS]


                        which contains all unicode punctuation and symbols.






                        share|improve this answer

























                          15














                          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:



                          [pPpS]


                          which contains all unicode punctuation and symbols.






                          share|improve this answer























                            15












                            15








                            15






                            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:



                            [pPpS]


                            which contains all unicode punctuation and symbols.






                            share|improve this answer












                            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:



                            [pPpS]


                            which contains all unicode punctuation and symbols.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 5 '13 at 12:31









                            Sina Iravanian

                            12.8k32241




                            12.8k32241





















                                6














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





                                share|improve this answer

























                                  6














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





                                  share|improve this answer























                                    6












                                    6








                                    6






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





                                    share|improve this answer












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






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 5 '13 at 12:27









                                    anubhava

                                    519k46315389




                                    519k46315389





















                                        3














                                        import java.util.regex.Matcher;
                                        import java.util.regex.Pattern;

                                        public class detectspecial
                                        public int getSpecialCharacterCount(String s)






                                        share|improve this answer




















                                        • this code always return 0
                                          – En NuNYet de Can CalÇadA
                                          May 7 '17 at 16:36















                                        3














                                        import java.util.regex.Matcher;
                                        import java.util.regex.Pattern;

                                        public class detectspecial
                                        public int getSpecialCharacterCount(String s)






                                        share|improve this answer




















                                        • this code always return 0
                                          – En NuNYet de Can CalÇadA
                                          May 7 '17 at 16:36













                                        3












                                        3








                                        3






                                        import java.util.regex.Matcher;
                                        import java.util.regex.Pattern;

                                        public class detectspecial
                                        public int getSpecialCharacterCount(String s)






                                        share|improve this answer












                                        import java.util.regex.Matcher;
                                        import java.util.regex.Pattern;

                                        public class detectspecial
                                        public int getSpecialCharacterCount(String s)







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        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
















                                        • 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











                                        3














                                        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.






                                        share|improve this answer






















                                        • 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















                                        3














                                        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.






                                        share|improve this answer






















                                        • 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













                                        3












                                        3








                                        3






                                        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.






                                        share|improve this answer














                                        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.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        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
















                                        • 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











                                        2














                                        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.






                                        share|improve this answer






















                                        • 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 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










                                        • 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















                                        2














                                        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.






                                        share|improve this answer






















                                        • 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 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










                                        • 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













                                        2












                                        2








                                        2






                                        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.






                                        share|improve this answer














                                        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.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








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










                                        • 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










                                        • 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 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










                                        • 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











                                        2














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






                                        share|improve this answer

























                                          2














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






                                          share|improve this answer























                                            2












                                            2








                                            2






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






                                            share|improve this answer












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







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Jul 4 '16 at 6:41









                                            sam

                                            438




                                            438





















                                                2














                                                Here is my regex variant of a special character:



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


                                                (Java code)






                                                share|improve this answer
















                                                • 2




                                                  you missed •☺○♣♥☻☺ and more..
                                                  – Aks4125
                                                  Dec 30 '17 at 4:58
















                                                2














                                                Here is my regex variant of a special character:



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


                                                (Java code)






                                                share|improve this answer
















                                                • 2




                                                  you missed •☺○♣♥☻☺ and more..
                                                  – Aks4125
                                                  Dec 30 '17 at 4:58














                                                2












                                                2








                                                2






                                                Here is my regex variant of a special character:



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


                                                (Java code)






                                                share|improve this answer












                                                Here is my regex variant of a special character:



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


                                                (Java code)







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Mar 20 '17 at 14:52









                                                Chuck

                                                548711




                                                548711







                                                • 2




                                                  you missed •☺○♣♥☻☺ and more..
                                                  – Aks4125
                                                  Dec 30 '17 at 4:58













                                                • 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












                                                1














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






                                                share|improve this answer






















                                                • 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















                                                1














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






                                                share|improve this answer






















                                                • 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













                                                1












                                                1








                                                1






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






                                                share|improve this answer














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







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                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
















                                                • 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











                                                1














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



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





                                                share|improve this answer



























                                                  1














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



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





                                                  share|improve this answer

























                                                    1












                                                    1








                                                    1






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



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





                                                    share|improve this answer














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



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






                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Nov 12 at 12:05









                                                    Jan Černý

                                                    8121622




                                                    8121622










                                                    answered Nov 12 at 10:56









                                                    Suchismita Barik

                                                    112




                                                    112





















                                                        0














                                                        (^W$)



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






                                                        share|improve this answer

























                                                          0














                                                          (^W$)



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






                                                          share|improve this answer























                                                            0












                                                            0








                                                            0






                                                            (^W$)



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






                                                            share|improve this answer












                                                            (^W$)



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







                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Aug 26 '16 at 9:18









                                                            Sareesh Krishnan

                                                            20626




                                                            20626





















                                                                0














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





                                                                share|improve this answer

























                                                                  0














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





                                                                  share|improve this answer























                                                                    0












                                                                    0








                                                                    0






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





                                                                    share|improve this answer












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






                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Sep 20 at 12:18









                                                                    KayV

                                                                    3,65122054




                                                                    3,65122054



























                                                                        draft saved

                                                                        draft discarded
















































                                                                        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.




                                                                        draft saved


                                                                        draft discarded














                                                                        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





















































                                                                        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







                                                                        這個網誌中的熱門文章

                                                                        What does pagestruct do in Eviews?

                                                                        Dutch intervention in Lombok and Karangasem

                                                                        Channel Islands