/// <summary> /// Parses person name string and populates <b>Person</b> instance with parsed values. /// </summary> /// <param name="personName">Person name string that may contain first, last and middle names, name title and suffix.</param> /// <param name="person"><b>Person</b> instance to be populated with parsing results.</param> /// <param name="useEmptyStringsByDefault">If <b>true</b>, name properties that are not found in person name string will be populated with empty strings.</param> public static void Parse(string personName, Person person, bool useEmptyStringsByDefault = true) { if (personName == null) throw new ArgumentNullException("personName"); if (person == null) throw new ArgumentNullException("person"); WipeName(person, useEmptyStringsByDefault ? "" : null); ParseInternal(personName, person); }
private static void ParseInternal(string personName, Person person) { // Check if person name is represented as "Last name, first name". personName = TryReverse(personName); // Normalize person name string: remove all punctuation that is not a part of any name. personName = _trimRegex.Replace(personName, " ").Trim(); if (personName == "") return; var words = new List<string>(personName.Split()); // Check if first word is a title. if (_titleRegex.IsMatch(words[0])) { person.Title = NormalizeTitleInternal(words[0]); words.RemoveAt(0); if (words.Count == 0) return; } // Check if last word is a suffix. if (_suffixRegex.IsMatch(words[words.Count - 1])) { person.Suffix = NormalizeSuffixInternal(words[words.Count - 1]); words.RemoveAt(words.Count - 1); if (words.Count == 0) return; } if (words.Count == 1) { if (string.IsNullOrEmpty(person.Title) && string.IsNullOrEmpty(person.Suffix)) { // Single word which is not a title or a suffix: must be a first name. person.FirstName = words[0]; } else { // Single word with a title or a suffix: must be a last name. person.LastName = words[0]; } return; } // Last word is a last name. person.LastName = words[words.Count - 1]; words.RemoveAt(words.Count - 1); // Look for last name prefixes. var prefixFound = false; if (words.Count > 1) { // Check if previous words are two-part last name prefix. foreach (var prefix in _lastNameTwoPartPrefixes) { var parts = prefix.Split(); if (parts[0].Equals(words[words.Count - 2], StringComparison.OrdinalIgnoreCase) && parts[1].Equals(words[words.Count - 1], StringComparison.OrdinalIgnoreCase)) { person.LastName = words[words.Count - 2] + " " + words[words.Count - 1] + " " + person.LastName; words.RemoveRange(words.Count - 2, 2); prefixFound = true; break; } } } if (!prefixFound) { // Check if previous word is a one-part last name prefix. foreach (var prefix in _lastNameOnePartPrefixes) { if (prefix.Equals(words[words.Count - 1], StringComparison.OrdinalIgnoreCase)) { person.LastName = words[words.Count - 1] + " " + person.LastName; words.RemoveAt(words.Count - 1); break; } } } if (words.Count == 0) return; if (words.Count > 1) { // First word is a first name. person.FirstName = words[0]; words.RemoveAt(0); // Next word is a middle name. person.MiddleName = words[0]; words.RemoveAt(0); // If any words remain, add them to middle name. while (words.Count > 0) { person.MiddleName += " " + words[0]; words.RemoveAt(0); } } else { // Single remaining word is a first name. person.FirstName = words[0]; } }
private static void WipeName(Person person, string wipeString) { person.Title = wipeString; person.FirstName = wipeString; person.MiddleName = wipeString; person.LastName = wipeString; person.Suffix = wipeString; }
/// <summary> /// Parses person name string and returns new <b>Person</b> instance populated with parsed values. /// </summary> /// <param name="personName">Person name string that may contain first, last and middle names, name title and suffix.</param> /// <param name="useEmptyStringsByDefault">If <b>true</b>, name properties that are not found in person name string will be populated with empty strings.</param> /// <returns><b>Person</b> instance populated with parsing results.</returns> public static Person Parse(string personName, bool useEmptyStringsByDefault = true) { if (personName == null) throw new ArgumentNullException("personName"); var person = new Person(); if (useEmptyStringsByDefault) WipeName(person, ""); ParseInternal(personName, person); return person; }