C#验证类(使用正则表达式)
2015-06-29 17:01
585 查看
原文:http://www.cnblogs.com/wenanry/archive/2006/06/25/435015.html
1
using System;
2
using System.Text.RegularExpressions;
3
4
namespace bobomousecom.crm
5
{
6
/**//**//**//// <summary>
7
/// Regexlib 的摘要说明。
8
/// </summary>
9
public class Regexlib
10
{
11
public Regexlib()
12
{
13
//
14
// TODO: 在此处添加构造函数逻辑
15
//
16
}
17
18
19
//搜索输入字符串并返回所有 href=“”值
20
string DumpHrefs(String inputString)
21
{
22
Regex r;
23
Match m;
24
25
r = new Regex("href\s*=\s*(?:"(?<1>[^"]*)"|(?<1>\S+))",
26
RegexOptions.IgnoreCase|RegexOptions.Compiled);
27
for (m = r.Match(inputString); m.Success; m = m.NextMatch())
28
{
29
return("Found href " + m.Groups[1]);
30
}
31
}
32
33
34
35
//验证Email地址
36
bool IsValidEmail(string strIn)
37
{
38
// Return true if strIn is in valid e-mail format.
39
return Regex.IsMatch(strIn, @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
40
}
41
42
43
//dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。
44
string MDYToDMY(String input)
45
{
46
return Regex.Replace(input,"\b(?\d{1,2})/(?\d{1,2})/(?\d{2,4})\b","${day}-${month}-${year}");
47
}
48
49
50
//验证是否为小数
51
bool IsValidDecimal(string strIn)
52
{
53
54
return Regex.IsMatch(strIn,@"[0].d{1,2}|[1]");
55
}
56
57
58
//验证是否为电话号码
59
bool IsValidTel(string strIn)
60
{
61
return Regex.IsMatch(strIn,@"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?");
62
}
63
64
65
//验证年月日
66
bool IsValidDate(string strIn)
67
{
68
return Regex.IsMatch(strIn,@"^2d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]d|3[0-1])(?:0?[1-9]|1d|2[0-3]):(?:0?[1-9]|[1-5]d):(?:0?[1-9]|[1-5]d)$");
69
}
70
71
72
//验证后缀名
73
bool IsValidPostfix(string strIn)
74
{
75
return Regex.IsMatch(strIn,@".(?i:gif|jpg)$");
76
}
77
78
79
//验证字符是否在4至12之间
80
bool IsValidByte(string strIn)
81
{
82
return Regex.IsMatch(strIn,@"^[a-z]{4,12}$");
83
}
84
85
86
//验证IP
87
bool IsValidIp(string strIn)
88
{
89
return Regex.IsMatch(strIn,@"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$");
90
}
91
}
92
}
1
using System;
2
using System.Text.RegularExpressions;
3
4
namespace bobomousecom.crm
5
{
6
/**//**//**//// <summary>
7
/// Regexlib 的摘要说明。
8
/// </summary>
9
public class Regexlib
10
{
11
public Regexlib()
12
{
13
//
14
// TODO: 在此处添加构造函数逻辑
15
//
16
}
17
18
19
//搜索输入字符串并返回所有 href=“”值
20
string DumpHrefs(String inputString)
21
{
22
Regex r;
23
Match m;
24
25
r = new Regex("href\s*=\s*(?:"(?<1>[^"]*)"|(?<1>\S+))",
26
RegexOptions.IgnoreCase|RegexOptions.Compiled);
27
for (m = r.Match(inputString); m.Success; m = m.NextMatch())
28
{
29
return("Found href " + m.Groups[1]);
30
}
31
}
32
33
34
35
//验证Email地址
36
bool IsValidEmail(string strIn)
37
{
38
// Return true if strIn is in valid e-mail format.
39
return Regex.IsMatch(strIn, @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
40
}
41
42
43
//dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。
44
string MDYToDMY(String input)
45
{
46
return Regex.Replace(input,"\b(?\d{1,2})/(?\d{1,2})/(?\d{2,4})\b","${day}-${month}-${year}");
47
}
48
49
50
//验证是否为小数
51
bool IsValidDecimal(string strIn)
52
{
53
54
return Regex.IsMatch(strIn,@"[0].d{1,2}|[1]");
55
}
56
57
58
//验证是否为电话号码
59
bool IsValidTel(string strIn)
60
{
61
return Regex.IsMatch(strIn,@"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?");
62
}
63
64
65
//验证年月日
66
bool IsValidDate(string strIn)
67
{
68
return Regex.IsMatch(strIn,@"^2d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]d|3[0-1])(?:0?[1-9]|1d|2[0-3]):(?:0?[1-9]|[1-5]d):(?:0?[1-9]|[1-5]d)$");
69
}
70
71
72
//验证后缀名
73
bool IsValidPostfix(string strIn)
74
{
75
return Regex.IsMatch(strIn,@".(?i:gif|jpg)$");
76
}
77
78
79
//验证字符是否在4至12之间
80
bool IsValidByte(string strIn)
81
{
82
return Regex.IsMatch(strIn,@"^[a-z]{4,12}$");
83
}
84
85
86
//验证IP
87
bool IsValidIp(string strIn)
88
{
89
return Regex.IsMatch(strIn,@"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$");
90
}
91
}
92
}
C#正则表达式小结 只能输入数字:"^[0-9]*$"。 只能输入n位的数字:"^\d{n}$"。 只能输入至少n位的数字:"^\d{n,}$"。 只能输入m~n位的数字:。"^\d{m,n}$" 只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。 只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。 只能输入有1~3位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。 只能输入非零的正整数:"^\+?[1-9][0-9]*$"。 只能输入非零的负整数:"^\-[1-9][]0-9"*$。 只能输入长度为3的字符:"^.{3}$"。 只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。 只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。 只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。 只能输入由数字和26个英文字母组成的字符串:"^[A-Za-z0-9]+$"。 只能输入由数字、26个英文字母或者下划线组成的字符串:"^\w+$"。 只能输入由数字、26个英文字母或者下划线,中文组成的字符串:^\\w+$ 验证用户密码:"^[a-zA-Z]\w{5,17}$"正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。 验证是否含有^%&',;=?$\"等字符:"[^%&',;=?$\x22]+"。 只能输入汉字:"^[\u4e00-\u9fa5]{0,}$" 验证Email地址:"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"。 验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。 验证电话号码:"^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$"正确格式为:"XXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"和"XXXXXXXX"。 验证***号(15位或18位数字):"^\d{15}|\d{18}$"。 验证一年的12个月:"^(0?[1-9]|1[0-2])$"正确格式为:"01"~"09"和"1"~"12"。 验证一个月的31天:"^((0?[1-9])|((1|2)[0-9])|30|31)$"正确格式为;"01"~"09"和"1"~"31"。 利用正则表达式限制网页表单里的文本框输入内容: 用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))" 用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))" 用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" 用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" 得用正则表达式从URL地址中提取文件名的javascript程序,如下结果为page1 s="http://www.9499.net/page1.htm" s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2") alert(s) 匹配双字节字符(包括汉字在内):[^\x00-\xff] 应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;} 匹配空行的正则表达式:\n[\s| ]*\r 匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/ 匹配首尾空格的正则表达式:(^\s*)|(\s*$) String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); } 利用正则表达式分解和转换IP地址: 下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的javascript程序: function IP2V(ip) { re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正则表达式 if(re.test(ip)) { return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1 } else { throw new Error("Not a valid IP address!") } } 不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下: var ip="10.100.20.168" ip=ip.split(".") alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
相关文章推荐
- C#高级编程三十二天----数组基础
- C# zip压缩 Ionic.Zip.dll
- Mono制作安装包让C#桌面应用程序脱离net framework步骤
- C# FFmpeg获取码率
- C#操作字符串方法总结<转>
- C#实现将选中复选框的信息返回给用户的方法
- C#判断多个文本框是否为空的方法
- C# 执行批处理文件(*.bat)的方法代码
- C#中字符串中含有\0的问题处理
- C#趣味程序---三色球问题
- C#中Base64之编码,解码方法
- C#执行外部命令的方法
- C# 非托管内存使用时的注意事项
- 【C#】datagridview里加一列序号
- C#计算函数执行的时间
- C#接口interface用法实例
- C#实现AddRange为数组添加多个元素的方法
- C#趣味程序---爱因斯坦的台阶问题
- C#,动态加载DLL,通过反射,调用参数,方法,窗体
- c#第5章 变量的更多内容 隐式和显式转换、枚举、结构、数组、