Trim Guest Title if there is Title is present in the name using Regex

2017-01-15

Requirement is ​remove the title from the guest name if there is title present in the name. Set of possible titles is already known & is stored in an array each one seperated by a seperator |
Title List: Mr|Mrs|Mr.|Dr|Dr.
Conditions to remove title from the guest name:
1. If the title is found in the above Title list
2. Match of title found(case insensitive)
Example: Mr Rama Murthy
after trimming: Rama Murthy
Example: Mr.RemaSankar
after trimming: RemaSankar
Code:
static void GuestNameUtility(string pGuestName, List pTitles)
        {
            //Split(null); equals white space; Refer:http://stackoverflow.com/questions/6111298/best-way-to-specify-whitespace-in-a-string-split-operation
            string[] result = pGuestName.Split(null);
            string pattern = string.Empty;
            StringBuilder sbPattern = new StringBuilder(string.Empty);
            int dotIndex = 0;
            string titleWhiteSpaceTrimmed = string.Empty;
            string[] splitArray = null;
            string afterRemovingTitle = string.Empty;

            for (int i = 0; i < pTitles.Count; i++)
            {
                if (sbPattern.Length > 0)
                    sbPattern.Append("|");
                if (pTitles[i].Contains('.'))
                {
                    //We have to trim any whitespace in the Title as we are going to compare Title with the title 
     //found in the guestname where there also we are trimming the whitespace
                    //If this trimming is not used in both sides (i.e. while reaing the Titles given in IT settings & 
     //in reading guestName section by removing whitespace in the guestName section.)
                    //we will face issues while matching with whitespaces
                    titleWhiteSpaceTrimmed = pTitles[i].Trim();
                    dotIndex = titleWhiteSpaceTrimmed.IndexOf('.');
                    //Need to escape period(i.e. '.') as it should also be considered for a match. Escape the period otherwise 
     //it will be treated as period(.) has special meaning in Regex
                    sbPattern.Append(string.Format("{0}", titleWhiteSpaceTrimmed.Substring(0, dotIndex) + Regex.Escape(".")));
                }
                else
                {
                    sbPattern.Append(string.Format("{0}", pTitles[i].Trim()));
                }
            }
            //Need to match only the exact Titles with start & ending should match by using ^(start) & $(end)
            //So for example ^(Mr|Mrs.|Mrs |Master)$ is given Title patterns & a guestname with Mrs Srimati Gowri 
   //comes Mrs should be matched exactly with 'Mrs' & not with 'Mr'
            pattern = string.Format("^({0})$", sbPattern.ToString());
            if (result.Length == 0)
            {
                m_guestLastName = pGuestName;
            }
            else
            {
                m_guestLastName = result[result.Length - 1];
                StringBuilder sbFirstName = new StringBuilder(string.Empty);
                for (int i = 0; i <= result.Length - 2; i++)
                {
                    //If the iterating guest name part has period(.), we need to split by period(.) & compare for a match 
                    //This logic is required as by this only we can find a match if the title is coming along with guestname 
     //wihtout any space. Example is:Mr.Asif Abdulla
                    //If this logic is not used then we will not be able to identify the title match for 'Mr.' 
     //in the guest name:Mr.Asif Abdulla


                    if (result[i].Contains('.'))
                    {
                        splitArray = result[i].Split('.');
                        splitArray[0] = string.Format("{0}.", splitArray[0]);
                    }
                    else
                    {
                        splitArray = new string[1];
                        splitArray[0] = result[i];
                    }
                    foreach (string splitArrayItem in splitArray)
                    {
                        Match matchResult = Regex.Match(splitArrayItem, pattern, RegexOptions.IgnoreCase);
                        if (matchResult.Success)
                        {
                            afterRemovingTitle = Regex.Replace(splitArrayItem, pattern, string.Empty, RegexOptions.IgnoreCase);
                            if (!string.IsNullOrEmpty(afterRemovingTitle))
                            {
                                if (sbFirstName.Length > 0)
                                    sbFirstName.Append(" ");
                                sbFirstName.Append(afterRemovingTitle);
                            }
                        }
                        else
                        {
                            if (sbFirstName.Length > 0)
                                sbFirstName.Append(" ");
                            sbFirstName.Append(splitArrayItem);
                        }
                    }
                }
                m_guestFirstName = sbFirstName.ToString();
                if (string.IsNullOrEmpty(m_guestFirstName))
                    m_guestFirstName = m_guestLastName;
            }
        }​

0 comments: