正则表达式的用法

来源:互联网 发布:强力文件粉碎软件 编辑:程序博客网 时间:2024/05/04 18:53
<iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-1508116105633727&amp;dt=1180749143890&amp;lmt=1180707633&amp;format=728x90_as&amp;output=html&amp;correlator=1180749143880&amp;channel=2744893162&amp;url=http%3A%2F%2Fwww.julysoft.info%2Fself%2F3.htm&amp;ad_type=text_image&amp;flash=9&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true" frameborder="0" width="728" scrolling="no" height="90" allowtransparency="allowtransparency"></iframe>

两种使用方法:

1、在文本输入框后加入正则表达式验证控件RegularExpressionValidator。此种方法适用于WebForm中。
在ValidationExpression选项中,输入验证规则。
在ControlToValidate选项中,选择要验证的控件。
在ErrorMesage选项中,输入如果验证没有通过页面要显示的信息。

2、在隐藏代码中使用,此方法适用于WinForm和WebForm中。
这种方法要先加入声明命名空间:
using System.Text.RegularExpressions;
然后在方法中声明验证规则:
   string s_reg = @"^/d+$";
声明要验证的字符串:
   string s = textBox1.Text;
新建一个实例,并将验证规则以参数形式传入实例,初始化类:
Regex reg = new Regex( s_reg );
然后拿实例对要验证的字符串进行验证,返回结果为bool值:
    if ( reg.IsMatch (s) )
    {
     MessageBox.Show( "It 's OK~");
    }
    else
    {
     MessageBox.Show( "It 's Bad~");
    } 
原创粉丝点击