Solved

Regular Expression for numbers

Posted on 2006-11-30
8
174 Views
Last Modified: 2010-04-16
My problem seems simple but the solution has evaded me so far.  I want to validate that a number is in a set of possible numbers.

For example:
value = 22
possible values = (11,22,33,44)

Can anyone provide a regular expression that will validate 22 but not 2 or 222?  Does that make sense?
0
Comment
Question by:CBeach1980
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 18049860
(11|22|33|44) ?
0
 
LVL 11

Accepted Solution

by:
Expert1701 earned 250 total points
ID: 18049869
Like this?

  System.Text.RegularExpressions.Regex.Match("2", "^(11)|(22)|(33)|(44)$").Success //Returns false

  System.Text.RegularExpressions.Regex.Match("22", "^(11)|(22)|(33)|(44)$").Success //Returns true
0
 
LVL 15

Assisted Solution

by:ozymandias
ozymandias earned 250 total points
ID: 18049884
Regex r = new Regex("^(11|22|33|44)$");
Console.WriteLine(r.IsMatch("2").ToString());
Console.WriteLine(r.IsMatch("22").ToString());
Console.WriteLine(r.IsMatch("2222").ToString());

output :


False
True
False



0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 15

Expert Comment

by:ozymandias
ID: 18049947
Expert1701 :

Regex.Match("2", "^(11)|(22)|(33)|(44)$").Success;           <-- returns false
Regex.Match("22", "^(11)|(22)|(33)|(44)$").Success;         <-- returns true
Regex.Match("2222", "^(11)|(22)|(33)|(44)$").Success;      <-- returns true
Regex.Match("12344", "^(11)|(22)|(33)|(44)$").Success;    <-- returns true
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 18049968
Hi CBeach1980;

Here is some sample code to do what you want.

using System.Text.RegularExpressions;

        private void button2_Click(object sender, EventArgs e)
        {
            string possiblevalues = "11,22,33,44";
            MessageBox.Show(ValidateNumber(possiblevalues, textBox1.Text).ToString());
        }

        private Boolean ValidateNumber(string findin, string num)
        {
            num = num.Trim();
            try
            {
                int digits = Convert.ToInt32(num);
            }
            catch
            {
                return false;
            }

            return Regex.IsMatch(findin, @",?\s*(" + num + @")\s*,?");
        }


Fernando
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 18049971
You would need and extra group : "^((11)|(22)|(33)|(44))$" to make it work.
0
 
LVL 4

Author Comment

by:CBeach1980
ID: 18049982
Thank you both.  They both are good answers though ozymandias' is a little cleaner.
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 18050633
Whoops, thank you for the correction, ozymandias!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question