2using System.Text.RegularExpressions;
3
4namespace 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}
93
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))