473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. private void button2_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.             //invalid input checking
  5.             if (regex_match(DelayA1Pulse6.Text))
  6.             {
  7.                 return;
  8.             }
  9.     }
  10.  
  11.  
  12. public static bool regex_match(string s)
  13.         {
  14.             Regex checkNumber = new Regex("/b/d{1,4}/b");
  15.             if (checkNumber.IsMatch(s))
  16.             {
  17.                 return false;
  18.  
  19.             }
  20.             else
  21.                 MessageBox.Show("Enter a one to four digit number!");
  22.                 return true;
  23.  
  24.         }
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.
Jan 27 '11 #1
8 4024
Rabbit
12,516 Expert Mod 8TB
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".
Jan 27 '11 #2
Rabbit
12,516 Expert Mod 8TB
You need to post your code in addition to any error messages you may have gotten along with test data and expected output.
Jan 27 '11 #3
Rabbit
12,516 Expert Mod 8TB
Try replacing the / with \. I think I had the slashes mixed up.
Jan 28 '11 #4
I don't understand. Do I need to put ("\.b\.d{1,4}\.b");
in as the regular expression?
Jan 30 '11 #5
Rabbit
12,516 Expert Mod 8TB
Get rid of the periods, the period was just to end the sentence.
Jan 30 '11 #6
Rabbit, It is giving me an error "Unregconized escape sequence"
Jan 31 '11 #7
Rabbit
12,516 Expert Mod 8TB
Can you post the current regex you have?
Jan 31 '11 #8
Imade it work with "\\b\\d{1,4}\\b" Thanks for your help.
Jan 31 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Derek Stone | last post by:
In my continuing inability to completely understand regular expressions I have a new one for you. I'd like to capture a string "A" unless it is anywhere in between string "B" and string "C". ...
4
by: Johnny Lee | last post by:
Hi, I've met a problem in match a regular expression in python. Hope any of you could help me. Here are the details: I have many tags like this: xxx<a href="http://xxx.xxx.xxx" xxx>xxx xxx<a...
1
by: seash | last post by:
Hi I need a regular expresssion which validates any kind of Phone numbers format..irrespective of the Country need hel ---seash
3
by: Jay Douglas | last post by:
Hello all. I'm having a hard time coming up with a regular expression that can extract just the numbers out of string into a GroupCollection. Any help is appreciated.
5
by: Paul Johnston | last post by:
Hi Just started using c# to do a task I usually use Perl to do and hit a limit in my knowledge :-) I have a program which reads from a large text file, extracts certain lines then gets a string...
17
by: Randy Webb | last post by:
I know that the /g flag will match all occurrences. Is there a way, with a Regular Expression, to match all occurrences *except* the last one? pattern = /df/g; var myString = "asdfasdfasdfasdf";...
2
by: teo | last post by:
match word with interpunctuation Hallo, I need to build a regular expression able to match a specified word, preceded and followed by few chars (mainly interpunctuation) Below the code. ...
2
by: =?Utf-8?B?QW5keQ==?= | last post by:
Hi. Has anyone a reliable regular expression for UK phone numbers? Done the usual google search and on regexlib but so far nothing seems close to actually working. Maybe of our stupid phone...
6
Markus
by: Markus | last post by:
I'm asking the question for once! Say I have the expression @@ that matches anything other than the characters specified. I also want spaces to be allows, including \n, \t, etc. I've tried adding...
2
by: Sal Sal | last post by:
If I have a string as follows XXXasdf23s5\r\n asdflkoirfn329i4\r\n sef29384ewrj28039\r\n XXX123sd3t334\r\n sdorfu23984rr\r\n sdflk2893rjf\r\n weirj2983jhwer2398\r\n XXX12356789\r\n
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.