How to match a string in this way?












0














I need to check if a String matches this specific pattern.



The pattern is:




(Numbers)(all characters allowed)(numbers)




and the numbers may have a comma ("." or ",")!



For instance the input could be 500+400 or 400,021+213.443.



I tried Pattern.matches("[0-9],?.?+[0-9],?.?+", theequation2), but it didn't work!



I know that I have to use the method Pattern.match(regex, String), but I am not being able to find the correct regex.










share|improve this question




















  • 2




    please, give possible input and output, and show your attempt.
    – The Scientific Method
    Nov 10 at 18:21










  • Done! I have added them"
    – AyhamSYR
    Nov 10 at 18:26










  • If "all characters are allowed", then there is no way to know the start of the 2nd numbers group (as numbers are a subset of "all"). So there must be a better differentiation here.
    – KevinO
    Nov 10 at 18:39










  • Could you give an example please?
    – AyhamSYR
    Nov 10 at 18:41










  • Your requirement is too vague. You might as well use s.matches("\d.*\d") to match a string starting and ending with a digit and having any 0+ chars in between.
    – Wiktor Stribiżew
    Nov 10 at 20:13
















0














I need to check if a String matches this specific pattern.



The pattern is:




(Numbers)(all characters allowed)(numbers)




and the numbers may have a comma ("." or ",")!



For instance the input could be 500+400 or 400,021+213.443.



I tried Pattern.matches("[0-9],?.?+[0-9],?.?+", theequation2), but it didn't work!



I know that I have to use the method Pattern.match(regex, String), but I am not being able to find the correct regex.










share|improve this question




















  • 2




    please, give possible input and output, and show your attempt.
    – The Scientific Method
    Nov 10 at 18:21










  • Done! I have added them"
    – AyhamSYR
    Nov 10 at 18:26










  • If "all characters are allowed", then there is no way to know the start of the 2nd numbers group (as numbers are a subset of "all"). So there must be a better differentiation here.
    – KevinO
    Nov 10 at 18:39










  • Could you give an example please?
    – AyhamSYR
    Nov 10 at 18:41










  • Your requirement is too vague. You might as well use s.matches("\d.*\d") to match a string starting and ending with a digit and having any 0+ chars in between.
    – Wiktor Stribiżew
    Nov 10 at 20:13














0












0








0







I need to check if a String matches this specific pattern.



The pattern is:




(Numbers)(all characters allowed)(numbers)




and the numbers may have a comma ("." or ",")!



For instance the input could be 500+400 or 400,021+213.443.



I tried Pattern.matches("[0-9],?.?+[0-9],?.?+", theequation2), but it didn't work!



I know that I have to use the method Pattern.match(regex, String), but I am not being able to find the correct regex.










share|improve this question















I need to check if a String matches this specific pattern.



The pattern is:




(Numbers)(all characters allowed)(numbers)




and the numbers may have a comma ("." or ",")!



For instance the input could be 500+400 or 400,021+213.443.



I tried Pattern.matches("[0-9],?.?+[0-9],?.?+", theequation2), but it didn't work!



I know that I have to use the method Pattern.match(regex, String), but I am not being able to find the correct regex.







java regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 20:11









Wiktor Stribiżew

307k16126202




307k16126202










asked Nov 10 at 18:19









AyhamSYR

53




53








  • 2




    please, give possible input and output, and show your attempt.
    – The Scientific Method
    Nov 10 at 18:21










  • Done! I have added them"
    – AyhamSYR
    Nov 10 at 18:26










  • If "all characters are allowed", then there is no way to know the start of the 2nd numbers group (as numbers are a subset of "all"). So there must be a better differentiation here.
    – KevinO
    Nov 10 at 18:39










  • Could you give an example please?
    – AyhamSYR
    Nov 10 at 18:41










  • Your requirement is too vague. You might as well use s.matches("\d.*\d") to match a string starting and ending with a digit and having any 0+ chars in between.
    – Wiktor Stribiżew
    Nov 10 at 20:13














  • 2




    please, give possible input and output, and show your attempt.
    – The Scientific Method
    Nov 10 at 18:21










  • Done! I have added them"
    – AyhamSYR
    Nov 10 at 18:26










  • If "all characters are allowed", then there is no way to know the start of the 2nd numbers group (as numbers are a subset of "all"). So there must be a better differentiation here.
    – KevinO
    Nov 10 at 18:39










  • Could you give an example please?
    – AyhamSYR
    Nov 10 at 18:41










  • Your requirement is too vague. You might as well use s.matches("\d.*\d") to match a string starting and ending with a digit and having any 0+ chars in between.
    – Wiktor Stribiżew
    Nov 10 at 20:13








