Solved

Regular Expression for numbers

Posted on 2006-11-30
8
172 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connection String 16 55
designing in object programming 12 75
PrintingFoundIt(Almost!(Damn!)) 1 26
Need to start a web service from Visual Studio 2015 Pro 2 22
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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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