工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1911|回复: 6

正则表达式简介

[复制链接]
发表于 2006-6-18 22:48 | 显示全部楼层 |阅读模式
正则表达式(Regular Expressions)是用来处理文本和匹配模式的。很多语言都支持正则表达式,比如:perl,JAVA,JSCRIPT等等。
    我们在某些文本编辑器进行查找替换时,其实也算是用了正则表达式。只是正则表达式的功能远不止此。先看看有哪些符号,匹配什么?

在JDK1.5的文档中,有这样的文字解释:


  1. Character classes
  2. [abc] a, b, or c (simple class)
  3. [^abc] Any character except a, b, or c (negation)
  4. [a-zA-Z] a through z or A through Z, inclusive (range)
  5. [a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
  6. [a-z&&[def]] d, e, or f (intersection)
  7. [a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
  8. [a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)

  9. Predefined character classes
  10. . Any character (may or may not match line terminators)
  11. \d A digit: [0-9]
  12. \D A non-digit: [^0-9]
  13. \s A whitespace character: [ \t\n\x0B\f\r]
  14. \S A non-whitespace character: [^\s]
  15. \w A word character: [a-zA-Z_0-9]
  16. \W A non-word character: [^\w]
复制代码


不想翻译它,给点中文的。

  1. \w  字母或数字;相当于 [0-9A-Za-z]

  2. \W  非字母,数字

  3. \s  [ \t\n\r\f]空字符;相当于 [ \t\n\r\f]

  4. \S  非空字符

  5. \d  [0-9]数字;相当于 [0-9]

  6. \D  非数字字符

  7. *  前面元素出现0或多次

  8. +  前面元素出现1或多次

  9. {m,n}  前面元素最少出现m次,最多出现n次

  10. ?  前面元素最多出现1次;相当于 {0,1}

  11. $ 匹配输入字符串的结尾位置。
复制代码


这些也只是部分而已。先给个例子。
在网上填资料时,有时会遇到输入框只能输入数据或者只能输入英文的情况。好,就给个只能输入数字的例子。

只能输入数字:
<input type="text" onkeyup="value=value.replace(/\D/g,'')"> 或:
<input type="text" onkeyup="value=value.replace(/[^0-9]/g,'')">

有时候用来验证输入是否合法,比如E-mail地址验证。

  1. <script language="javascript">
  2. function emailCheck(strEmail)
  3. {
  4.         var myReg = /^[_a-zA-Z0-9]+@([_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/;
  5.         if(myReg.test(strEmail))
  6.         alert("有效Email地址");
  7.         else
  8.         alert("无效Email地址");
  9. }
  10. </script>
  11. <form>
  12. <input type="text" name="mytext">
  13. <input type="button" value="OK" onsubmit="return false" onclick="emailCheck(mytext.value)">
  14. </form>
复制代码


还有日期验证,网址验证等,这里就不再给出例子了。在JAVA里要用到Pattern和Match类,也免了例子。

本文只是简单的介绍,有兴趣的话可以关注下正则表达式,大家一起学习交流。
发表于 2006-6-19 09:39 | 显示全部楼层
j2ee中的validator框架,就是struts里面集成的那个验证框架,就支持在配置文件中写大量正则表达式来实现输入验证。
回复

使用道具 举报

发表于 2006-9-3 10:30 | 显示全部楼层
开始学正则表达式了。。。。
回复

使用道具 举报

发表于 2006-9-3 10:53 | 显示全部楼层
有个问题:在DW里,自带了的JS:

  1. /\D+\_+\W+\s+\S+/
复制代码


说是onlyNumbers但测试了好像不对。。。
onlyNumbers 应该是这个吧:
  1. /^\d+$/
复制代码
回复

使用道具 举报

发表于 2006-9-3 13:24 | 显示全部楼层
学习正则表达式,做了个辅助页面。。。

将下面代码保存成一个HTM文件即可


  1. 规则输入例子: /\d/ 应输入为 \d 即不用两个斜杆,<br />
  2. 表达式:<br /><textarea name="regularStr" id="regularStr" cols="40" rows="2"></textarea><br />
  3. 测试字符串:<br /><textarea name="inStr" id="inStr" cols="40" rows="2"></textarea> <br />
  4. <input type="button" name="chk" id="chk"  value="Check"/><br />
  5. <textArea  name="output" id="output"  cols="16" rows="13"></textArea>
  6. <script language="javascript" src="parase.js" type="text/javascript"></script>
  7. <script type="text/javascript">
  8. chk.onclick=function(){
  9.         var inString=inStr.value;
  10.         var reg=new RegExp(regularStr.value);
  11.         output.value=reg+".test("+inString+")="+reg.test(inString);       
  12. }</script>
复制代码

[ 本帖最后由 iptton 于 2006-9-3 13:49 编辑 ]
回复

使用道具 举报

发表于 2006-9-3 16:20 | 显示全部楼层
^(\d+|\(\d+,\d+\))([\+\-\*\/](\d+|\(\d+,\d+\)))*$

这个表达式表示为:

以 (a,b) 和c 两种式子为单元(?)的四则算式如

(3,4)*3+(44,54)-(23,3)

不知道对不对....

验算了几个
回复

使用道具 举报

发表于 2006-9-4 20:06 | 显示全部楼层
validator,spring中也有,只是觉得有些地方处理不是很好,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-15 07:08

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表