Friday, December 5, 2008

Names of People converting to Mixed Case and Roman Numbers Using Regex

Our web app takes data from an order entry system. In this data the person's full name is in all upper case letters in one field.

Ex. DOE III, JOHN D For John D Doe the Third

We have a method that converts the all upper case letters to a mixed case.
We found a bug in our logic in respects to the handling of roman numbers inside the name suffix.

Our logic would output the following:

Doe Iii, John D

To fix this it was proposed to use a series of string.replace() method to fix the name suffix after it was set to mixed case. Please see Figure 1

Figure 1:
...
strName = strFormattedName.ToString();


//Handle Roman numerals up to X

strName = " " + strName + " ";

strName = strName.Replace(" Ii ", " II ");

strName = strName.Replace(" Iii ", " III ");

strName = strName.Replace(" Iv ", " IV ");

strName = strName.Replace(" Vi ", " VI ");

strName = strName.Replace(" Vii ", " VII ");

strName = strName.Replace(" Viii ", " VIII ");

strName = strName.Replace(" Ix ", " IX ");

strName = strName.Trim();



return strName;

}

I felt that was wasteful. We were sending the token that contained the suffix to the mix case method, placed the whole name back together and then sending the whole name through the above method (Figure 1) to undo the mix case to the suffix. Also wasteful was the series of 7 string replacement calls.

I thought it would be better to use a regular expression to check to see if a token contained a roman number generation suffix. If a token did contain a roman number, then we could not send it to the mix case method. This code is more efficient and cleaner.

Figure 2 contains a method that checks a string for roman numbers 2 though 4. The downside to this is I have yet to figure out the difference between the name Vi and the roman number 6. If I think of something cool, I will update this post.



Figure 2:

private bool isSuffix(string inName)
{
bool ret = false;
try
{
string sPattern = @"^\W?(II|III|IV)\W?$";
if (Regex.IsMatch(inName, sPattern))
ret = true;
}
catch //todo: add error ex handle
{
}
return ret;
}

No comments: