Skip to content

Instantly share code, notes, and snippets.

@aanari
Created May 15, 2012 12:30
Show Gist options
  • Save aanari/2701394 to your computer and use it in GitHub Desktop.
Save aanari/2701394 to your computer and use it in GitHub Desktop.
Clean Names in C#/LINQ
var inputNames = new [] { "Ann Boelyn", "King Henry VIII", "Charles of Macedonia", "Edgar J. Hoover", "Birkenstock", "Teenage Mutant Ninja Turtles", "Henry Ford Sr.", "Alexander the Great Jr.", "Sir Ixion Galahad", "W. Bryan Cranston, Jr.", "Neil Patrick-Harris", "Harold & Kumar", "Tipsy/Topsy/Turvy", "Eddard, House of Stark", "Jay Z"}.ToList();
var excludeList = new [] { ",", "/", "&", "@", "(", ")" }.ToList();
var matchNames = new List<string>();
TextInfo textInfo = Thread.CurrentThread.CurrentCulture.TextInfo;
foreach (var name in inputNames)
{
if (!excludeList.Any(name.Contains))
{
var cleanName = Regex.Match(name, @"(.+?)(\s(?:I|II|III|Jr|Sr)?(?:\.)?)?$").Groups[1].Value;
var match = Regex.Match(cleanName, @"^(\S+) (?:(?:\S.\ )|(?:\S+ ))?(\S+)");
if (match.Success)
{
matchNames.Add(name + "," + Regex.Replace(textInfo.ToTitleCase(match.Groups[1].Value.ToLower()), @"\d", "") + " " + Regex.Replace(textInfo.ToTitleCase(match.Groups[2].Value.ToLower()), @"\d", ""));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment