using System.Text.RegularExpressions.Regex类 应用正则表达式
2008-12-07 20:19
423 查看
来自http://hi.csdn.net/it_123
using System.Text.RegularExpressions.Regex类应用正则表达式。
1.打开VS2005新建网站,在新建的默认页面添加一个文本控件和一个按钮。
2.在代码页中加入自定义方法和双击按钮事件添加代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;//这里是自己添加的
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//自定义的方法test(),用来验证邮件地址格式。
public void test(string input)
{
string p = @"[a-zA-Z]+@[a-zA-Z]+/.com$";//这里应用正则表达式
Regex r = new Regex(p);//这里用到using System.Text.RegularExpressions.Regex类
;
Match m = r.Match(input);//Match()为Regex类的方法,它在输入字符串中搜索正则表
达式的匹配项,并将匹配成功的结果作为单个Match对象返回。
if (m.Success)//循环输出所有的匹配字符串。
{
Page.Response.Write("你输入的地址是"+m.Value);
}
else
{
Page.Response.Write("请输入合法的地址如:jsina@sina.com");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = TextBox1.Text;
test(str);
}
}
3.F5运行即可。//http://hi.csdn.net/it_123
using System.Text.RegularExpressions.Regex类应用正则表达式。
1.打开VS2005新建网站,在新建的默认页面添加一个文本控件和一个按钮。
2.在代码页中加入自定义方法和双击按钮事件添加代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;//这里是自己添加的
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//自定义的方法test(),用来验证邮件地址格式。
public void test(string input)
{
string p = @"[a-zA-Z]+@[a-zA-Z]+/.com$";//这里应用正则表达式
Regex r = new Regex(p);//这里用到using System.Text.RegularExpressions.Regex类
;
Match m = r.Match(input);//Match()为Regex类的方法,它在输入字符串中搜索正则表
达式的匹配项,并将匹配成功的结果作为单个Match对象返回。
if (m.Success)//循环输出所有的匹配字符串。
{
Page.Response.Write("你输入的地址是"+m.Value);
}
else
{
Page.Response.Write("请输入合法的地址如:jsina@sina.com");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = TextBox1.Text;
test(str);
}
}
3.F5运行即可。//http://hi.csdn.net/it_123
相关文章推荐
- 正则表达式 C#System.Text.RegularExpressions.Regex
- .net正则表达式大全(.net 的 System.Text.RegularExpressions.Regex.Match()方法使用)
- .net正则表达式大全(.net 的 System.Text.RegularExpressions.Regex.Match()方法使用)
- C#System.Text.RegularExpressions.Regex使用(一) .
- C#System.Text.RegularExpressions.Regex使用(二)
- C#System.Text.RegularExpressions.Regex使用(一) .
- 正则表达式 C#System.Text.RegularExpressions.Regex
- C#System.Text.RegularExpressions.Regex使用(一) .
- C#System.Text.RegularExpressions.Regex使用(二) .
- System.Text.RegularExpressions.Regex
- C#System.Text.RegularExpressions.Regex使用(二) .
- C#System.Text.RegularExpressions.Regex使用(二) .
- System.Text.RegularExpressions.Regex 使用实例
- C#System.Text.RegularExpressions.Regex使用(二)
- C#System.Text.RegularExpressions.Regex使用(一)
- C#System.Text.RegularExpressions.Regex使用(一)
- ExtJS4的文本框(textField)使用正则表达式进行验证(Regex)的方法
- 正则表达式基础应用(匹配matches(regex))
- ExtJS textField 的常用正则表达式验证(Regex)
- php学习第四章:正则表达式(一)正则表达式概论与firefox浏览器正则插件Regular Expressions Tester