Need some regex help here please :)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaolinman829
    New Member
    • Nov 2006
    • 7

    Need some regex help here please :)

    I suck at regex's and I need a little hope. Hopefully there is a guru out there to help me on this one. I want to do a match on the following:

    one or more numeric characters [0-9]
    zero or more blank spaces \s
    zero or more dash characters -

    I've tried everything I know for now, but I still can't get my unit test to pass my conditions above. Anyone out there who could help? Thanks so much.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by shaolinman829
    I suck at regex's and I need a little hope. Hopefully there is a guru out there to help me on this one. I want to do a match on the following:

    one or more numeric characters [0-9]
    zero or more blank spaces \s
    zero or more dash characters -

    I've tried everything I know for now, but I still can't get my unit test to pass my conditions above. Anyone out there who could help? Thanks so much.
    What does your program look like right now?

    Comment

    • shaolinman829
      New Member
      • Nov 2006
      • 7

      #3
      Originally posted by sicarie
      What does your program look like right now?
      Well, I've tried several things. Here are a few:

      mystring.matche s("(\\d*)?(-*)?(\\s*)?")

      and I've tried things this way:

      Pattern patter1 = Pattern.compile ("(\\d)+(\\s )*(-)*");
      Matcher m = patter1.matcher (mystring);
      return m.find();

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by shaolinman829
        Well, I've tried several things. Here are a few:

        mystring.matche s("(\\d*)?(-*)?(\\s*)?")

        and I've tried things this way:

        Pattern patter1 = Pattern.compile ("(\\d)+(\\s )*(-)*");
        Matcher m = patter1.matcher (mystring);
        return m.find();
        Are you specifically grouping them to pull them out? If not, you can omit the parentheses - have you tried [0-9]+ for the digits?

        Is there anything on the line either before or after that is matched? You might need a .* to continue the search through the end of the line...

        What are you trying to parse/find?

        Comment

        • shaolinman829
          New Member
          • Nov 2006
          • 7

          #5
          Originally posted by sicarie
          Are you specifically grouping them to pull them out? If not, you can omit the parentheses - have you tried [0-9]+ for the digits?

          Is there anything on the line either before or after that is matched? You might need a .* to continue the search through the end of the line...

          What are you trying to parse/find?
          I need to verify a serial number is correct. Ok this is what I have now:

          public static boolean validateValue(S tring value)
          {
          Pattern p = Pattern.compile ("[0-9]+[-]*[\\s]*");
          Matcher m = p.matcher(value );
          return m.find();
          }

          The thing is the string must contain as least 1 digit, but the "-" and " " characters are optional (there can be zero or more of them). No other characters are allowed. The "No other characters allowed" part is the main issue I've been having whenever I think I've solved the problem. Should I do another check of some sort before? I'm not sure how to do the nots for the optional "-" and " " characters when at least 1 digit is required.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by shaolinman829
            I need to verify a serial number is correct. Ok this is what I have now:

            public static boolean validateValue(S tring value)
            {
            Pattern p = Pattern.compile ("[0-9]+[-]*[\\s]*");
            Matcher m = p.matcher(value );
            return m.find();
            }

            The thing is the string must contain as least 1 digit, but the "-" and " " characters are optional (there can be zero or more of them). No other characters are allowed. The "No other characters allowed" part is the main issue I've been having whenever I think I've solved the problem. Should I do another check of some sort before? I'm not sure how to do the nots for the optional "-" and " " characters when at least 1 digit is required.
            Here's Java's regex tutorial.

            This Java tutorial describes exceptions, basic input/output, concurrency, regular expressions, and the platform environment


            Have you compiled that? It looks correct (without me actually creating tests for it and running it...). With regex's + means "1 or more" and * means "0 or more".
            At the moment, that regex will pick up

            1
            1-
            12
            12-
            1234567897654
            23456789765-

            Which is the way you are describing the serials (which seems a little odd to me). Are the integers allowed anywhere in between the '-' or the whitespace?

            I think you might want something like [0-9]+[-]*[0-9]*[\\s]*[0-9]* but I'm not sure - are there a few example serials?

            Comment

            • shaolinman829
              New Member
              • Nov 2006
              • 7

              #7
              Thanks so much for the help. Yes I compiled everything and it gives me correct output for any combination of white space, digits, and - characters (assuming the condition that there is at least 1 digit). The problem I am having is if someone enters (for example):

              1111-11 111-x111
              1x
              1-x
              --- x
              etc.

              (I will use the character 'x' here for example only, assume x is any non-digit, non-whitespace, non-dash character):
              If 'x' is anywhere in the string it should fail.

              And I don't write the requirements, I just code them :-)

              Comment

              • shaolinman829
                New Member
                • Nov 2006
                • 7

                #8
                This gives me what I need from my test and the conditions I posed above (although it would be nice to know how to do this in one statement):

                public static boolean validateValue1( String value)
                {
                Pattern p2 = Pattern.compile ("[^0-9-\\s]");
                Matcher m2 = p2.matcher(valu e);
                if(m2.find())
                {
                return false;
                }
                Pattern p = Pattern.compile ("[0-9]+[-]*[\\s]*");
                Matcher m = p.matcher(value );
                return m.find();
                }

                Thanks for all of your help!

                Comment

                Working...