Character Convert Regex in Java





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to make a text converter for file names with regex, in this text there are some different dash types and hyphen types minus signs etc.etc. I want to convert them all to just a simple hyphen.



there are several types of different dashes and their unicodes are here:



u2212 u002D u05BE u2011 u2012 u2013 u2014 u2015 u207B u208B u2212 uFE58 uFE63 uFF0D 


if a character is one of these unicodes, then I want to convert it to simple hyphen which is u2010 with regex.



How can make this character convert regex with java ?



At first I was checking the file name is valid or not like this:



private static final Pattern FILE_NAME = Pattern.compile("^[a-zA-Z0-9_\u2212\u002D\u00AD\u05BE\u2010\u2011\u2012\u2013\u2014\u2015\u207B\u208B\u2212\uFE58\uFE63\uFF0D\.\s\,\[\]()\{\}]+$");

if (!FILE_NAME.matcher(file.getFilename()).matches()) {
throw new FileValidationException(FILE_NAME_INVALID);
}


but now I don't want to validate it I just want to replace characters with hyphen u2010










share|improve this question































    0















    I am trying to make a text converter for file names with regex, in this text there are some different dash types and hyphen types minus signs etc.etc. I want to convert them all to just a simple hyphen.



    there are several types of different dashes and their unicodes are here:



    u2212 u002D u05BE u2011 u2012 u2013 u2014 u2015 u207B u208B u2212 uFE58 uFE63 uFF0D 


    if a character is one of these unicodes, then I want to convert it to simple hyphen which is u2010 with regex.



    How can make this character convert regex with java ?



    At first I was checking the file name is valid or not like this:



    private static final Pattern FILE_NAME = Pattern.compile("^[a-zA-Z0-9_\u2212\u002D\u00AD\u05BE\u2010\u2011\u2012\u2013\u2014\u2015\u207B\u208B\u2212\uFE58\uFE63\uFF0D\.\s\,\[\]()\{\}]+$");

    if (!FILE_NAME.matcher(file.getFilename()).matches()) {
    throw new FileValidationException(FILE_NAME_INVALID);
    }


    but now I don't want to validate it I just want to replace characters with hyphen u2010










    share|improve this question



























      0












      0








      0








      I am trying to make a text converter for file names with regex, in this text there are some different dash types and hyphen types minus signs etc.etc. I want to convert them all to just a simple hyphen.



      there are several types of different dashes and their unicodes are here:



      u2212 u002D u05BE u2011 u2012 u2013 u2014 u2015 u207B u208B u2212 uFE58 uFE63 uFF0D 


      if a character is one of these unicodes, then I want to convert it to simple hyphen which is u2010 with regex.



      How can make this character convert regex with java ?



      At first I was checking the file name is valid or not like this:



      private static final Pattern FILE_NAME = Pattern.compile("^[a-zA-Z0-9_\u2212\u002D\u00AD\u05BE\u2010\u2011\u2012\u2013\u2014\u2015\u207B\u208B\u2212\uFE58\uFE63\uFF0D\.\s\,\[\]()\{\}]+$");

      if (!FILE_NAME.matcher(file.getFilename()).matches()) {
      throw new FileValidationException(FILE_NAME_INVALID);
      }


      but now I don't want to validate it I just want to replace characters with hyphen u2010










      share|improve this question
















      I am trying to make a text converter for file names with regex, in this text there are some different dash types and hyphen types minus signs etc.etc. I want to convert them all to just a simple hyphen.



      there are several types of different dashes and their unicodes are here:



      u2212 u002D u05BE u2011 u2012 u2013 u2014 u2015 u207B u208B u2212 uFE58 uFE63 uFF0D 


      if a character is one of these unicodes, then I want to convert it to simple hyphen which is u2010 with regex.



      How can make this character convert regex with java ?



      At first I was checking the file name is valid or not like this:



      private static final Pattern FILE_NAME = Pattern.compile("^[a-zA-Z0-9_\u2212\u002D\u00AD\u05BE\u2010\u2011\u2012\u2013\u2014\u2015\u207B\u208B\u2212\uFE58\uFE63\uFF0D\.\s\,\[\]()\{\}]+$");

      if (!FILE_NAME.matcher(file.getFilename()).matches()) {
      throw new FileValidationException(FILE_NAME_INVALID);
      }


      but now I don't want to validate it I just want to replace characters with hyphen u2010







      java regex character






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 13:08









      Yolgie

      222314




      222314










      asked Jan 3 at 11:50









      Chris GarsonnChris Garsonn

      576




      576
























          4 Answers
          4






          active

          oldest

          votes


















          1














          It should look like:



          name = name.replaceAll("[u2212u002Du00ADu05BE"
          + "u2011u2012u2013u2014u2015u207Bu208Bu2212uFE58uFE63uFF0D]", "u2010");


          Here u2212 are in java chars with the 4-digit hexadecimal values of UTF-16.
          There is no magic, u0063 is the same as c like in public u0063lass X { }.



          Resource for dash like Unicode.






          share|improve this answer































            3














            Use <TargetString>.replaceAll(<pattern>, <replacement>) (See: Java 8 API: String)



            In your case this would be: file.getFilename().replaceAll(FILE_NAME.toString(), "u2010")






            share|improve this answer


























            • But I would strongly suggest a different variable name for the pattern...

              – Yolgie
              Jan 3 at 12:08











            • thanks but here I want to only convert dashes minus signs that I mentioned whose unicodes, not letters so can you right the regex pattern correctly ?

              – Chris Garsonn
              Jan 3 at 12:12











            • just remove them from your regex, just keep the stuff in there that you want replaced

              – Yolgie
              Jan 3 at 12:13



















            1














            You can do it like this:



            fileName.replaceAll("u2212|u002D|u05BE|u2011|u2012|u2013|u2014|u2015|u207B|u208B|u2212|uFE58|uFE63|uFF0D", "u2010");





            share|improve this answer































              0














              Call the replaceAll() method on file.getFilename() as shown below:



              file.getFilename().replaceAll(FILE_NAME.toString() ,"\u2010");





              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%2f54021718%2fcharacter-convert-regex-in-java%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                It should look like:



                name = name.replaceAll("[u2212u002Du00ADu05BE"
                + "u2011u2012u2013u2014u2015u207Bu208Bu2212uFE58uFE63uFF0D]", "u2010");


                Here u2212 are in java chars with the 4-digit hexadecimal values of UTF-16.
                There is no magic, u0063 is the same as c like in public u0063lass X { }.



                Resource for dash like Unicode.






                share|improve this answer




























                  1














                  It should look like:



                  name = name.replaceAll("[u2212u002Du00ADu05BE"
                  + "u2011u2012u2013u2014u2015u207Bu208Bu2212uFE58uFE63uFF0D]", "u2010");


                  Here u2212 are in java chars with the 4-digit hexadecimal values of UTF-16.
                  There is no magic, u0063 is the same as c like in public u0063lass X { }.



                  Resource for dash like Unicode.






                  share|improve this answer


























                    1












                    1








                    1







                    It should look like:



                    name = name.replaceAll("[u2212u002Du00ADu05BE"
                    + "u2011u2012u2013u2014u2015u207Bu208Bu2212uFE58uFE63uFF0D]", "u2010");


                    Here u2212 are in java chars with the 4-digit hexadecimal values of UTF-16.
                    There is no magic, u0063 is the same as c like in public u0063lass X { }.



                    Resource for dash like Unicode.






                    share|improve this answer













                    It should look like:



                    name = name.replaceAll("[u2212u002Du00ADu05BE"
                    + "u2011u2012u2013u2014u2015u207Bu208Bu2212uFE58uFE63uFF0D]", "u2010");


                    Here u2212 are in java chars with the 4-digit hexadecimal values of UTF-16.
                    There is no magic, u0063 is the same as c like in public u0063lass X { }.



                    Resource for dash like Unicode.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 3 at 12:21









                    Joop EggenJoop Eggen

                    79k755105




                    79k755105

























                        3














                        Use <TargetString>.replaceAll(<pattern>, <replacement>) (See: Java 8 API: String)



                        In your case this would be: file.getFilename().replaceAll(FILE_NAME.toString(), "u2010")






                        share|improve this answer


























                        • But I would strongly suggest a different variable name for the pattern...

                          – Yolgie
                          Jan 3 at 12:08











                        • thanks but here I want to only convert dashes minus signs that I mentioned whose unicodes, not letters so can you right the regex pattern correctly ?

                          – Chris Garsonn
                          Jan 3 at 12:12











                        • just remove them from your regex, just keep the stuff in there that you want replaced

                          – Yolgie
                          Jan 3 at 12:13
















                        3














                        Use <TargetString>.replaceAll(<pattern>, <replacement>) (See: Java 8 API: String)



                        In your case this would be: file.getFilename().replaceAll(FILE_NAME.toString(), "u2010")






                        share|improve this answer


























                        • But I would strongly suggest a different variable name for the pattern...

                          – Yolgie
                          Jan 3 at 12:08











                        • thanks but here I want to only convert dashes minus signs that I mentioned whose unicodes, not letters so can you right the regex pattern correctly ?

                          – Chris Garsonn
                          Jan 3 at 12:12











                        • just remove them from your regex, just keep the stuff in there that you want replaced

                          – Yolgie
                          Jan 3 at 12:13














                        3












                        3








                        3







                        Use <TargetString>.replaceAll(<pattern>, <replacement>) (See: Java 8 API: String)



                        In your case this would be: file.getFilename().replaceAll(FILE_NAME.toString(), "u2010")






                        share|improve this answer















                        Use <TargetString>.replaceAll(<pattern>, <replacement>) (See: Java 8 API: String)



                        In your case this would be: file.getFilename().replaceAll(FILE_NAME.toString(), "u2010")







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jan 3 at 12:43









                        Tejas Chandrashekhar

                        7915




                        7915










                        answered Jan 3 at 12:04









                        YolgieYolgie

                        222314




                        222314













                        • But I would strongly suggest a different variable name for the pattern...

                          – Yolgie
                          Jan 3 at 12:08











                        • thanks but here I want to only convert dashes minus signs that I mentioned whose unicodes, not letters so can you right the regex pattern correctly ?

                          – Chris Garsonn
                          Jan 3 at 12:12











                        • just remove them from your regex, just keep the stuff in there that you want replaced

                          – Yolgie
                          Jan 3 at 12:13



















                        • But I would strongly suggest a different variable name for the pattern...

                          – Yolgie
                          Jan 3 at 12:08











                        • thanks but here I want to only convert dashes minus signs that I mentioned whose unicodes, not letters so can you right the regex pattern correctly ?

                          – Chris Garsonn
                          Jan 3 at 12:12











                        • just remove them from your regex, just keep the stuff in there that you want replaced

                          – Yolgie
                          Jan 3 at 12:13

















                        But I would strongly suggest a different variable name for the pattern...

                        – Yolgie
                        Jan 3 at 12:08





                        But I would strongly suggest a different variable name for the pattern...

                        – Yolgie
                        Jan 3 at 12:08













                        thanks but here I want to only convert dashes minus signs that I mentioned whose unicodes, not letters so can you right the regex pattern correctly ?

                        – Chris Garsonn
                        Jan 3 at 12:12





                        thanks but here I want to only convert dashes minus signs that I mentioned whose unicodes, not letters so can you right the regex pattern correctly ?

                        – Chris Garsonn
                        Jan 3 at 12:12













                        just remove them from your regex, just keep the stuff in there that you want replaced

                        – Yolgie
                        Jan 3 at 12:13





                        just remove them from your regex, just keep the stuff in there that you want replaced

                        – Yolgie
                        Jan 3 at 12:13











                        1














                        You can do it like this:



                        fileName.replaceAll("u2212|u002D|u05BE|u2011|u2012|u2013|u2014|u2015|u207B|u208B|u2212|uFE58|uFE63|uFF0D", "u2010");





                        share|improve this answer




























                          1














                          You can do it like this:



                          fileName.replaceAll("u2212|u002D|u05BE|u2011|u2012|u2013|u2014|u2015|u207B|u208B|u2212|uFE58|uFE63|uFF0D", "u2010");





                          share|improve this answer


























                            1












                            1








                            1







                            You can do it like this:



                            fileName.replaceAll("u2212|u002D|u05BE|u2011|u2012|u2013|u2014|u2015|u207B|u208B|u2212|uFE58|uFE63|uFF0D", "u2010");





                            share|improve this answer













                            You can do it like this:



                            fileName.replaceAll("u2212|u002D|u05BE|u2011|u2012|u2013|u2014|u2015|u207B|u208B|u2212|uFE58|uFE63|uFF0D", "u2010");






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 3 at 12:13









                            HPCSHPCS

                            730619




                            730619























                                0














                                Call the replaceAll() method on file.getFilename() as shown below:



                                file.getFilename().replaceAll(FILE_NAME.toString() ,"\u2010");





                                share|improve this answer




























                                  0














                                  Call the replaceAll() method on file.getFilename() as shown below:



                                  file.getFilename().replaceAll(FILE_NAME.toString() ,"\u2010");





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Call the replaceAll() method on file.getFilename() as shown below:



                                    file.getFilename().replaceAll(FILE_NAME.toString() ,"\u2010");





                                    share|improve this answer













                                    Call the replaceAll() method on file.getFilename() as shown below:



                                    file.getFilename().replaceAll(FILE_NAME.toString() ,"\u2010");






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 3 at 12:18









                                    Tejas ChandrashekharTejas Chandrashekhar

                                    7915




                                    7915






























                                        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.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021718%2fcharacter-convert-regex-in-java%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







                                        Popular posts from this blog

                                        MongoDB - Not Authorized To Execute Command

                                        How to fix TextFormField cause rebuild widget in Flutter

                                        in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith