Using Pattern and Matcher to search for special characters (Example: $)












0















Apologies if this has already been answered.



I am using the following code to search for a substring:



String subject = "ABC"
String subString = "AB"
Pattern pattern = Pattern.compile(subString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(subject);

while (matcher.find()){
//Matched
}


But when my subject string contains a $ in the beginning, it does not work since it is a special character.



String subject = "$ABC"
String subString = "$"


How does one handle that?










share|improve this question



























    0















    Apologies if this has already been answered.



    I am using the following code to search for a substring:



    String subject = "ABC"
    String subString = "AB"
    Pattern pattern = Pattern.compile(subString, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(subject);

    while (matcher.find()){
    //Matched
    }


    But when my subject string contains a $ in the beginning, it does not work since it is a special character.



    String subject = "$ABC"
    String subString = "$"


    How does one handle that?










    share|improve this question

























      0












      0








      0








      Apologies if this has already been answered.



      I am using the following code to search for a substring:



      String subject = "ABC"
      String subString = "AB"
      Pattern pattern = Pattern.compile(subString, Pattern.CASE_INSENSITIVE);
      Matcher matcher = pattern.matcher(subject);

      while (matcher.find()){
      //Matched
      }


      But when my subject string contains a $ in the beginning, it does not work since it is a special character.



      String subject = "$ABC"
      String subString = "$"


      How does one handle that?










      share|improve this question














      Apologies if this has already been answered.



      I am using the following code to search for a substring:



      String subject = "ABC"
      String subString = "AB"
      Pattern pattern = Pattern.compile(subString, Pattern.CASE_INSENSITIVE);
      Matcher matcher = pattern.matcher(subject);

      while (matcher.find()){
      //Matched
      }


      But when my subject string contains a $ in the beginning, it does not work since it is a special character.



      String subject = "$ABC"
      String subString = "$"


      How does one handle that?







      java regex pattern-matching






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 1:34









      SunnySunny

      2,700144578




      2,700144578
























          2 Answers
          2






          active

          oldest

          votes


















          3














          By escaping the special character in the subString. Like,



          String subString = "\$";


          or telling the Pattern to match literals. Like,



          Pattern pattern = Pattern.compile(subString, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);





          share|improve this answer



















          • 1





            If you don't know in advance what you're searching for, Pattern.quote is your friend.

            – Amadan
            Nov 20 '18 at 1:43



















          1














          There are few meta characters in regex. And some of them which are supported by regex in java are



          ( ) [ ] { {  ^ $ | ? * + . < > - = !


          So $ is a indeed meta character here. The meta character conveys special meaning to the regex engine and hence can't be use literally. So in order to use them you have to combine them with escape character which is backslash



          So String subject = "\$ABC"
          String subString = "\$"



          would do. Java uses double backslash instead of single for escape character unlike the other regex engine.






          share|improve this answer





















          • 2





            "Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.

            – racraman
            Nov 20 '18 at 1:54













          • yes. My bad I just forget it is the case of escape character

            – Brij Raj Kishore
            Nov 20 '18 at 1:57











          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%2f53385004%2fusing-pattern-and-matcher-to-search-for-special-characters-example%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          By escaping the special character in the subString. Like,



          String subString = "\$";


          or telling the Pattern to match literals. Like,



          Pattern pattern = Pattern.compile(subString, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);





          share|improve this answer



















          • 1





            If you don't know in advance what you're searching for, Pattern.quote is your friend.

            – Amadan
            Nov 20 '18 at 1:43
















          3














          By escaping the special character in the subString. Like,



          String subString = "\$";


          or telling the Pattern to match literals. Like,



          Pattern pattern = Pattern.compile(subString, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);





          share|improve this answer



















          • 1





            If you don't know in advance what you're searching for, Pattern.quote is your friend.

            – Amadan
            Nov 20 '18 at 1:43














          3












          3








          3







          By escaping the special character in the subString. Like,



          String subString = "\$";


          or telling the Pattern to match literals. Like,



          Pattern pattern = Pattern.compile(subString, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);





          share|improve this answer













          By escaping the special character in the subString. Like,



          String subString = "\$";


          or telling the Pattern to match literals. Like,



          Pattern pattern = Pattern.compile(subString, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 1:37









          Elliott FrischElliott Frisch

          154k1393182




          154k1393182








          • 1





            If you don't know in advance what you're searching for, Pattern.quote is your friend.

            – Amadan
            Nov 20 '18 at 1:43














          • 1





            If you don't know in advance what you're searching for, Pattern.quote is your friend.

            – Amadan
            Nov 20 '18 at 1:43








          1




          1





          If you don't know in advance what you're searching for, Pattern.quote is your friend.

          – Amadan
          Nov 20 '18 at 1:43





          If you don't know in advance what you're searching for, Pattern.quote is your friend.

          – Amadan
          Nov 20 '18 at 1:43













          1














          There are few meta characters in regex. And some of them which are supported by regex in java are



          ( ) [ ] { {  ^ $ | ? * + . < > - = !


          So $ is a indeed meta character here. The meta character conveys special meaning to the regex engine and hence can't be use literally. So in order to use them you have to combine them with escape character which is backslash



          So String subject = "\$ABC"
          String subString = "\$"



          would do. Java uses double backslash instead of single for escape character unlike the other regex engine.






          share|improve this answer





















          • 2





            "Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.

            – racraman
            Nov 20 '18 at 1:54













          • yes. My bad I just forget it is the case of escape character

            – Brij Raj Kishore
            Nov 20 '18 at 1:57
















          1














          There are few meta characters in regex. And some of them which are supported by regex in java are



          ( ) [ ] { {  ^ $ | ? * + . < > - = !


          So $ is a indeed meta character here. The meta character conveys special meaning to the regex engine and hence can't be use literally. So in order to use them you have to combine them with escape character which is backslash



          So String subject = "\$ABC"
          String subString = "\$"



          would do. Java uses double backslash instead of single for escape character unlike the other regex engine.






          share|improve this answer





















          • 2





            "Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.

            – racraman
            Nov 20 '18 at 1:54













          • yes. My bad I just forget it is the case of escape character

            – Brij Raj Kishore
            Nov 20 '18 at 1:57














          1












          1








          1







          There are few meta characters in regex. And some of them which are supported by regex in java are



          ( ) [ ] { {  ^ $ | ? * + . < > - = !


          So $ is a indeed meta character here. The meta character conveys special meaning to the regex engine and hence can't be use literally. So in order to use them you have to combine them with escape character which is backslash



          So String subject = "\$ABC"
          String subString = "\$"



          would do. Java uses double backslash instead of single for escape character unlike the other regex engine.






          share|improve this answer















          There are few meta characters in regex. And some of them which are supported by regex in java are



          ( ) [ ] { {  ^ $ | ? * + . < > - = !


          So $ is a indeed meta character here. The meta character conveys special meaning to the regex engine and hence can't be use literally. So in order to use them you have to combine them with escape character which is backslash



          So String subject = "\$ABC"
          String subString = "\$"



          would do. Java uses double backslash instead of single for escape character unlike the other regex engine.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 1:57

























          answered Nov 20 '18 at 1:45









          Brij Raj KishoreBrij Raj Kishore

          1,0201418




          1,0201418








          • 2





            "Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.

            – racraman
            Nov 20 '18 at 1:54













          • yes. My bad I just forget it is the case of escape character

            – Brij Raj Kishore
            Nov 20 '18 at 1:57














          • 2





            "Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.

            – racraman
            Nov 20 '18 at 1:54













          • yes. My bad I just forget it is the case of escape character

            – Brij Raj Kishore
            Nov 20 '18 at 1:57








          2




          2





          "Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.

          – racraman
          Nov 20 '18 at 1:54







          "Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.

          – racraman
          Nov 20 '18 at 1:54















          yes. My bad I just forget it is the case of escape character

          – Brij Raj Kishore
          Nov 20 '18 at 1:57





          yes. My bad I just forget it is the case of escape character

          – Brij Raj Kishore
          Nov 20 '18 at 1:57


















          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%2f53385004%2fusing-pattern-and-matcher-to-search-for-special-characters-example%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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()