Regular Expression Ideas

I need to validate input into a textblock, and I'm I'm think a regular expression might be suitable.

The criteria is as follows:

numeric . (AV or AI or SCH)numeric

e.g. 1234.AV4321 or 18327.AI1928 or 10293.SCH1928

The first digit of the numeric parts can not be a zero. 01929.AV01929 is not a valid input.

Any ideas?
LVL 1
wint100Asked:
Who is Participating?
 
wdosanjosCommented:
The Regex to validate that pattern is as follows:

var pattern = new Regex(@"^[1-9]\d*\.((AV)|(AI)|(SCH))[1-9]\d*$");

Here is a sample test:

var pattern = new Regex(@"^[1-9]\d*\.((AV)|(AI)|(SCH))[1-9]\d*$");
var values = new string[]{"1234.AV4321","18327.AI1928","10293.SCH1928","01929.AV01929","1929.AV01929","01929.AV1929"};

foreach (var value in values)
{
	Console.WriteLine("{0} = {1}", value, pattern.IsMatch(value));
}

Open in new window


Output:

1234.AV4321 = True
18327.AI1928 = True
10293.SCH1928 = True
01929.AV01929 = False
1929.AV01929 = False
01929.AV1929 = False

Open in new window


I hope this helps.
0
 
dj_alikCommented:
Play with this tool:
Expresso 3.0
http://www.ultrapico.com/Expresso.htm
0
 
käµfm³d 👽Commented:
Try:
^[1-9]\d*\.(?:AV|AI|SCH)\d+$

Open in new window

0
 
käµfm³d 👽Commented:
Correction:
^[1-9]\d*\.(?:AV|AI|SCH)[1-9]\d*$

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.