How to match a string in this way?











up vote
0
down vote

favorite












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















up vote
0
down vote

favorite












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













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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

305k16123200




305k16123200










asked Nov 10 at 18:19









AyhamSYR

72




72








  • 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

















up vote
1
down vote



accepted










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






























    up vote
    0
    down vote













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








      up vote
      1
      down vote



      accepted










      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



























        up vote
        1
        down vote



        accepted










        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

























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






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




          3,04131628
























              up vote
              0
              down vote













              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



























                up vote
                0
                down vote













                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

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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

                  19.4k71323




                  19.4k71323






























                      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







                      Popular posts from this blog

                      The Sandy Post

                      Danny Elfman

                      Pages that link to "Head v. Amoskeag Manufacturing Co."