【问题标题】:Find phone number from given string c#从给定的字符串c#中查找电话号码
【发布时间】:2015-10-16 11:43:43
【问题描述】:

我有一份简历,我想查找用户的联系电话(手机号码或电话号码) 从简历中,需要任何想法或解决方案或任何帮助来实现目标。

到目前为止我已经尝试过什么......

var numString = "";
        string strData = ",38,,,,,,,,,,,,,,,,,,,,,,,,,,,,382350,,,,,0,,,,8141884584,,,,,,,,";
        char[] separator = new char[] { ',' };
        string[] strSplitArr = strData.Split(separator);
        for (int q = 0; q < strSplitArr.Length; q++)
        {
            if (strSplitArr[q] != "")
            {
                int no = 0;

                no = strSplitArr[q].Length;
                if (no >= 10 && no <= 12)
                {
                    numString += strSplitArr[q].ToString() + ", ";
                }
            }
        }

【问题讨论】:

  • @Izzy 请查看更新后的帖子
  • 只需使用像,[\d]{10},这样的正则表达式模式
  • 您的示例数据看起来不像简历。您是否有理由向我们展示拆分 CSV 字符串的代码?仅供参考,代码可以简化为var numString = string.Join(", ", strData.Split(',').Where(s =&gt; s.Length &gt;= 10 &amp;&amp; s.Length &lt;=12));
  • 任何带有我们给定模式@MacroMan 的示例代码
  • @juharr 我认为我的方法很糟糕

标签: c# asp.net regex string


【解决方案1】:

我建议你使用Regular Expression

以下是查找美国电话号码的示例代码:

string text = MyInputMethod();
const string MatchPhonePattern =
       @"\(?\d{3}\)?-? *\d{3}-? *-?\d{4}";

        Regex rx = new Regex(MatchPhonePattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);

        // Find matches.
        MatchCollection matches = rx.Matches(text);

        // Report the number of matches found.
        int noOfMatches = matches.Count;


        //Do something with the matches

        foreach (Match match in matches)
        {
            //Do something with the matches
           string tempPhoneNumber= match.Value.ToString(); ;

         }

【讨论】:

  • 每个国家/地区有不同的模式吗?
  • 这取决于电话号码的格式。但我也将这个用于其他国家:@"\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多