regular expressions的漫长历史开始于计算机科学理论领域自动控制原理和formal 语言理论。它的历史延续到unix和其它的操作系统,在那里正则表达式被经常用作在unix和unix-like的工具中:像awk(一个由其创作者,aho, weinberger, and kernighan,命名,能够进行文本分析处理的编程语言), emacs (一个开发工具),和grep (一个在一个或多个文件中匹配正则表达式,为了全局地正则表达式打印的工具。
什么是正则表达式? a regular expression,也被known as regex or regexp,是一个描述了一个字符串集合的pattern(template)。这个pattern决定了什么样的字符串属于这个集合,它由文本字符和元字符(metacharacters,由有特殊的而不是字符含义的字符)组成。为了识别匹配的检索文本的过程—字符串满足一个正则表达式—称作模式匹配(pattern matching)。
假如我们用前述的metacharacter 替换.ox会怎么样呢?也就是,我们指定java regexdemo . "the quick brown fox jumps over the lazy ox."会有什么样的输出,因为period metacharacter 匹配任何字符, regexdemo 在命令行输出每一个匹配字符,包括结尾的period字符。
)中出现时(像 //. or //q.//e)的双倍的反斜杠。不要在当它在命令行参数中出现的时候用双倍的反斜杠。
character classes
有时我们限定产生的matches到一个特定的字符集和。例如,我们可以检索元音a, e, i, o, and u ,任何一个元音字符的出现都以为着一个match。a character类, 通过在方括号之间的一个字符集和指定的regex construct ,帮我们完成这个任务。pattern 支持以下的character classes:
简单字符: 支持被依次放置的字符串并仅匹配这些字符。例如:[abc] 匹配字符a, b, and c。以下的命令行提供了另外一个示例: java regexdemo [csw] cave
通过将它们放置在一起来在一个range character class内联合多个范围。例如:[a-za-z] 匹配所有的大写和小写字母。
联合: 由多个嵌套的character classes组成,匹配属于联合结果的所有字符。例如:[a-d[m-p]] 匹配字符a到d和m到p。 characters a through d and m through p。以下的命令行提供了另外一个示例: java regexdemo [ab[c-e]] abcdef
java regexdemo [ab[c-e]] abcdef 匹配它们在abcdef中的副本 a, b, c, d, and e。没有其它的匹配存在。
交集: 由所有嵌套的class的共同部分组成,且仅匹配字符的共同部分。例如:[a-z&&[d-f]] 匹配字符d, e, 和 f。以下的命令行提供了另外一个示例: java regexdemo [aeiouy&&[y]] party
java regexdemo [aeiouy&&[y]] party 匹配在party中的y。没有其它的匹配存在。
the 回车符 (/r/) the 回行符 (/n) the 回车符紧跟回行符 (/r/n) the 回行字符 (/u0085) the 行分割字符 (/u2028) the 段落分割字符 (/u2029)
捕获组
pattern支持在pattern匹配的过程中,一个regex construct 调用capturing group 来保存为了以后recall 的match的字符;此结构是由圆括号包围的字符序列。在捕获的group中的所有字符在匹配的过程中被作为一个单独的单元。例如,(java) capturing group 结合了字符 j, a, v, 和a为一个单独单元。capturing group依据在text中java的出现来匹配java pattern。每一个match用下一个匹配的java字符替代了前一个match保存的java字符。
capturing groups 在其它capturing groups内被嵌套。例如:在(java( language)),( language) 在(java)内嵌套。每一个嵌套或非嵌套的capturing group有它自己的数字,数字从1开始,capturing 数字从左边至右。在这个例子中,(java( language))是capturing group 1,( language)是capturing group 2。在(a)(b),(a)是捕获组1,(b)是捕获组2。
每一个capturing group随后通过a back reference来recall保存的match。指定跟随在一个反斜杠后的数字来指示一个capturing group,back reference来 recalls 一个capturing group捕获的文本字符。一个back reference 的出现导致了一个matcher 使用the back reference的 capturing group number来recall捕获组保存的match ,然后使用匹配的字符进行进一步的匹配操作。随后的示例示范了为了检查语法错误进行的text 搜索的用法:
java regexdemo "(java( language)/2)" "the java language language"
这个例子使用(java( language)/2) regex为了检查语法错误,来检索字符串the java language language,那里java直接地在两个连续出现的language之前。regex 指定了两个capturing groups: number 1 is (java( language)/2), 它匹配java language language,number 2 is ( language), 它匹配由language跟随的space characer。/2 back reference recalls number 2's 保存的match,它允许matcher检索空格后跟随language的第二次出现,which 直接地跟随space character and language的第一次出现。随后的输出显示了regexdemo's matcher 找到了什么:
regex = (java( language)/2) text = the java language language
found java language language starting at index 4 and ending at index 26
matcher假设了确定的却省值,例如大小写敏感的匹配。一个程序可以使用an embedded flag expression 来覆盖缺省值,也就是,使用一个正则表达式结构,圆括号元字符包围一个问号元字符后跟小写字母。pattern认可以下的embedded flag expressions :
(?i): enables 大小写不敏感的pattern 匹配。例如:java regexdemo (?i)tree treehouse 来匹配tree和tree。大小写敏感是缺省值。 (?x): 允许空格和注释用#元字符开始出现在pattern中。一个matcher 忽略全部它们。例如:java regexdemo ".at(?x)#match hat, cat, and so on" matter 匹配.at和mat。缺省地,空格和注释式不被允许的;一个matcher 将它们考虑为对match有贡献的字符。 (?s): 使dotall 方式有效。在这种模式中,句点除了其它字符外还匹配text结束。 例如:java regexdemo (?s). /n ,. 匹配了 /n。nondotall 方式是缺省的:不匹配行结尾。 (?m): 使多行方式有效。在多行模式下 ^ and $ 恰好分别的在一行的终结或末端之后或者之前。例如:java regexdemo (?m)^.ake make/rlake/n/rtake 匹配 .ake 和 make、 lake与 take。非多行模式是缺省的: ^ and $ match仅在整个文本的开始和结束。 (?u): enables unicode-aware case folding. this flag works with (?i) to perform case-insensitive matching in a manner consistent with the unicode standard. the default: case-insensitive matching that assumes only characters in the us-ascii character set match。 (?d): enables unix lines mode. in that mode, a matcher recognizes only the /n line terminator in the context of the ., ^, and $ metacharacters. non-unix lines mode is the default: a matcher recognizes all terminators in the context of the aforementioned metacharacters。 embedded flag expressions 类似于 capturing groups因为两个regex constructs都用圆括号包围字符。不像capturing group,embedded flag expression 没有捕获匹配的字符。因而,一个embedded flag expression是noncapturing group的特例。也就是说,一个不捕获text字符的regex construct ;它指定了由元字符圆括号包围的字符序列。在pattern's sdk 文档中出现了一些noncapturing groups。
tip
为了在正则表达式中指定多个embedded flag 表达式。或者吧它们并排的放在一起 (e.g., (?m)(?i)) 或者 把它们的小写字母并排的放在一起 (e.g., (?mi))。
public static pattern compile(string regex): 将regex内容编译为在一个新的pattern对象内存储的树状结构的对象表示。返回那个对象引用。例如:pattern p = pattern.compile ("(?m)^//.");创建了一个,存储了一个编译的表示了匹配以句点开始的行的表示。 public static pattern compile(string regex, int flags): 完成前一个方法的相同任务。然而,它同时考虑包含了flag常量(flags指定的)。flag常量在pattern中(except the canonical equivalence flag, canon_eq)被作为二选一的embedded flag expressions被声明。例如:pattern p = pattern.compile ("^//.", pattern.multiline);和上一个例子等价,pattern.multiline 常量和the (?m) embedded flag 表达式完成相同的任务。(参考sdk's pattern 文档学习其它的常量。) 假如不是这些在pattern中被定义的常量在flag中出现,方法将抛出illegalargumentexception 异常。 假如需要,通过调用以下方法可以得到一个pattern对象的flag和最初的被编译为对象的正则表达式:
public int flags(): 返回当正则表达式编译时指定的pattern的flag。例如:system.out.println (p.flags ()); 输出p引用的的pattern相关的flag。 public string pattern(): 返回最初的被编译进pattern的正则表达式。例如:system.out.println (p.pattern ()); 输出对应p引用pattern的正则表达式。(matcher 类包含了类似的返回matcher相关的pattern对象的pattern pattern() 方法。) 在创建一个pattern对象后,你一般的通过调用pattern的公有方法matcher(charsequence text)获得一个matcher对象。这个方法需要一个简单的,实现了charsequence接口的文本对象参数。获得的对象在pattern匹配的过程中扫描输入的文本对象。例如:pattern p = pattern.compile ("[^aeiouy]"); matcher m = p.matcher ("this is a test."); 获得一个在text中匹配所有非元音字母的matcher。
当你想检查一个pattern是否完全的匹配一个文本序列的时候创建pattern和matcher对象是令人烦恼的。幸运的是,pattern提供了一个方便的方法完成这个任务;public static boolean matches(string regex, charsequence text)。当且仅当整个字符序列匹配regex的pattern的场合下静态方法返回布尔值true。例如:system.out.println (pattern.matches ("[a-z[//s]]*", "all lowercase letters and whitespace only"));返回布尔值true, 指出仅空格字符和小写字符在all lowercase letters and whitespace only中出现。
public string [] split(charsequence text, int limit): 分割在当前的pattern对象的pattern匹配周围的text。这个方法返回一个数组,它的每一个条目指定了一个从下一个由pattern匹配(或者文本结束)分开的字符序列;且所有条目以它们在text中出现相同的顺序存储。书组条目的数量依赖于limit,它同时也控制了匹配发生次数。一个正数意味着,至多,limit-1 个匹配被考虑且数组的长度不大于限定的条目数。一个负值以为着所有匹配的可能都被考虑且数组可以任意长。一个0值以为着所有可能匹配的条目都被考虑,数组可以有任意的长度,且尾部的空字符串被丢弃。 public string [] split(charsequence text): 用0作为限制调用前边方法,返回方法调用的结果。 假如你想一个拆分雇员记录,包含了姓名,年龄,街道和工资,为它的组成部分。以下的代码用split(charsequence text)方法完成了这个任务:
pattern p = pattern.compile (",//s"); string [] fields = p.split ("john doe, 47, hillsboro road, 32000"); for (int i = 0; i < fields.length; i++) system.out.println (fields [i]);
the code fragment above specifies a regex that matches a comma character immediately followed by a single-space character and produces the following output:
john doe 47 hillsboro road 32000
note
string合并了三个方便的方法调用它们等价的pattern方法: public boolean matches(string regex), public string [] split(string regex), and public string [] split(string regex, int limit)。
public int groupcount(): 返回在matcher的pattern中capturing groups 的个数。这个计数没有包含特定的capturing group 数字 0,它捕获前一个match(不管一个pattern包含capturing groups与否。) public string group(): 通过capturing group 数字 0记录返回上一个match的字符。此方法可以根据一个空的字符串返回一个空字符串。假如match还没有被尝试或者上次的match操作失败将抛出illegalstateexception异常。 public string group(int group): 像上一个方法,除了通过group指定的capturing group number返回以前的match字符外。假如没有group number 指定的capturing group在pattern中存在,此方法抛出 一个indexoutofboundsexception 异常。 以下代码示范了the capturing group 方法:
pattern p = pattern.compile ("(.(.(.)))"); matcher m = p.matcher ("abc"); m.find ();
system.out.println (m.groupcount ()); for (int i = 0; i <= m.groupcount (); i++) system.out.println (i + ": " + m.group (i));
the example produces the following output:
3 0: abc 1: abc 2: bc 3: c
capturing group 数字0 保存了previous match 且与has nothing to do with whether 一个capturing group在一个pattern中出现与否没有任何关系。也就是 is (.(.(.)))。其它的三个capturing groups捕获了previous match属于这个capturing groups的字符。例如,number 2, (.(.)), 捕获 bc; and number 3, (.), 捕获 c.
在我们离开讨论matcher的方法之前,我们将examine四个match位置方法:
public int start(): 返回previous match的开始位置。假如match还没有被执行或者上次的match失败,此方法抛出一个illegalstateexception异常。 public int start(int group): 类似上一个方法,除了返回group指定的capturing group 的相关的previous match 的开始索引外,假如在pattern中没有指定的capturing group number 存在,start(int group) 抛出indexoutofboundsexception 异常。 public int end(): 返回上次match中匹配的字符的索引位置加上1。假如match还没有被尝试或者上次的match操作失败将抛出illegalstateexception异常。 public int end(int group): 类似上一个方法,除了返回group指定的capturing group 的相关的previous match 的end索引外。假如在pattern中没有指定的capturing group number 存在,end(int group) 抛出indexoutofboundsexception 异常。 下边的示例示范了两个match position 方法,为capturing group number 2报告起始/结束match 位置:
pattern p = pattern.compile ("(.(.(.)))"); matcher m = p.matcher ("abcabcabc");
while (m.find ()) { system.out.println ("found " + m.group (2)); system.out.println (" starting at index " + m.start (2) + " and ending at index " + m.end (2)); system.out.println (); }
the example produces the following output:
found bc starting at index 1 and ending at index 3
found bc starting at index 4 and ending at index 6
found bc starting at index 7 and ending at index 9
输出show我们仅仅对与capturing group number 2相关的matcher感兴趣,也就是这些匹配的起始结束位置。
public string getdescription(): 返回语法错误描述。 public int getindex(): 返回语法错误发生位置的近似索引或-1,假如index是未知的。 public string getmessage(): 建立一个多行的,包含了其它三个方法返回的信息的综合,以可视的方式指出在pattern中错误的位置字符串。 public string getpattern(): 返回不正确的正则表达式。 因为patternsyntaxexception 从java.lang.runtimeexception继承而来,代码不需要指定错误handler。this proves appropriate when regexes are known to have correct patterns。但当有潜在的pattern语法错误存在的时候,一个异常handler是必需的。因而,regexdemo的源代码(参看 listing 1) 包含了try { ... } catch (parsesyntaxexception e) { ... },它们调用了patternsyntaxexception四个异常方法中的每一个来获得非法pattern的信息。
什么组成了非法的pattern?在embedded flag expression 中没有指定结束的元字符结束符号就是一个例。假如你执行java regexdemo (?itree treehouse。此命令的非法正则表达式(?tree pattern 导致 p = pattern.compile (args [0]); 抛出patternsyntaxexception 异常。你将看到如下输出:
public patternsyntaxexception(string desc, string regex, int index) 构造函数让你创建你自己的patternsyntaxexception对象, that constructor comes in handy should you ever create your own preprocessing compilation method that recognizes your own pattern syntax, translates that syntax to syntax recognized by pattern's compilation methods, and calls one of those compilation methods. if your method's caller violates your custom pattern syntax, you can throw an appropriate patternsyntaxexception object from that method。
一个正则表达式应用实践
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
// extcmnt.java
import java.io.*; import java.util.regex.*;
class extcmnt { public static void main (string [] args) { if (args.length != 2) { system.err.println ("usage: java extcmnt infile outfile"); return; }
pattern p; try { // 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.
// 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.
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.
resources
download this article's source code and resource files: http://www.javaworld.com/javaworld/jw-02-2003/java101/jw-0207-java101.zip for a glossary specific to this article, homework, and more, see the java 101 study guide that accompanies this article: http://www.javaworld.com/javaworld/jw-02-2003/jw-0207-java101guide.html "magic with merlin: parse sequences of characters with the new regex library," john zukowski (ibm developerworks, august 2002) explores java.util.regex's support for pattern matching and presents a complete example that finds the longest word in a text file: http://www-106.ibm.com/developerworks/java/library/j-mer0827/ "matchmaking with regular expressions," benedict chng (javaworld, july 2001) explores regexes in the context of apache's jakarta oro library: http://www.javaworld.com/javaworld/jw-07-2001/jw-0713-regex.html "regular expressions and the java programming language," dana nourie and mike mccloskey (sun microsystems, august 2002) presents a brief overview of java.util.regex, including five illustrative regex-based applications: http://developer.java.sun.com/developer/technicalarticles/releases/1.4regex/ in "the java platform" (onjava.com), an excerpt from chapter 4 of o'reilly's java in a nutshell, 4th edition, david flanagan presents short examples of charsequence and java.util.regex methods: http://www.onjava.com/pub/a/onjava/excerpt/javanut4_ch04 the java tutorial's "regular expressions" lesson teaches the basics of sun's java.util.regex package: http://java.sun.com/docs/books/tutorial/extra/regex/index.html wikipedia defines some regex terminology, presents a brief history of regexes, and explores various regex syntaxes: http://www.wikipedia.org/wiki/regular_expression read jeff's previous java 101 column: "tools of the trade, part 3" (javaworld, january 2003): http://www.javaworld.com/javaworld/jw-01-2003/jw-0103-java101.html? check out past java 101 articles: http://www.javaworld.com/javaworld/topicalindex/jw-ti-java101.html browse the core java section of javaworld's topical index: http://www.javaworld.com/channel_content/jw-core-index.shtml need some java help? visit our java beginner discussion: http://forums.devworld.com/[email protected]@.ee6b804 java experts answer your toughest java questions in javaworld's java q&a column: http://www.javaworld.com/javaworld/javaqa/javaqa-index.html for tips 'n tricks, see: http://www.javaworld.com/javaworld/javatips/jw-javatips.index.html sign up for javaworld's free weekly core java email newsletter: http://www.javaworld.com/subscribe you'll find a wealth of it-related articles from our sister publications at idg.net