Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Regular Expression for numbers

Posted on 2006-11-30
Medium Priority
178 Views
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
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

LVL 15

Expert Comment

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

LVL 11

Accepted Solution

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

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

LVL 15

Expert Comment

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 64

Expert Comment

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

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

LVL 4

Author Comment

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

LVL 11

Expert Comment

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

## Featured Post

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
###### Suggested Courses
Course of the Month9 days, 11 hours left to enroll