private void button1_Click_2(object sender, EventArgs e)
{
//string Pattern = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
//Regex re = new Regex(Pattern, RegexOptions.IgnoreCase);
//if (!re.IsMatch(textBox2.Text))
//{
// MessageBox.Show("不匹配");
// textBox2.Focus();
//}
string testStr = "";
testStr = "你是谁呀?";
if (Regex.IsMatch(testStr, @"[^\x00-\xff]", RegexOptions.IgnoreCase))
MessageBox.Show("字符串:“" + testStr + "”里有中文");
testStr = "我是谁,我是yongfa365";
if (Regex.IsMatch(testStr, @"\d{3}", RegexOptions.IgnoreCase))
MessageBox.Show("字符串:“" + testStr + "”里有三个连续的数字");
testStr = "我 12是23 43柳53 53永23 法234 24呀234!234";
MessageBox.Show(testStr + "\r\n\r\n替换空格及数字后为:\r\n\r\n" + Regex.Replace(testStr, @"\d| ", ""));
testStr = "我123是12345谁123456";
MessageBox.Show(testStr + "\r\n\r\n替换\"所有汉字后的、前2个数字为空\"后为:\r\n\r\n" + Regex.Replace(testStr, @"([^\x00-\xff])\d{1,2}", "$1"));
testStr = "你是1号,我是23号,他是45号,都几号来了?";
foreach (Match m in Regex.Matches(testStr, @"\d+号"))
MessageBox.Show(testStr + "\r\n\r\n里,都有几号来了?\r\n\r\n" + m.Value);
}
感觉C#的正则表达式用起来还是方便些,VBS正则必须得先建一个类的实例。而C#可以直接使用Regex,这个叫静态类,可以直接使用,具体怎么讲以后慢慢学。
总的来说,如果以前用的vbs或js的正则,学C#的正则还是很容易的,毕竟正则表达式没有变化的,不一样的只是怎么用他而已,且怎么用变化也不大。
要使用正则表达式得先:
using System.Text;
using System.Text.RegularExpressions;
引用本页地址:
http://www.yongfa365.com/item/CZhengZeBiaoDaShiChuCiChangShi.html