Solved

Regular Expression for numbers

Posted on 2006-11-30
8
171 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 62

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now