2




2




please, give possible input and output, and show your attempt.
– The Scientific Method
Nov 10 at 18:21




please, give possible input and output, and show your attempt.
– The Scientific Method
Nov 10 at 18:21












Done! I have added them"
– AyhamSYR
Nov 10 at 18:26




Done! I have added them"
– AyhamSYR
Nov 10 at 18:26












If "all characters are allowed", then there is no way to know the start of the 2nd numbers group (as numbers are a subset of "all"). So there must be a better differentiation here.
– KevinO
Nov 10 at 18:39




If "all characters are allowed", then there is no way to know the start of the 2nd numbers group (as numbers are a subset of "all"). So there must be a better differentiation here.
– KevinO
Nov 10 at 18:39












Could you give an example please?
– AyhamSYR
Nov 10 at 18:41




Could you give an example please?
– AyhamSYR
Nov 10 at 18:41












Your requirement is too vague. You might as well use s.matches("\d.*\d") to match a string starting and ending with a digit and having any 0+ chars in between.
– Wiktor Stribiżew
Nov 10 at 20:13




Your requirement is too vague. You might as well use s.matches("\d.*\d") to match a string starting and ending with a digit and having any 0+ chars in between.
– Wiktor Stribiżew
Nov 10 at 20:13












2 Answers
2






active

oldest

votes


















1














Dealing with numbers can be difficult. This approach will deal with your examples, but check carefully. I also didn't do "all characters" in the middle grouping, as "all" would include numbers, so instead I assumed that finding the next non-number would be appropriate.



This Java regex handles the requirements:



"((-?)[\d,.]+)([^\d-]+)((-?)[\d,.]+)"


However, there is a potential issue in the above. Consider the following:
300 - -200. The foregoing won't match that case.



Now, based upon the examples, I think the point is that one should have a valid operator. The number of math operations is likely limited, so I would whitelist the operators in the middle. Thus, something like:



"((-?)[\d,.]+)([\s]*[*/+-]+[\s]*)((-?)[\d,.]+)"


Would, I think, be more appropriate. The [*/+-] can be expanded for the power operator ^ or whatever. Now, if one is going to start adding words (such as mod) in the equation, then the expression will need to be modified.



You can see this regular expression here






