用多个字符串数组拆分字符串

时间:2018-08-07 20:11:54

标签: c# regex split

我有一个记录列表,这些记录的地址未与邮件列表分开。我想按街道地址和城市名称分割记录。

对于第一个问题,我该如何按街道类型(例如“ St”,“ Drive”,“ Dr”,“ Trail”等

在此示例中,String.Split“吃掉了” Ct“。

 string source1 = "Cxxx, Kxxx,9999 Valleycrest Ct Allen TX 75002 ,,,,,,,,,";
      // string source2= "Cxxx, Mxxx Exxx,9999 Chesterwood Dr Little Elm, TX 75068 ,,,,,,,,,";

        string[] stringSeparators = new string[] { "Drive", "St", "Dr", "Trail","Ct" };
        string[] result;

        // ...
        result = source1.Split(stringSeparators, StringSplitOptions.None);

        foreach (string s in result)
        {
            Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
        }

        //Objective
       // "Cxxx, Kxxx,9999 Valleycrest Ct, Allen, TX, 75002 ,,,,,,,,,"

这里是列表的示例。

  

“ Pxxx,Sxxx”,“ 9999 Southgate McKinney博士,德克萨斯州75070” ,,,,,,,,
   “ Hxxxx,Mxxxx”,“ 9999 Glendale Ct Allen,TX 75013” ,,,,,,,,
   “ Axxxx,Nxxxxx”,“ 99999 Balez Drive Frisco,TX 75035” ,,,,,,,
   “ Sxxx,Dxxxx”,“ 999 Pine Trail Allen,TX 75002” ,,,,,,,
   “ Vxxx,Sxxxx”,“ 9999 Richmond Ave Dallas,TX 75206” ,,,,,,,

我的列表中不包含“圣路易斯”,因此这不是问题。

为简化我的问题。

如果我有以下字符串:

“ Cxxx,Kxxx,9999 Valleycrest Ct Allen TX 75002”

我想分割以下字符串“ Ct,Dr,Ave”

我想要以下结果[]

result [0] =“ Cxxx,Kxxx,9999 Valleycrest Ct” result [1] =“ Allen TX 75002”

因为分隔符字符串不包含在返回数组的元素中,所以我希望不要删除它们。我还有其他选择吗?

换句话说,不要删除“ Ct”,“ Dr”或我发现/使用的任何分隔符。

谢谢

2 个答案:

答案 0 :(得分:2)

您可以尝试使用此RegEx:

    @PostMapping(path="/sendData")
public @ResponseBody String sendData(HttpServletResponse response, 
        @RequestParam(required=true, name="start") String start, 
        @RequestParam(required=true, name="end") String end,
        @RequestParam(required=true, name="locale") Locale locale,
        @RequestParam(required=false, name="objectIds") Integer[] objectIds) throws DocumentException, IOException {

    //some more code
}

现在,您可以访问Capture.Groups [“ first”]和Capture.Groups [“ second]; 但是,这可以像您的示例中那样不使用引号。

顺便说一句:您可以在这里试用:RegExBuilder

编辑:

@"(?<first>.+(?:Drive|St|Dr|Trail|Ct))(?<second>[^""]*)"

将创建一个命名组。

(?<first> 将一次或多次匹配任何字符。

.+

将创建一个非捕获组,该组与(?: 中表示“或”的任何单词匹配 名为“第二”的组将匹配任何不是引号的字符(零个或多个字符)。

答案 1 :(得分:2)

正如评论员所指出的那样,如果您有一个大型的地址数据库,则可能会遇到一些无法正确解析的地址,因此您必须进行一些调整。因此,我会将风险包含在专门用于解析地址的单独类中。为了进行解析,您只需要循环使用IndexOf的老式方法:

public class Address
{
    static private readonly string[] separators = new string[] { "Drive", "St", "Dr", "Trail","Ct" };

    protected readonly string _text;

    public Address(string text)
    {
        _text = text;
        foreach (var s in separators)
        {
            var i = text.IndexOf(s);
            if (i == -1) continue;
            var splitPoint = i + s.Length;
            StreetPart = text.Substring(0,splitPoint);
            CityPart = text.Substring(splitPoint+1);
            return;
        }
        StreetPart = text;
        CityPart = null;
    }

    public string StreetPart { get; private set; }
    public string CityPart { get; private set; }

    public override string ToString()
    {
        return _text;
    }
}

然后您可以这样称呼它:

public class Program
{
    public static string[] tests = new string []
    {
        @"9999 Southgate Dr McKinney,TX 75070",
        @"Glendale Ct Allen, TX 75013",
        @"99999 Balez Drive Frisco, TX 75035",
        @"999 Pine Trail Allen, TX 75002",
        @"999 Richmond Ave Dallas, TX 75206"
    };

    public static void Main()
    {
        foreach (var t in tests)
        {
            var a = new Address(t);
            Console.WriteLine("Address: '{0}'  StreetPart: '{1}' CityPart: '{2}'", a, a.StreetPart, a.CityPart);
        }
    }
}

输出:

Address: '9999 Southgate Dr McKinney,TX 75070'  StreetPart: '9999 Southgate Dr' CityPart: 'McKinney,TX 75070'
Address: 'Glendale Ct Allen, TX 75013'  StreetPart: 'Glendale Ct' CityPart: 'Allen, TX 75013'
Address: '99999 Balez Drive Frisco, TX 75035'  StreetPart: '99999 Balez Drive' CityPart: 'Frisco, TX 75035'
Address: '999 Pine Trail Allen, TX 75002'  StreetPart: '999 Pine Trail' CityPart: 'Allen, TX 75002'
Address: '999 Richmond Ave Dallas, TX 75206'  StreetPart: '999 Richmond Ave Dallas, TX 75206' CityPart: ''

Example on DotNetFiddle