Solved

Regular Expression for numbers

Posted on 2006-11-30
8
170 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
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.

757 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

23 Experts available now in Live!

Get 1:1 Help Now