?
Solved

Regular Expression for numbers

Posted on 2006-11-30
8
Medium Priority
?
177 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 1000 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 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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