在asp.net 2.0中可以用split方法

Code
string[] results = input.Split(new char[] { '-', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
在asp.net 1.1 中,因没有StringSplitOptions.RemoveEmptyEntries参数,想到的只有正则表达式了

Code
// string phone = "-0271-36584758-4534";
public static string[] GetMatches(string input)
{
Regex r = new Regex(@"\b(?<=[\s()-]|^)(?<number>\d+)(?=[\s()-]|$)\b");
MatchCollection mc = r.Matches(input);
int count = mc.Count;
string[] matches = new string[count];
for (int i = 0; i< count; i++)
{
matches[i] = mc[i].Groups["number"].Value;
}
return matches;
}
对于电话获取前2组就够了

Code
/// <summary>
/// 将区号和电话号码分组
/// </summary>
/// <param name="txtPhoneAreaCode">区号</param>
/// <param name="txtPhoneNumber">电话号码</param>
/// <param name="input">输入的电话号码</param>
public static void SetPhoneValue(TextBox txtPhoneAreaCode, TextBox txtPhoneNumber, string input)
{
string[] arrs = GetMatches(input);
if (arrs.Length == 0)
{
txtPhoneAreaCode.Text = string.Empty;
txtPhoneNumber.Text = string.Empty;
}
else if (arrs.Length == 1)
{
txtPhoneAreaCode.Text = string.Empty;
txtPhoneNumber.Text = arrs[0];
}
else
{
txtPhoneAreaCode.Text = arrs[0];
txtPhoneNumber.Text = arrs[1];
}
}