using System;
using System.Text.RegularExpressions;
public class test
{
static void Main()
{
string p = @"^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])$";
string [] s = {"-", "abcd", "a-c", "-a"};
foreach(string c in s)
{
Console.WriteLine("{0} {1}", c, Regex.IsMatch(c, p));
}
}
}
string str = this.TextBox2.Text.Trim();
System.Text.RegularExpressions.Regex strRegex = new Regex(@"[0-9]",RegexOptions.IgnoreCase);
if(strRegex.Matches(str).Count == str.Length)
{
Response.Write("<br>输入的全是数");
Response.Write(" "+str);
}
else
{
string pat = @"([0-9]+)";
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
Match m = r.Match(str);
if (m.Success)
{
Group g = m.Groups[1];
CaptureCollection cc = g.Captures;
Capture c = cc[0];
if (c.Index == 0)
{
Response.Write("<br>输入的不全是数,前面的数字:");
Response.Write(" "+c);
}
}
}