概要
				
		
		
				什么是正则表达式,正则表达式用来干什么,可能会困扰一些初学者。我们都用过一些文本编辑器,例如
				Word
				,
				Word
				为我们提供“查找—替换”功能,正则表达式就类似于
				Word
				的这种功能。
				“
				正则表达式
				”
				(
				Regular Expression
				)就是一个字符构成的串,它定义了一个用来搜索匹配字符串的模式。
				
						
						
				
		
		
				
						
								Java.util.
						Pattern
				
		
		
				JDK
				对正则表达式的支持,来源于
				java.util.Pattern
				类。在
				java api
				中定义
				Pattern
				为“
				A 
		
		
				compiled representation of a regular expression
				”,译为“正则表达式编译后的表现”。
		
		
				
						Java.util.Matcher
				
		
		
				在
				java api
				中定义
				Matcher
				为“
				An engine that performs match operations on 
				a character sequence by interpreting a Pattern
				”,
可以译为“依据解释
				Pattern
				而生成的字符序列来执行匹配操作的引擎”。
				在
				API
				中,给出了典型的调用过程:
		
		
				
						      Pattern p = Pattern.compile("a*b");   //
						正则表达式
				
				
						      Matcher m = p.matcher("aaaaab");      //
						代匹配的字符串
						
								
								
						
				
				
						      boolean b = m.matches();
				
		
		
				
						正则表达式摘要
				
		
		
				
				
				
						字符集
				
				
						(characters)
				
		
		
				
						
								| 
												
														字符
														
																
																
														
												
										 | 
												
														含义
														
																
																
														
												
										 | 
						
								| 
												x
										 | 
												X
												字符
										 | 
						
								| 
												\\
										 | 
												反斜线符号
												(\)
										 | 
						
								| 
												
														
																\0
														
												
												
														n
												
										 | 
												八进制数值
												(
												其中
												0<=n<=7)
										 | 
						
								| 
												
														
																\0
														
												
												
														nn
												
										 | 
												八进制数值
												(
												其中
												0<=n<=7)
										 | 
						
								| 
												
														
																\0
														
												
												
														mnn
												
										 | 
												八进制数值
												(
												其中
												0<=m<=3
												,
												0<=n<=7)
										 | 
						
								| 
												
														
																\x
														
												
												
														hh
												
										 | 
												十六进制数值
										 | 
						
								| 
												
														
																\t
														
												
										 | 
												Tab
												字符
												(‘
												
														
																\u0009'
														
												
												’)
										 | 
						
								| 
												
														
																\e
														
												
										 | 
												Esc
												字符
												(‘
												
														
																\u001B'
														
												
												’)
										 | 
						
								| 
												
														
																\n
														
												
										 | 
												换行符
												(‘
												
														
																\u000A'
														
												
												’)
										 | 
						
								| 
												
														
																\d
														
												
										 | 
												[0-9]
										 | 
						
								| 
												
														
																\D
														
												
										 | 
												[^0-9]
										 | 
						
								| 
												
														
																\w
														
												
										 | 
												[A-Z0-9]
										 | 
						
								| 
												
														
																
																		 
																
														
												
										 | 
												
														 
												
										 | 
						
								| 
												
														
																
																		 
																
														
												
										 | 
												
														 
												
										 | 
				
		
		
				
						字符类集
				
		
		
				
						
								| 
												
														字符
														
																
																
														
												
										 | 
												
														含义
														
																
																
														
												
										 | 
						
								| 
												
														
																[abc]
														
												
										 | 
												
														
																a
														
												
												, 
												
														
																b
														
												
												, or 
												
														
																c
														
												
												(simple class)
										 | 
						
								| 
												
														
																[^abc]
														
												
										 | 
												Any character except 
												
														
																a
														
												
												, 
												
														
																b
														
												
												, or 
												
														
																c
														
												
												(
												否
												)
										 | 
						
								| 
												
														
																[a-zA-Z]
														
												
										 | 
												
														
																a
														
												
												through 
												
														
																z
														
												
												or 
												
														
																A
														
												
												through 
												
														
																Z
														
												
												, inclusive (
												区间
												)
										 | 
						
								| 
												
														
																[a-d[m-p]]
														
												
										 | 
												
														
																| 
																				
																						[a-d[m-p]]
																				
																		 | 
																				
																						a through d, or m through p: [a-dm-p] (
																				
																				
																						联合)
																				
																		 |  
												
														
														
												
										 | 
						
								| 
												
														
																[a-z&&[def]]
														
												
										 | 
												
														
																d
														
												
												, 
												
														
																e
														
												
												, or 
												
														
																f
														
												
												(
												交集
												)
										 | 
						
								| 
												
														
																[a-z&&[^bc]]
														
												
										 | 
												
														
																a
														
												
												through 
												
														
																z
														
												
												, except for 
												
														
																b
														
												
												and 
												
														
																c
														
												
												: 
												
														
																[ad-z]
														
												
												(
												子集
												)
										 | 
						
								| 
												
														
																[a-z&&[^m-p]]
														
												
										 | 
												
														
																a
														
												
												through 
												
														
																z
														
												
												, and not 
												
														
																m
														
												
												through 
												
														
																p
														
												
												: 
												
														
																[a-lq-z]
														
												
												(
												子集
												)
										 | 
				
		
		其他的正则表达式请参考API。
示例
      private static final String procedureREx = "(REP_(\\d{1})_(\\d{1,2})(_(\\d{1}))?)";  //正则表达式
     
   private static ProcedureInfo getProcedureInfo(String procedure) {
        ProcedureInfo info = new ProcedureInfo();
        String procedureName = null;
        String category = null;
        String reportNo = null;
        String rowType = null;
        Pattern procPattern = Pattern.compile(procedureREx);      //编译正则表达式
        Matcher matcher = procPattern.matcher(procedure);         //匹配字符串到正则表达式
        if (matcher.find()) {                                    //字符串是否匹配正则表达式
            procedureName = matcher.group(1);
            category = matcher.group(2);                     
            reportNo = matcher.group(3);
            rowType = matcher.group(5);
            info.setName(procedureName);
            info.setCategory(Integer.parseInt(category));
            info.setReportNo(Integer.parseInt(reportNo));
            if (rowType != null) {
                info.setRowType(Integer.parseInt(rowType));
            }
            info.setArgs(getArgs(procedure));
            return info;
        } else {
            System.out.println("can't recognize procedure:" + procedure);
            return null;
        }
    }
参考:
http://buy.ccw.cn/htm/app/aprog/01_7_31_4.asp
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/package-summary.html
http://www.javaresearch.org/article/showarticle.jsp?column=331&thread=2488
	posted on 2006-04-25 17:43 
zhangxl 阅读(785) 
评论(1)  编辑  收藏  所属分类: 
JAVA 基础文章