|
Java 库的建立方法及其实例(4) 下面,我们以一个具体的有实际用处的类库来进一步讨论库的设计方法。这个库是jregex. (jregex.sourceforge.net)。这是一个用Java实现的兼容Perl 5.6的正则表达式的库。有很多这样的库, 比如gnu.regexp,com.stevesoft.pat, 还有J2SE SDK 1.4中新增加的regex.为什么选用jregex呢?是因为 它是目前源代码公开的regex库中兼容Perl 5.6的正则表达式,而且刚刚更新过源代码,并且是稳定 版,另外一个就是,它的内核算法选用了NFA(Not Finite Automata)。
要了解一个包,在看源代码之前先应该看的就是它的API文档。那么,看文档的第一步应该看什么 呢?当然是树形结构。
Class Hierarchy
class java.lang.Object
class jregex.Matcher (implements jregex.MatchResult)
class jregex.Optimizer
class jregex.util.io.PathPattern
class jregex.Pattern (implements jregex.REFlags, java.io.Serializable)
class jregex.PerlSubstitution (implements jregex.Substitution)
class jregex.Replacer
class jregex.RETokenizer (implements java.util.Enumeration)
class java.lang.Throwable (implements java.io.Serializable)
class java.lang.Exception
class java.lang.RuntimeException
class java.lang.IllegalArgumentException
class jregex.PatternSyntaxException
class jregex.util.io.WildcardFilter (implements java.io.FilenameFilter)
Interface Hierarchy
interface jregex.MatchIterator
interface jregex.MatchResult
interface jregex.REFlags
interface jregex.Substitution
interface jregex.TextBuffer
interface jregex.Replacer.WriterWrap
--------------------------------------------------------------------------------
在一个正则表达式中,我们知道有两个元素很重要,第一个就是Pattern(模式), 第二个是Matcher(匹 配结果字符串)。在jregex中,Pattern类实现了jregex.REFlags interface, 和java.io.Serializable。先来看 看jregex.REFlags的说明。jregex.REFlags定义了一些静态的常量,看起来是一些标志。Pattern实现了 jregex.REFlags, 也就是说,Pattern类中包含了这些静态的常量。
下一步,我们看看Pattern的API说明:
Pattern是一个预编译好的正则表达式的表示。要匹配一个正则表达式,先创建一个Pattern实例:
Pattern p=new Pattern(myExpr);
然后取得Matcher的实例
Matcher matcher=p.matcher(myText);
Matcher的实例是一个自动的匹配和搜索的对象。它提供如下方法:
搜索匹配结果: matcher.find() or matcher.findAll();
监测是否全文匹配 : matcher.matches();
监测是否匹配开头 : matcher.isStart();
带选项的查找 : matcher.find(int options)
标志
标志(参考REFlags)改变了在预编译的时候正则表达式符号的意义。这些标志是:
REFlags.IGNORE_CASE - 忽略大小写
REFlags.MULTILINE - 用^和$来表示一行文本的开头和结尾
REFlags.DOTALL - 用.来表示回车换行
REFlags.IGNORE_SPACES - 忽略空格
REFlags.UNICODE - 使用UNICODE, 即w, d不再被解释为正则表达式的意义,而是被解释为 UNICODE.
REFlags.XML_SCHEMA - 使用XML语义。
Pattern是线程安全的。也就是说,你可以在不同的线程中使用同一个Pattern的实例。
在API函数说明中,我们还能看到Pattern类的public方法。这一点将在下面有用处。先来看看:
构造函数
Pattern(java.lang.String regex)
Compiles an expression with default flags.
Pattern(java.lang.String regex, int flags)
Compiles a regular expression using REFlags.
Pattern(java.lang.String regex, java.lang.String flags)
Compiles a regular expression using Perl5-style flags.
方法
int groupCount()
How many capturing groups this expression includes?
java.lang.Integer groupId(java.lang.String name)
Get numeric id for a group name.
Matcher matcher()
Returns a targetless matcher.
Matcher matcher(char[] data, int start, int end)
Returns a matcher for a specified region.
Matcher matcher(MatchResult res, int groupId)
Returns a matcher for a match result (in a performance-friendly way).
Matcher matcher(MatchResult res, java.lang.String groupName)
Just as above, yet with symbolic group name.
Matcher matcher(java.io.Reader text, int length)
Returns a matcher taking a text stream as target.
Matcher matcher(java.lang.String s)
Returns a matcher for a specified string.
Replacer replacer(java.lang.String expr)
Returns a replacer of a pattern by specified perl-like expression.
Replacer replacer(Substitution model)
Returns a replacer will substitute all occurences of a pattern through applying a user-defined substitution model.
RETokenizer tokenizer(char[] data, int off, int len)
Tokenizes a specified region by an occurences of the pattern.
RETokenizer tokenizer(java.io.Reader in, int length)
Tokenizes a specified region by an occurences of the pattern.
RETokenizer tokenizer(java.lang.String text)
Tokenizes a text by an occurences of the pattern.
java.lang.String toString_d()
Returns a less or more readable representation of a bytecode for the pattern.
java.lang.String toString()
|