1

我正在尝试将姓名、城市、州、邮编和电话与完整地址分开。我设法使用下面的代码分离 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.

这个问题差不多结束了。

4

2 回答 2

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})
于 2013-07-03T12:21:56.407 回答
0

提取名称的方法。

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

提取手机的方法。

@"(\d{3}\ \d{2}\ \d{6})", @"(\d{4}\ \d{3}\ \d{4})"
于 2013-07-16T08:36:51.450 回答