java正则表达式: regular expression(二)

一个正则表达式应用实践

Regexes let you create powerful text-processing applications. One application you might find helpful extracts comments from a Java, C, or C++ source file, and records those comments in another file. Listing 2 presents that application's source code:
Listing 2. ExtCmnt.java
 
  1. // ExtCmnt.java  
  2.   
  3. import java.io.*;  
  4. import java.util.regex.*;  
  5.   
  6. class ExtCmnt {  
  7.     public static void main (String [] args) {  
  8.         if (args.length != 2) {  
  9.             System.err.println ("usage: java ExtCmnt infile outfile");  
  10.             return;  
  11.         }  
  12.   
  13.         Pattern p;  
  14.         try {  
  15.             // The following pattern lets this extract multi-line comments that  
  16.             // appear on a single line (e.g., /* same line */) and single-line  
  17.             // comments (e.g., // some line). Furthermore, the comment may  
  18.             // appear anywhere on the line.  
  19.   
  20.             p = Pattern.compile (".*/\\*.*\\*/|.*//.*$");  
  21.         } catch (PatternSyntaxException e) {  
  22.             System.err.println ("Regex syntax error: " + e.getMessage ());  
  23.             System.err.println ("Error description: " + e.getDescription ());  
  24.             System.err.println ("Error index: " + e.getIndex ());  
  25.             System.err.println ("Erroneous pattern: " + e.getPattern ());  
  26.             return;  
  27.         }  
  28.   
  29.         BufferedReader br = null;  
  30.         BufferedWriter bw = null;  
  31.   
  32.         try {  
  33.             FileReader fr = new FileReader (args [0]);  
  34.             br = new BufferedReader (fr);  
  35.   
  36.             FileWriter fw = new FileWriter (args [1]);  
  37.             bw = new BufferedWriter (fw);  
  38.   
  39.             Matcher m = p.matcher ("");  
  40.             String line;  
  41.             while ((line = br.readLine ()) != null) {  
  42.                 m.reset (line);  
  43.                 /* entire line must match */   
  44.                 if (m.matches ()){  
  45.                     bw.write (line);  
  46.                     bw.newLine ();  
  47.                 }  
  48.             }  
  49.         } catch (IOException e) {  
  50.             System.err.println (e.getMessage ());  
  51.             return;  
  52.         } finally // Close file {  
  53.             try {  
  54.                 if (br != null)  
  55.                     br.close ();  
  56.   
  57.                 if (bw != null)  
  58.                     bw.close ();  
  59.             } catch (IOException e) {}  
  60.         }  
  61.     }  
  62. }  
在创建Pattern 和Matcher 对象之后,ExtCmnt 逐行的读取一个文本文件的内容。对于每一行,matcher尝试匹配pattern的行,鉴别是一个单行的注释或者多行的注释在一行中出现。假如一行匹配 pattern,ExtCmnt 将此行写入另外一个文本文件中。例如,java ExtCmnt ExtCmnt.java out 读取ExtCmnt.java 文件的每一行,根据pattern来尝试着一行,将匹配的行输出到名叫out的文件。 (不要担心理解文件的读写逻辑。我将在将来的文章中explore这些代码。) 在ExtCmnt执行完成,out 文件包含了以下行:

// ExtCmnt.java
// The following pattern lets this extract multiline comments that
// appear on a single line (e.g., /* same line */) and single-line
// comments (e.g., // some line). Furthermore, the comment may
// appear anywhere on the line.
p = Pattern.compile (".*/\\*.*\\*/|.*//.*$");
if (m.matches ()) /* entire line must match */
finally // Close file.

这个输出显示ExtCmnt 并不完美:p = Pattern.compile (".*/\\*.*\\*/|.*//.*$"); 没有描绘一个注释。出现在out中的行因为ExtCmnt的matcher匹配了//字符。

关于pattern ".*/\\*.*\\*/|.*//.*$"由一些有趣的事,竖线元字符metacharacter (|)。依照SDK documentation,圆括号元字符在capturing group和 竖线元字符是逻辑操作符号。vertical bar 描述了一个matcher,它使用操作符左侧的正则表达式结构来在matcher的文本中定为一个match。假如没有match存在,matcher使 用操作符号右侧的正则表达式进行再次的匹配尝试。

温习

尽管正则表达式简化了在text处理程序中pattern匹配的代码,除非你理解它们,否则你不能有效的在你的程序中使用正则表达式。这篇文章通过介绍给 你regex terminology,the java.util.regex 包和示范regex constructs的程序来让你对正则表达式有一个基本的理解。既然你对regexes有了一个基本的理解,建立在通过阅读additional articles (see Resources)和学习java.util.regex's SDK 文档,那里你可以学习更多的regex constructs ,例如POSIX (Portable Operating System Interface for Unix) 字符类。

我鼓励你用这篇文章中或者其它以前文章中资料中问题email me。(请保持问题和这个栏目讨论的文章相关性。)你的问题和我的回答将出现在相关的学习guides中。)

After writing Java 101 articles for 28 consecutive months, I'm taking a two-month break. I'll return in May and introduce a series on data structures and algorithms.
About the author

Jeff Friesen has been involved with computers for the past 23 years. He holds a degree in computer science and has worked with many computer languages. Jeff has also taught introductory Java programming at the college level. In addition to writing for JavaWorld, he has written his own Java book for beginners—Java 2 by Example, Second Edition (Que Publishing, 2001; ISBN: 0789725932)—and helped write Using Java 2 Platform, Special Edition (Que Publishing, 2001; ISBN: 0789724685). Jeff goes by the nickname Java Jeff (or JavaJeff). To see what he's working on, check out his Website at http://www.javajeff.com.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值