private static string BuildDelimitedTagList(List <string> Tags, StringBuilder OutputStringBuilder, List <string> ReturnStrings = null, bool OnlyReturnMatterTags = false, string TagStartToken = DefaultTagStartToken)
        {
            string matterID = string.Empty;

            foreach (var tag in Tags)
            {
                OutputStringBuilder.Append(tag);
                OutputStringBuilder.Append(" ");

                if (ReturnStrings != null)
                {
                    // If filtering for valid matters, ensure pattern is satisfied
                    if (OnlyReturnMatterTags)
                    {
                        // If a valid matter add it to the list
                        if (MatterIdentificationBL.ValidateMatterID(TagStartToken, tag))
                        {
                            ReturnStrings.Add(tag);
                            //If the first founder matter tag assign it as the relevant matter ID
                            if (matterID.Equals(string.Empty))
                            {
                                matterID = tag;
                            }
                        }
                    }
                    else
                    {
                        ReturnStrings.Add(tag);
                    }
                }
            }
            return(matterID);
        }
        /// <summary>Process tags in a search, looking for ordinal tags.</summary>
        /// <param name="Search">The plain text (html elements stripped) version of the email to be parsed and other configuration paramaters.</param>
        /// <param name="ReturnOnlyMattersTags">Should the results be filtered to only include matter tags.</param>
        public static List <string> ProcessOrdinalTags(TagsSearchByTagStartTokenOrdinal Search, bool ReturnOnlyMattersTags = false)
        {
            List <string> returnStrings = null;

            if (Search != null && Search.TagStartToken != null && Search.TagStartToken.Length > 0 && Search.EmailBodyText != null && Search.EmailBodyText.Length > 0)
            {
                //Find all of the tags in the email
                var tags = EmailParser.FindTags(Search.EmailBodyText, Search.TagStartToken, Search.RemoveDuplicates, Search.ExcludePriorEmailsFromSearch);

                int           tagSetSize  = TagProcessingBusinessLogic.GetOrdinalTagSetSize(Search);
                List <string> ordinalTags = null;

                //Search for ordinal tags if there is a declared minimum set size greater than 0. Note that a search below size of 2 is pointless so we may want to revisit ths
                if (tagSetSize > 0)
                {
                    //Find ordinal (sequenced) tags in the email
                    ordinalTags = EmailParser.FindTagsOnContiguousNewLines(Search.EmailBodyText, Search.TagStartToken, Search.RemoveDuplicates, Search.ExcludePriorEmailsFromSearch);
                }

                foreach (var item in tags)
                {
                    if (MatterIdentificationBL.ValidateMatterID(Search.TagStartToken, item))
                    {
                        Search.MatterId = item.Substring(Search.TagStartToken.Length);
                        break;
                    }
                }

                //Process all of the tags into the persistence object and send to storage, event if we do not find tags in the content
                var strings = TagProcessingBusinessLogic.ProcessOrdinalTagsFoundInEmailBodyText(Search, returnStrings, tags, ordinalTags, tagSetSize, true, true);

                //If we are only supposed to return matter related tags filter accordingly
                if (ReturnOnlyMattersTags)
                {
                    returnStrings = new List <string>();
                    //Iterate over the found tags searching for valid matter numbers
                    foreach (var item in strings)
                    {
                        if (MatterIdentificationBL.ValidateMatterID(Search.TagStartToken, item))
                        {
                            returnStrings.Add(item);
                        }
                    }
                }
                //otherwise return all found tags
                else
                {
                    returnStrings = strings;
                }
            }
            return(returnStrings);
        }