What is a regular expression to match only four digit numbers?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Teresa Clark
    New Member
    • Jan 2011
    • 4

    What is a regular expression to match only four digit numbers?

    I am trying to find the syntax for using a regular expression to only match four digit numbers and there has to be at least one number. Any Ideas? :)

    I have a form with a textbox and I am trying to using regular expression
    matching to verify the form. I don't want the user to be able to leave the
    textbox blank, it must be a real number from 0 to 9999. The user can't
    leave the text box blank. The way it is working now is any number I put
    in the textbox it is popping up the MessageBox "Enter a one to four digit
    number!" If the user enters invalid data I want the button event to return
    to the form method and stop executing.


    Here's the code:
    Code:
    private void button2_Click(object sender, EventArgs e)
            {
               
                //invalid input checking
                if (regex_match(DelayA1Pulse6.Text))
                {
                    return;
                }
    	}
    
    
    public static bool regex_match(string s)
            {
                Regex checkNumber = new Regex("/b/d{1,4}/b");
                if (checkNumber.IsMatch(s))
                {
                    return false;
                    
                }
                else
                    MessageBox.Show("Enter a one to four digit number!");
                    return true;
    
            }
    The way it is working right now is any number I put in the textbox I am getting the error message box. I only want the message box to pop up when the user enters invalid input like 10000, blank, ASDF, 0100 etc.
    Last edited by Niheel; Jan 31 '11, 06:37 PM. Reason: The code and question details were merged so someone reading this question in the future can get the full question details.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    A 4 digit number by definition has exactly 4 numbers and no other characters so I don't know what you mean by it must have at least one number. But the expression for that would be "/b/d{4,4}/b". If what you mean is a number with at least one digit and at most 4 digits then the expression would be "/b/d{1,4}/b".

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      You need to post your code in addition to any error messages you may have gotten along with test data and expected output.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Try replacing the / with \. I think I had the slashes mixed up.

        Comment

        • Teresa Clark
          New Member
          • Jan 2011
          • 4

          #5
          I don't understand. Do I need to put ("\.b\.d{1,4}\. b");
          in as the regular expression?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Get rid of the periods, the period was just to end the sentence.

            Comment

            • Teresa Clark
              New Member
              • Jan 2011
              • 4

              #7
              Rabbit, It is giving me an error "Unregconiz ed escape sequence"

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                Can you post the current regex you have?

                Comment

                • Teresa Clark
                  New Member
                  • Jan 2011
                  • 4

                  #9
                  Imade it work with "\\b\\d{1,4}\\b " Thanks for your help.

                  Comment

                  Working...