07-23


1、入门简介

  简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具。我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例如,vi编辑器,Perl或PHP脚本语言,以及awk或sed shell程序等。此外,象JavaScript这种客户端的脚本语言也提供了对正则表达式的支持。由此可见,正则表达式已经超出了某种语言或某个系统的局限,成为人们广为接受的概念和功能。
  正则表达式可以让用户通过使用一系列的特殊字符构建匹配模式,然后把匹配模式与数据文件、程序输入以及WEB页面的表单输入等目标对象进行比较,根据比较对象中是否包含匹配模式,执行相应的程序。
  举例来说,正则表达式的一个最为普遍的应用就是用于验证用户在线输入的邮件地址的格式是否正确。如果通过正则表达式验证用户邮件地址的格式正确,用户所填写的表单信息将会被正常处理;反之,如果用户输入的邮件地址与正则表达的模式不匹配,将会弹出提示信息,要求用户重新输入正确的邮件地址。由此可见正则表达式在WEB应用的逻辑判断中具有举足轻重的作用。
查看全文»


04-08


网上搜集了些实例

正则表达式是一个查询的字符串,它包含一般的字符和一些特殊的字符,特殊字符可以扩展查找字符串的能力,正则表达式在查找和替换字符串的作用不可忽视,它能很好提高工作效率。

EditPlus的查找,替换,文件中查找支持以下的正则表达式:

Expression Description
\t Tab character.
\n New line.
. Matches any character.
| Either expression on its left and right side matches the target string.
For example, “a|b” matches “a” and “b”.
[] Any of the enclosed characters may match the target character.
For example, “[ab]” matches “a” and “b”. “[0-9]” matches any digit.
[^] None of the enclosed characters may match the target character.
For example, “[^ab]” matches all character EXCEPT “a” and “b”.
“[^0-9]” matches any non-digit character.
* Character to the left of asterisk in the expression should match 0 or more times.
For example “be*” matches “b”, “be” and “bee”.
+ Character to the left of plus sign in the expression should match 1 or more times.
For example “be+” matches “be” and “bee” but not “b”.
? Character to the left of question mark in the expression should match 0 or 1 time.
For example “be?” matches “b” and “be” but not “bee”.
^ Expression to the right of ^ matches only when it is at the beginning of line.
For example “^A” matches an “A” that is only at the beginning of line.
$ Expression to the left of $ matches only when it is at the end of line.
For example “e$” matches an “e” that is only at the end of line.
() Affects evaluation order of expression and also used for tagged expression.
\ scape character. If you want to use character “\” itself, you should use “\\”.

查看全文»


02-11


下面都是我收集的一些比较常用的正则表达式,因为平常可能在表单验证的时候,用到的比较多。特发出来,让各位朋友共同使用。呵呵。

匹配中文字符的正则表达式: [u4e00-u9fa5]
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了

匹配双字节字符(包括汉字在内):[^x00-xff]
评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

匹配空白行的正则表达式:ns*r
评注:可以用来删除空白行

匹配HTML标记的正则表达式:< (S*?)[^>]*>.*?|< .*? />
评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力

匹配首尾空白字符的正则表达式:^s*|s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式

查看全文»


02-11


表单的验证在实际的开发当中是件很烦琐又无趣的事情
今天在做一个小项目的时候,需要JS验证,寻找到一个比较好的东西

地址如下:

http://blog.csdn.net/goodfunman/archive/2005/10/21/513338.aspx
http://blog.csdn.net/yhl_621/archive/2006/03/04/615273.aspx
http://blog.csdn.net/NetDreamwing/archive/2004/10/11/131975.aspx
http://regexlib.com/Search.aspx

查看全文»


01-23


"^\d+$"  //非负整数(正整数 + 0
 
"^[0-9]*[1-9][0-9]*$"  //正整数
 
"^((-\d+)|(0+))$"  //非正整数(负整数 + 0
 
"^-[0-9]*[1-9][0-9]*$"  //负整数
 
"^-?\d+$"    //整数
 
"^\d+(\.\d+)?$"  //非负浮点数(正浮点数 + 0
 
"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数
 
"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"  //非正浮点数(负浮点数 + 0
 
"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数
 
"^(-?\d+)(\.\d+)?$"  //浮点数
 
"^[A-Za-z]+$"  //由26个英文字母组成的字符串
 
"^[A-Z]+$"  //由26个英文字母的大写组成的字符串
 
"^[a-z]+$"  //由26个英文字母的小写组成的字符串
 
"^[A-Za-z0-9]+$"  //由数字和26个英文字母组成的字符串
 
"^\w+$"  //由数字、26个英文字母或者下划线组成的字符串
 
"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"    //email地址
 
"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$"  //url