Regular Expressions (2) ---- Common Used Samples
說明:
以下所有的例子都在EditPad Pro下經(jīng)過驗證,如果不能用,可能和特定的解釋引擎有關,稍稍修改就可以了。J(看得頭都暈了~~)
更多的例子:http://www.regexlib.com
簡單驗證電子郵件地址 |
|
表達式 |
("w[-._"w]*"w@"w[-._"w]*"w"."w{2,3}) |
描述 |
簡單符合<name>@xxx.com即可。 粗略驗證 |
匹配的例子 |
[foo@bar.com], [foobar@foobar.com.au] |
不匹配的例子 |
[foo@bar], [$$$@bar.com] |
標準驗證電子郵件地址 |
|
表達式 |
^(([^<>;()["]"".,;:@"]+(".[^<>()["]"".,;:@"]+)*)|(".+"))@((([a-z]([-a-z0-9]*[a-z0-9])?)|(#[0-9]+)|("[((([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))".){3}(([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))"]))".)*(([a-z]([-a-z0-9]*[a-z0-9])?)|(#[0-9]+)|("[((([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))".){3}(([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))"]))$ |
描述 |
所有符合RFC 821(http://www.cis.ohio-state.edu/cgi-bin/rfc/rfc0821.html#page-30)規(guī)定的格式的郵件地址 |
匹配的例子 |
blah@[10.0.0.1]|||a@b.c |
不匹配的例子 |
non@match@. |
大于零的Decimal數(shù)字 |
|
表達式 |
(^"d*".?"d*[1-9]+"d*$)|(^[1-9]+"d*"."d*$) |
描述 |
大于零的Decimal數(shù)字 |
匹配的例子 |
0.050 5.0000 5000 |
不匹配的例子 |
0 0.0 .0 |
任何Decimal數(shù)字 |
|
表達式 |
^("d|-)?("d|,)*".?"d*$ |
描述 |
匹配: 純數(shù)字 負數(shù) 逗號分割的數(shù)字 點分割的decimal格式 |
匹配的例子 |
5,000 -5,000 100.044 .2 |
不匹配的例子 |
abc 1.3.4 |
0-99999999的帶或不帶逗號的數(shù)字 |
|
表達式 |
^("d|-)?("d|,)*".?"d*$ |
描述 |
匹配: 0-99999999的帶或不帶逗號的數(shù)字 |
匹配的例子 |
5,000 100,044 |
不匹配的例子 |
123,888,888 1.1 |
0-65535的值 |
|
表達式 |
^([0-5]?"d?"d?"d?"d|6[0-4]"d"d"d|65[0-4]"d"d|655[0-2]"d|6553 [0-5])$ |
描述 |
0-65535數(shù)字 |
匹配的例子 |
5000 100 |
不匹配的例子 |
66666 -2 |
HTML和XML標記 |
|
表達式 |
<[^>]+> |
描述 |
所有的html和xml標記 |
匹配的例子 |
<b> </b> 5.0000 5000 |
不匹配的例子 |
http://www.regexlib.com/Add.aspx |
ISBN格式的字符串 |
|
表達式 |
<[^>]+> |
描述 |
ISBN格式的字符串。 詳細定義:http://www.isbn.org/standards/home/isbn/international/html/usm4.htm |
匹配的例子 |
ISBN 0 93028 923 4 ISBN 1-56389-668-0 ISBN 1-56389-016-X |
不匹配的例子 |
http://www.regexlib.com/Add.aspx |
GUID |
|
表達式 |
^[{|"(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[")|}]?$ |
描述 |
以下格式的32位GUID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) |
匹配的例子 |
914D226A-2F5B-4944-934D-96BBE6571977 {914D226A-2F5B-4944-934D-96BBE6571977} |
不匹配的例子 |
914D226A-2F5B-4944-XXXX-96BBE6571977 {914D226A-2F5B-4944-XXXX-96BBE6571977} |
信用卡號 |
|
表達式 |
^[{|"(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[")|}]?$ |
描述 |
16位數(shù)字 16位數(shù)字用連字符或者空格或者分割 |
匹配的例子 |
1234343425262837 1111-2323-2312-3434 1111 2323 2312 3434 |
不匹配的例子 |
1111 2323 2312-3434 |
連在一起的兩個相同的單詞 |
|
表達式 |
("w+)"s+"1 |
描述 |
驗證連在一起的兩個相同的單詞 |
匹配的例子 |
abc abc |
不匹配的例子 |
abc abcd |
雙引號括起來的詞 |
|
表達式 |
"((""")|[^"(""")])+" |
描述 |
驗證用雙引號括起來的詞 |
匹配的例子 |
“Abc” “abc”sff” |
不匹配的例子 |
“sdfsdfsdf |
驗證html中的16位顏色值 |
|
表達式 |
^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$ |
描述 |
驗證html中的16進制的顏色值 #是可選的。 |
匹配的例子 |
#00ccff ffffcc |
不匹配的例子 |
blue 0x000000 |
驗證包含JSP事件的html代碼 |
|
表達式 |
<[a-zA-Z][^>]*"son"w+=("w+|'[^']*'|"[^"]*")[^>]*> |
描述 |
驗證包含JSP事件的html代碼 |
匹配的例子 |
<IMG onmouseover="window.close()"> |
不匹配的例子 |
IMG src="star.gif" |
驗證一對封閉的xml標記 |
|
表達式 |
^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$ |
描述 |
驗證一對括起來的xml標記。 |
匹配的例子 |
<body> text<br/>More Text </body> <a href="link.html">Link</a> |
不匹配的例子 |
blue 0x000000 |
驗證HTML中所有合法的on事件 |
|
表達式 |
(?i:on(blur|c(hange|lick)|dblclick|focus|keypress|(key|mouse )(down|up)|(un)?load|mouse(move|o(ut|ver))|reset|s(elect|ubm it))) |
描述 |
驗證HTML中所有合法的on事件 |
匹配的例子 |
onclick onmouseover |
不匹配的例子 |
Click Move |
查找html中的注釋 |
|
表達式 |
<!"-"-.*?"-"-> |
描述 |
查找html中的注釋 |
匹配的例子 |
<!-- <h1>this text has been removed</h1> --> |
不匹配的例子 |
<h1>this text has been removed</h1> |
查找html中的特定文件(swf.jpg.gif…) |
|
表達式 |
<[^>]*"n?.*=("|')?(.*".jpg)("|')?.*"n?[^<]*> |
描述 |
查找html中的特定文件(swf.jpg.gif…) 把jpg換為gif,即是查找所有的gif文件。 |
匹配的例子 |
<td background="../img/img.jpg" > |
不匹配的例子 |
= img.jpg |
日期驗證 |
|
表達式 |
^(?:(?:(?:(?:(?:1[6-9]|[2-9]"d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))("/|-|".)(?:0?2"1(?:29)))|(?:(?:(?:1[6-9]|[2-9]"d)?"d{2})("/|-|".)(?:(?:(?:0?[13578]|1[02])"2(?:31))|(?:(?:0?[1,3-9]|1[0-2])"2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))"2(?:0?[1-9]|1"d|2[0-8]))))$ |
描述 |
驗證格式為y/m/d的日期從1600/1/1 - 9999/12/31的日期 |
匹配的例子 |
04/2/29 2002-4-30 02.10.31 |
不匹配的例子 |
2003/2/29 02.4.31 00/00/00 |
日期和時間驗證 |
|
表達式 |
^(?="d)(?:(?:(?:(?:(?:0?[13578]|1[02])("/|-|".)31)"1|(?:(?:0?[1,3-9]|1[0-2])("/|-|".)(?:29|30)"2))(?:(?:1[6-9]|[2-9]"d)?"d{2})|(?:0?2("/|-|".)29"3(?:(?:(?:1[6-9]|[2-9]"d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))("/|-|".)(?:0?[1-9]|1"d|2[0-8])"4(?:(?:1[6-9]|[2-9]"d)?"d{2}))($|" (?="d)))?(((0?[1-9]|1[012])(:[0-5]"d){0,2}(" [AP]M))|([01]"d|2[0-3])(:[0-5]"d){1,2})?$ |
描述 |
驗證所有合法的日期和時間 |
匹配的例子 |
20:20 04/2/29 02.4.31 02.10.312002-4-30 02.10.31 |
不匹配的例子 |
2003/2/29 00/00/00 |
標準ANSI SQL日期驗證 |
|
表達式 |
^(("d{2}(([02468][048])|([13579][26]))["-"/"s]?((((0?[13578])|(1[02]))["-"/"s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))["-"/"s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2["-"/"s]?((0?[1-9])|([1-2][0-9])))))|("d{2}(([02468][1235679])|([13579][01345789]))["-"/"s]?((((0?[13578])|(1[02]))["-"/"s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))["-"/"s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2["-"/"s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))("s(((0?[1-9])|(1[0-2]))":([0-5][0-9])(("s)|(":([0-5][0-9])"s))([AM|PM|am|pm]{2,2})))?$ |
描述 |
匹配ANSI SQL的日期格式:YYYY-mm-dd hh:mi:ss am/pm 包括檢查從1901-2099是否是閏年。 |
匹配的例子 |
2004-2-29 2004-02-29 10:29:39 pm 2004/12/31 |
不匹配的例子 |
04-2-29 04-02-29 10:29:39 pm 04/12/31 |
匹配字體 |
|
表達式 |
^("d)("d)*( )*(px|PX|Px|pX|pt|PT|Pt|pT|)$ |
描述 |
查找字體的后綴 |
匹配的例子 |
1px 100 PT 20Px |
不匹配的例子 |
1abc px 1 sdfs |
匹配MD5哈西字符串 |
|
表達式 |
^("d)("d)*( )*(px|PX|Px|pX|pt|PT|Pt|pT|)$ |
描述 |
^([a-z0-9]{32})$ |
匹配的例子 |
790d2cf6ada1937726c17f1ef41ab125 |
不匹配的例子 |
790D2CF6ADA1937726C17F1EF41AB125 |
Regex的相關工具和word文檔可以在這里找到。:""xafile"Share"Public"Bond"Regex
PS:
這個列表會一直更新的,希望大家也能多多支持。把看到的有用的Regex都貼進來。
J