【问题标题】:How to extract name and phone number from address如何从地址中提取姓名和电话号码
【发布时间】:2013-06-30 16:34:36
【问题描述】:

我正在尝试将姓名、城市、州、邮编和电话与完整地址分开。我设法使用下面的代码分离 zip。

 String input = richTextBox1.Text;

        Regex regex = new Regex(@"(\d{6})",
            RegexOptions.Compiled |
            RegexOptions.CultureInvariant);

        Match match = regex.Match(input);

        if (match.Success)
            textBox2.Text = match.Groups[1].Value;

没有。邮政编码中的位数不会改变,所以对我来说相当简单。但是对于电话号码,没有。位数会发生变化,有时会出现空格,如下例所示。我对如何提取这种类型的电话号码感到非常困惑。

我尝试了下面的电话号码代码和“.star\s.star”来提取姓名,但它不起作用。

        Regex regex1 = new Regex(@"\b\d{3}\s\d{2}\s\d{6}",
            RegexOptions.Compiled | RegexOptions.CultureInvariant);

        Match match1 = regex1.Match(input);

        if (match1.Success)
            textBox3.Text = match1.Groups[1].Value;

        else
        {
            Regex regex2 = new Regex(@"\b\d{4}\s\d{3}\s\d{4}",
            RegexOptions.Compiled | RegexOptions.CultureInvariant);

            Match match2 = regex2.Match(input);

            if (match2.Success)
                textBox3.Text = match2.Groups[1].Value;
        }

样品地址:

H.
Mithras Developers
Tiruchendur Road, VM Chathiram PO, Radhapur, Tirunelveli, TN 628809 ‎
097 43 838122 ‎

编辑---

我终于找到电话的答案了。

 @"(\d{3}\ \d{2}\ \d{6})", @"(\d{4}\ \d{3}\ \d{4})"

请帮我提取城市和州。我目前正在提取名称。如果我自己找到答案,我会更新这篇文章。

编辑---

我找到了提取名称的方法。

@"^(.*)", selecting the first line of the address.

问题差不多结束了。

【问题讨论】:

  • 电话号码总是在地址的最后一行吗?
  • 是的,它也是 xxx xx xxxxxx 或 xxxx xxx xxxx 的形式,还有一种方法可以提取下一行,然后是 A-Z\。 (提取名称)。感谢您的帮助。

标签: c# regex


【解决方案1】:

试试这个...

    (\w+\s*\w+)\s+([\w\s]+),\s+([\w\s]+),\s+([\w\s]+),\s+([\w\s]+),\s*(\w{2}\s*\d+)\s*([\d\s]{9,13})

【讨论】:

    【解决方案2】:

    名称的提取方法。

    @"^(.*)", selecting the first line of the address.
    

    提取手机的方法。

    @"(\d{3}\ \d{2}\ \d{6})", @"(\d{4}\ \d{3}\ \d{4})"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-12
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多