string phone ="";
string pattern @"|d{10}";
bool rusurt = false;
Console.WriteLine("请输入手机号");
do{
phone = Console.ReadLine();
rusurt = Rogex.IsMatch(phone,pattern);
phone 是手机号,pattern 正则表达式
if( !result )
Console.WriteLine("BadNum,try again");
}
while (!result)
Console.WriteLine("Good");
正则实例研究:
static void Main(string[] args) { Regex reg1 = new Regex(@"d+"); string str = reg1.Replace("123","insert into table where id = $&"); Console.WriteLine(str); //输出 insert into table where id = 123 Regex reg2 = new Regex(@"1+1=(d)"); string str2 = reg2.Replace("1+1=3","不是$1"); Console.WriteLine(str2); //输出 不是3 Regex reg3 = new Regex(@"1+1=(?<result>d)"); string str3 = reg3.Replace("1+1=3", "不是${result}"); Console.WriteLine(str3); //输出 不是3 Regex reg4 = new Regex(@"d+"); string str4 = reg4.Replace("123ABC", "后面是$'"); //匹配文本之后的文本 Console.WriteLine(str4); //输出 后面是ABCABC 为什么会输出 后面是ABCABC呢?因为$'指的是ABC,然后替换掉原字符串中的123。不懂看多几次这句话 Regex reg5 = new Regex(@"d+"); string str5 = reg5.Replace("ABC123", "前面是$`"); //ABC前面是ABC 符号是 1左边那个 Console.WriteLine(str5); Regex reg6 = new Regex(@"d+"); string str6 = reg6.Replace("ABC123","右边原始输入字符串$_"); Console.WriteLine(str6); //输出 右边是原始字符串ABC123 Console.ReadKey(); }