如何获取字符串中匹配电话或者手机的号码

top_99 2010-07-08 10:44:39
比如说 客户发了一条短信过来问。 请问13327270098是哪里的号码
我现在可以得到这条短信,但我想获取其中匹配手机或者固话的这段号码。然后
返回给他 13327270098暂无登记.

很抱歉,您查的“用户问题中的号码”暂无登记。号码查单位,如: 021-63632626,63632626.
这种.

我尝试过用这种异常的方法,但效率太低. 不行. 能有高手帮忙解决下吗?

private string SubPhone(string contents)
{
int first = 0;
int end = 0;
for (int i = 0; i < contents.Length - 1; i++)
{
string aa = contents.Substring(i, i + 1);
try
{
int test = Convert.ToInt32(aa);
}
catch (Exception)
{
continue;
}
first = i;
break;
}

for (int j = contents.Length - 1; j > 0; j--)
{
string aa = contents.Substring(j - 1, j);
try
{
int test = Convert.ToInt32(aa);
}
catch (Exception)
{
continue;
}
end = j;
break;
}
string result = contents.Substring(first, end);
return result;
}
...全文
95 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
malun666 2010-07-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhanlixin 的回复:]

using System.Text.RegularExpressions;
....
string strReg = @"(\d{3}-\d{8}|\d{4}-\d{7})|(((\(\d{3}\))|(\d{3}\-))?13\d{9}|15[89]\d{8})";
string strData = "我的电话010-12345678 ,手机号13812345678";
MatchCo……
[/Quote]

顶,正则表达式的妙用
Zhanlixin 2010-07-08
  • 打赏
  • 举报
回复
using System.Text.RegularExpressions;
....
string strReg = @"(\d{3}-\d{8}|\d{4}-\d{7})|(((\(\d{3}\))|(\d{3}\-))?13\d{9}|15[89]\d{8})";
string strData = "我的电话010-12345678 ,手机号13812345678";
MatchCollection Matches = Regex.Matches(strData,strReg);
foreach (Match NextMatch in Matches)
{
string str = NextMatch.Value;//这是你要的电话号或手机号
}
top_99 2010-07-08
  • 打赏
  • 举报
回复
是的。
andyxl 2010-07-08
  • 打赏
  • 举报
回复
是想从一段文字里面获取 手机号码 吗?
top_99 2010-07-08
  • 打赏
  • 举报
回复
自己先顶顶。。。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