share|improve this answer































    0














    In your regex you have to escape the dot . to match it literally and escape the + or else it would make the ? a possessive quantifier. To match 1+ digits you have to use a quantifier [0-9]+



    For your example data, you could match 1+ digits followed by an optional part which matches either a dot or a comma at the start and at the end. If you want to match 1 time any character you could use a dot.



    Instead of using a dot, you could also use for example a character class [-+*] to list some operators or list what you would allow to match. If this should be the only match, you could use anchors to assert the start ^ and the end $ of the string.



    d+(?:[.,]d+)?.d+(?:[.,]d+)?


    In Java:



    String regex = "\d+(?:[.,]\d+)?.\d+(?:[.,]\d+)?";


    Regex demo



    That would match:





    • d+(?:[.,]d+)? 1+ digits followed by an optional part that matches . or , followed by 1+ digits


    • . Match any character (Use .+) to repeat 1+ times

    • Same as the first pattern






    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%2f53242034%2fhow-to-match-a-string-in-this-way%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









      1














      Dealing with numbers can be difficult. This approach will deal with your examples, but check carefully. I also didn't do "all characters" in the middle grouping, as "all" would include numbers, so instead I assumed that finding the next non-number would be appropriate.



      This Java regex handles the requirements:



      "((-?)[\d,.]+)([^\d-]+)((-?)[\d,.]+)"


      However, there is a potential issue in the above. Consider the following:
      300 - -200. The foregoing won't match that case.



      Now, based upon the examples, I think the point is that one should have a valid operator. The number of math operations is likely limited, so I would whitelist the operators in the middle. Thus, something like:



      "((-?)[\d,.]+)([\s]*[*/+-]+[\s]*)((-?)[\d,.]+)"


      Would, I think, be more appropriate. The [*/+-] can be expanded for the power operator ^ or whatever. Now, if one is going to start adding words (such as mod) in the equation, then the expression will need to be modified.



      You can see this regular expression here






      share|improve this answer




























        1














        Dealing with numbers can be difficult. This approach will deal with your examples, but check carefully. I also didn't do "all characters" in the middle grouping, as "all" would include numbers, so instead I assumed that finding the next non-number would be appropriate.



        This Java regex handles the requirements:



        "((-?)[\d,.]+)([^\d-]+)((-?)[\d,.]+)"


        However, there is a potential issue in the above. Consider the following:
        300 - -200. The foregoing won't match that case.



        Now, based upon the examples, I think the point is that one should have a valid operator. The number of math operations is likely limited, so I would whitelist the operators in the middle. Thus, something like:



        "((-?)[\d,.]+)([\s]*[*/+-]+[\s]*)((-?)[\d,.]+)"


        Would, I think, be more appropriate. The [*/+-] can be expanded for the power operator ^ or whatever. Now, if one is going to start adding words (such as mod) in the equation, then the expression will need to be modified.



        You can see this regular expression here






        share|improve this answer


























          1












          1








          1






          Dealing with numbers can be difficult. This approach will deal with your examples, but check carefully. I also didn't do "all characters" in the middle grouping, as "all" would include numbers, so instead I assumed that finding the next non-number would be appropriate.



          This Java regex handles the requirements:



          "((-?)[\d,.]+)([^\d-]+)((-?)[\d,.]+)"


          However, there is a potential issue in the above. Consider the following:
          300 - -200. The foregoing won't match that case.



          Now, based upon the examples, I think the point is that one should have a valid operator. The number of math operations is likely limited, so I would whitelist the operators in the middle. Thus, something like:



          "((-?)[\d,.]+)([\s]*[*/+-]+[\s]*)((-?)[\d,.]+)"


          Would, I think, be more appropriate. The [*/+-] can be expanded for the power operator ^ or whatever. Now, if one is going to start adding words (such as mod) in the equation, then the expression will need to be modified.



          You can see this regular expression here






          share|improve this answer














          Dealing with numbers can be difficult. This approach will deal with your examples, but check carefully. I also didn't do "all characters" in the middle grouping, as "all" would include numbers, so instead I assumed that finding the next non-number would be appropriate.



          This Java regex handles the requirements:



          "((-?)[\d,.]+)([^\d-]+)((-?)[\d,.]+)"


          However, there is a potential issue in the above. Consider the following:
          300 - -200. The foregoing won't match that case.



          Now, based upon the examples, I think the point is that one should have a valid operator. The number of math operations is likely limited, so I would whitelist the operators in the middle. Thus, something like:



          "((-?)[\d,.]+)([\s]*[*/+-]+[\s]*)((-?)[\d,.]+)"


          Would, I think, be more appropriate. The [*/+-] can be expanded for the power operator ^ or whatever. Now, if one is going to start adding words (such as mod) in the equation, then the expression will need to be modified.



          You can see this regular expression here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 19:00

























          answered Nov 10 at 18:42









          KevinO

          3,07131628




          3,07131628

























              0














              In your regex you have to escape the dot . to match it literally and escape the + or else it would make the ? a possessive quantifier. To match 1+ digits you have to use a quantifier [0-9]+



              For your example data, you could match 1+ digits followed by an optional part which matches either a dot or a comma at the start and at the end. If you want to match 1 time any character you could use a dot.



              Instead of using a dot, you could also use for example a character class [-+*] to list some operators or list what you would allow to match. If this should be the only match, you could use anchors to assert the start ^ and the end $ of the string.



              d+(?:[.,]d+)?.d+(?:[.,]d+)?


              In Java:



              String regex = "\d+(?:[.,]\d+)?.\d+(?:[.,]\d+)?";


              Regex demo



              That would match:





              • d+(?:[.,]d+)? 1+ digits followed by an optional part that matches . or , followed by 1+ digits


              • . Match any character (Use .+) to repeat 1+ times

              • Same as the first pattern






              share|improve this answer




























                0














                In your regex you have to escape the dot . to match it literally and escape the + or else it would make the ? a possessive quantifier. To match 1+ digits you have to use a quantifier [0-9]+



                For your example data, you could match 1+ digits followed by an optional part which matches either a dot or a comma at the start and at the end. If you want to match 1 time any character you could use a dot.



                Instead of using a dot, you could also use for example a character class [-+*] to list some operators or list what you would allow to match. If this should be the only match, you could use anchors to assert the start ^ and the end $ of the string.



                d+(?:[.,]d+)?.d+(?:[.,]d+)?


                In Java:



                String regex = "\d+(?:[.,]\d+)?.\d+(?:[.,]\d+)?";


                Regex demo



                That would match:





                • d+(?:[.,]d+)? 1+ digits followed by an optional part that matches . or , followed by 1+ digits


                • . Match any character (Use .+) to repeat 1+ times

                • Same as the first pattern






                share|improve this answer


























                  0












                  0








                  0






                  In your regex you have to escape the dot . to match it literally and escape the + or else it would make the ? a possessive quantifier. To match 1+ digits you have to use a quantifier [0-9]+



                  For your example data, you could match 1+ digits followed by an optional part which matches either a dot or a comma at the start and at the end. If you want to match 1 time any character you could use a dot.



                  Instead of using a dot, you could also use for example a character class [-+*] to list some operators or list what you would allow to match. If this should be the only match, you could use anchors to assert the start ^ and the end $ of the string.



                  d+(?:[.,]d+)?.d+(?:[.,]d+)?


                  In Java:



                  String regex = "\d+(?:[.,]\d+)?.\d+(?:[.,]\d+)?";


                  Regex demo



                  That would match:





                  • d+(?:[.,]d+)? 1+ digits followed by an optional part that matches . or , followed by 1+ digits


                  • . Match any character (Use .+) to repeat 1+ times

                  • Same as the first pattern






                  share|improve this answer














                  In your regex you have to escape the dot . to match it literally and escape the + or else it would make the ? a possessive quantifier. To match 1+ digits you have to use a quantifier [0-9]+



                  For your example data, you could match 1+ digits followed by an optional part which matches either a dot or a comma at the start and at the end. If you want to match 1 time any character you could use a dot.



                  Instead of using a dot, you could also use for example a character class [-+*] to list some operators or list what you would allow to match. If this should be the only match, you could use anchors to assert the start ^ and the end $ of the string.



                  d+(?:[.,]d+)?.d+(?:[.,]d+)?


                  In Java:



                  String regex = "\d+(?:[.,]\d+)?.\d+(?:[.,]\d+)?";


                  Regex demo



                  That would match:





                  • d+(?:[.,]d+)? 1+ digits followed by an optional part that matches . or , followed by 1+ digits


                  • . Match any character (Use .+) to repeat 1+ times

                  • Same as the first pattern







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 13:35

























                  answered Nov 11 at 12:22









                  The fourth bird

                  20.4k71326




                  20.4k71326






























                      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%2f53242034%2fhow-to-match-a-string-in-this-way%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







                      這個網誌中的熱門文章

                      Academy of Television Arts & Sciences

                      L'Équipe

                      1995 France bombings