1

这花了我一段时间才弄清楚,所以我将在问题中发布我的结果,因为这是已回答的。

问题:如何在名称字段中使用可能的分隔符数组拆分字符串,同时将分隔符保留在拆分数组中并排除拆分可能在数组中创建的空白。

示例:山姆华盛顿和珍娜

我的问题是我创建的名称解析器正在编写

名字:山姆

姓氏:珍娜

4

1 回答 1

1

使用以下代码,我能够像这样解析它

名字:山姆

姓氏:华盛顿

名字 2 珍娜

但是要小心,因为如果您要使用我的加入者列表,则不包括可以在常见名称中找到的字符串值,例如“And”和“OR”

这将解析你的名字 EX: "Andy" 将是 "And" , "Y"

EX2:“Gregory 将是“Greg”“或”“y”

希望这可以帮助某人。如果您有任何问题,请随时给我留言。

    /// <summary>
    /// remove bad name parts
    /// </summary>
    /// <param name="parts">name parsed for review</param>
    public static void CheckBadNames(ref string[] parts)
    {
        string[] BadName = new string[] {"LIFE", "ESTATE" ,"(",")","*","AN","LIFETIME","INTREST","MARRIED",
                                         "UNMARRIED","MARRIED/UNMARRIED","SINGLE","W/","/W","THE","ET",
                                         "ALS","AS", "TENANT","WIFE", "HUSBAND", "NOT", "DRIVE" ,"INSURED",                 
                                         "EXCLUDED","DISABLED" ,"LICENSED","TRUSTEE","ATSOT","A T S O T",
                                         "AKA", "-ATSOT","OF","DBA","EVOCABLE","FAMILY","INTEREST","MASTER"};
        string[] joiners = new string[9] { "&", @"AND\", @"OR\", "\\", "&/OR", "AND/OR", "&-OR", "/", "OF/AND" };

    Restart:
        List<string> list = new List<string>(parts); //convert array to list
        foreach (string part in list)
        {
            if (BadName.Any(s => part.ToUpper().Equals(s)) || part == "-")
            {
                list.Remove(part);
                parts = list.ToArray();
                goto Restart;
            }
            //check to see if any part ends with joiner
            if (joiners.Any(s => part.ToUpper().EndsWith(s)))
            {
                //check if by ends with means that it is just a joiner
                if (joiners.Any(s => part.ToUpper().Equals(s)))
                {
                    continue;
                }
                else //name part ends with a joiner EX. Washington&
                {
                    foreach (string div in joiners.Where(s => part.ToUpper().Contains(s)))  // each string that contains a joiner
                    {
                        var temp = Regex.Split(part, "(" + div + ")").Where(x => x != String.Empty); // split into parts ignore leading or trailing spaces
                        int pos = list.IndexOf(part);
                        list.Remove(part);

                        for (int i = 0; i < temp.Count(); i++)
                        {
                            list.Insert(pos + i, temp.ElementAt(i));
                        }
                        parts = list.ToArray();
                        goto Restart;
                    }
                }
            }
        }

        if (parts.Count() == 0)
        {
            return;
        }
        if (joiners.Any(s => list.Last().ToUpper().Equals(s))) //remove last part if is a joiner
        {
            list.Remove(list.Last());
        }

        parts = list.ToArray(); // convert list back to array
    }
于 2012-10-22T22:45:33.923 回答