Created
May 15, 2012 12:30
-
-
Save aanari/2701394 to your computer and use it in GitHub Desktop.
Clean Names in C#/LINQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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