|
|
Regular Expressions (2) ---- Common Used Samples |
|
|
作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站 |
Regular Expressions (2) ---- Common Used Samples
说明:
以下所有的例子都在EditPad Pro下经过验证,如果不能用,可能和特定的解释引擎有关,稍稍修改就可以了。J (看得头都晕了~~)
更多的例子:http://www.regexlib.com
电子邮件验证... 2
简单验证电子邮件地址... 2
标准验证电子邮件地址... 2
数字验证... 2
大于零的Decimal数字... 2
任何Decimal数字... 2
0-99999999的带或不带逗号的数字... 3
0-65535的值... 3
字符串验证... 3
HTML和XML表姐... 3
ISBN格式的字符串... 3
GUID. 4
信用卡号... 4
连在一起的两个相同的单词... 4
双引号括起来的词... 4
标记... 5
验证html中的16位颜色值... 5
验证包含JSP事件的html代码... 5
验证一对封闭的xml标记... 5
验证HTML中所有合法的on事件... 5
查找html中的注释... 6
查找html中的特定文件(swf.jpg.gif…)... 6
日期和时间验证... 6
日期验证... 6
日期和时间验证... 6
标准ANSI SQL日期验证... 7
其他... 7
匹配字体... 7
匹配MD5哈西字符串... 7
电子邮件验证
标准验证电子邮件地址 |
表达式 |
^(([^<>;()[\]\\.,;:@"]+(\.[^<>()[\]\\.,;:@"]+)*)|(".+"))@((([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)规定的格式的邮件地址 |
匹配的例子 |
blah@[10.0.0.1]|||[email protected] |
不匹配的例子 |
non@match@. |
大于零的Decimal数字 |
表达式 |
(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$) |
描述 |
大于零的Decimal数字 |
匹配的例子 |
0.050
5.0000
5000 |
不匹配的例子 |
0
0.0
.0 |
任何Decimal数字 |
表达式 |
^(\d|-)?(\d|,)*\.?\d*$ |
描述 |
匹配:
纯数字
负数
逗号分割的数字
点分割的decimal格式 |
匹配的例子 |
5,000
-5,000
100.044
.2 |
不匹配的例子 |
abc
1.3.4 |
|
表达式 |
^(\d|-)?(\d|,)*\.?\d*$ |
描述 |
匹配:
0-99999999的带或不带逗号的数字 |
匹配的例子 |
5,000
100,044 |
不匹配的例子 |
123,888,888
1.1 |
|
表达式 |
^([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数字 |
匹配的例子 |
5000
100 |
不匹配的例子 |
66666
-2 |
|
表达式 |
<[^>]+> |
描述 |
所有的html和xml标记 |
匹配的例子 |
<b>
</b>
5.0000
5000 |
不匹配的例子 |
http://www.regexlib.com/Add.aspx |
|
表达式 |
^[{|\(]?[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位数字
16位数字用连字符或者空格或者分割 |
匹配的例子 |
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" |
|
表达式 |
^#?([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 
|
|
相关文章:相关软件: |
|