用于解析邮政地址的正则表达式?

时间:2013-01-12 20:47:27

标签: c#-4.0

您好我是编程新手。我有以下格式的地址,我想分开门牌号码和街道号码部分。

house no.966 s# 70  Kashmir road Peshawar road Rawalpindi
house no.970 st. # 43  Darian Kalar saeedan
H# 110 Street No.35  Dhowk kala Rwp
h# 124 Street No.73  G-11/3 ISB
h no.423 St. No.23  Chan chirag Rawat
H No.437 st. # 61  Chaklal scheme 3 RAWALPINDI

对此有任何帮助建议吗?

1 个答案:

答案 0 :(得分:0)

看起来你刚好按照它们出现的顺序排列了两组数字,所以你可以这样做:

void Main()
{
    var addresses = new string[] { 
        "house no.966 s# 70  Kashmir road Peshawar road Rawalpindi",
        "house no.970 st. # 43  Darian Kalar saeedan",
        "H# 110 Street No.35  Dhowk kala Rwp",
        "h# 124 Street No.73  G-11/3 ISB",
        "h no.423 St. No.23  Chan chirag Rawat",
        "H No.437 st. # 61  Chaklal scheme 3 RAWALPINDI"
    };

    Regex houseAndStreet = new Regex(@"(\d+).+?(\d+)");

    foreach (string address in addresses)
    {
        Match match = houseAndStreet.Match(address);
        string house = match.Groups[1].Value;
        string street = match.Groups[2].Value;
        Console.WriteLine ("House = {0}, Street = {1}", house, street);
    }
}

您希望添加一些错误检查以使其更加健壮,但这是基本想法。

正则表达式如下所示:

  • (\d+)组中的一个或多个数字
  • .+?非贪婪搜索任何其他角色
  • (\d+)另一个组中的一个或多个数字