内容显示页
 
类别:.Net + C# | 浏览(30) | 2008-4-4 21:07:18

 

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
 
 
相关链接
 
网友评论:
姓名: 记住我
网址:
邮箱:
内容:
验证码:  验证码图片看不清? 换张图试试
 
   
 
 
文章分类
 
 
专题(3)
 
ASP+VBS(141)
 
 
 
 
Linux(10)
 
 
SQL(55)
 
web 2.0(22)
 
 
 
 
 
 
心程(48)
 
生活(71)
 
创业(5)
 
   

power by :柳永法(yongfa365)'Blog | model by :hibaidu | css by:众网友 | 京ICP备07011491号   我要统计  

本空间赞助商:北京中科兴联信息技术有限公司

QQ:64049027    E-mail:64049027<at>qq.com