Link to home
Start Free TrialLog in
Avatar of pzozulka
pzozulka

asked on

RegEx help in C#.NET

We perform validation of policy numbers users provide to see if it matches existing Masks.

An example of a mask is: 'A12'4N

Anything inside single quotes is considered a string literal. The 4N represents 4 digits.

So if a user enters A125555, that would satisfy the mask since A12 is the literal followed by 4 numbers.

However, the code that validates this (below), is not picking up the numeric part of the string literal. I'm new to RegEx and any help to figure out handle the numerics inside the string literal would be greatly appreciated.

Thanks.

public bool ValidatePolicyNumber(string policyNumber)
{
	string i1Expression = Mask.Value.Replace("'", "");
	Regex regEx = null;
	Dictionary<string, string> i1RegExList = new Dictionary<string, string>() 
	{ 
		{ @"\d+N", @"\d{0}" },
		{ @"\d+A", @"[a-zA-Z]{0}" },
		{ @"\d+Z", @"[\d|a-z|A-Z]{0}" }
	};
	string i1RegEx, match, replacementRegEx;

	i1RegExList.Keys.ToList().ForEach(exp =>
	{
		var matches = new Regex(exp).Matches(i1Expression);
		for (int index = 0; index < matches.Count; index++)
		{
			i1RegEx = matches[index].Value;
			match = new Regex(@"\d+").Match(i1RegEx).Value;
			replacementRegEx = String.Format(i1RegExList[exp], "{" + match + "}");
			i1Expression = i1Expression.Replace(i1RegEx, replacementRegEx);
		}
	});

	return new Regex("^" + i1Expression + "$").IsMatch(policyNumber);
}

Open in new window

Avatar of Mlanda T
Mlanda T
Flag of South Africa image

What do the other Masks look like? I think it might be easier if you provided a few more examples of the Masks, along with examples of the policy numbers that match, and perhaps a few examples of policy numbers that don't. Then we can work from there.
Avatar of pzozulka
pzozulka

ASKER

Here are a few Masks with examples for each:

Mask                  Example
‘0’6N’38’              012345638
‘CAR’7N               CAR1234567
6N1A4N1A          123456Z1234Y
‘INT601’8N          INT60112345678
‘22’1A7N              22B1234567
SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice, I see it now. While waiting for your solution I came up with the following, and wondering if that would work just as well?

The only things that changed, is the introduction of a new variable called markWithoutLiterals on line 2 below.

string i1Expression = Mask.Value.Replace("'", "");
string maskWithoutLiterals = new Regex(@"\'([^']*)\'").Replace(Mask.Value, ""); // new line of code
Regex regEx = null;
Dictionary<string, string> i1RegExList = new Dictionary<string, string>() 
{ 
	{ @"\d+N", @"\d{0}" },
	{ @"\d+A", @"[a-zA-Z]{0}" },
	{ @"\d+Z", @"[\d|a-z|A-Z]{0}" }
};
string i1RegEx, match, replacementRegEx;

i1RegExList.Keys.ToList().ForEach(exp =>
{
	var matches = new Regex(exp).Matches(maskWithoutLiterals); // new variable used here
	for (int index = 0; index < matches.Count; index++)
	{
		i1RegEx = matches[index].Value;
		match = new Regex(@"\d+").Match(i1RegEx).Value;
		replacementRegEx = String.Format(i1RegExList[exp], "{" + match + "}");
		i1Expression = i1Expression.Replace(i1RegEx, replacementRegEx);
	}
});

return new Regex("^" + i1Expression + "$").IsMatch(policyNumber);

Open in new window

Test this with this one:

'221A'1A7N      221AB1234567
Neither of our suggestions work. I put a break point on the following line of code:

return new Regex("^" + i1Expression + "$").IsMatch(policyNumber);

i1Expression = 22[a-zA-Z]{1}[a-zA-Z]{1}\d{7}
Ok, I modified the method to incorporate both of our suggestions, to the following. Seems to be working. All I did is create two strings on line 2 and 3, and instead of replacing i1Expression inside the loop, I do it all the way at the end.

Can you take a look to see if there's any problems with this approach?

string i1Expression = Mask.Value.Replace("'", "");
string maskWithoutLiterals = new Regex(@"\'([^']*)\'").Replace(Mask.Value, "");
string orgMaskWithoutLiterals = maskWithoutLiterals;
Regex regEx = null;
Dictionary<string, string> i1RegExList = new Dictionary<string, string>() 
{ 
	{ @"\dN", @"\d{0}" },
	{ @"\dA", @"[a-zA-Z]{0}" },
	{ @"\dZ", @"[\d|a-z|A-Z]{0}" }
};
string i1RegEx, match, replacementRegEx;

i1RegExList.Keys.ToList().ForEach(exp =>
{
	var matches = new Regex(exp).Matches(maskWithoutLiterals);
	for (int index = 0; index < matches.Count; index++)
	{
		i1RegEx = matches[index].Value;
		match = new Regex(@"\d+").Match(i1RegEx).Value;
		replacementRegEx = String.Format(i1RegExList[exp], "{" + match + "}");
		maskWithoutLiterals = maskWithoutLiterals.Replace(i1RegEx, replacementRegEx);
	}
});

i1Expression = i1Expression.Replace(orgMaskWithoutLiterals, maskWithoutLiterals);

return new Regex("^" + i1Expression + "$").IsMatch(policyNumber);

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial