KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > regex > Pattern


1 /*
2  * @(#)Pattern.java 1.113 07/05/07
3  *
4  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.regex;
9
10 import java.security.AccessController JavaDoc;
11 import java.security.PrivilegedAction JavaDoc;
12 import java.text.CharacterIterator JavaDoc;
13 import sun.text.Normalizer;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16
17
18 /**
19  * A compiled representation of a regular expression.
20  *
21  * <p> A regular expression, specified as a string, must first be compiled into
22  * an instance of this class. The resulting pattern can then be used to create
23  * a {@link Matcher} object that can match arbitrary {@link
24  * java.lang.CharSequence </code>character sequences<code>} against the regular
25  * expression. All of the state involved in performing a match resides in the
26  * matcher, so many matchers can share the same pattern.
27  *
28  * <p> A typical invocation sequence is thus
29  *
30  * <blockquote><pre>
31  * Pattern p = Pattern.{@link #compile compile}("a*b");
32  * Matcher m = p.{@link #matcher matcher}("aaaaab");
33  * boolean b = m.{@link Matcher#matches matches}();</pre></blockquote>
34  *
35  * <p> A {@link #matches matches} method is defined by this class as a
36  * convenience for when a regular expression is used just once. This method
37  * compiles an expression and matches an input sequence against it in a single
38  * invocation. The statement
39  *
40  * <blockquote><pre>
41  * boolean b = Pattern.matches("a*b", "aaaaab");</pre></blockquote>
42  *
43  * is equivalent to the three statements above, though for repeated matches it
44  * is less efficient since it does not allow the compiled pattern to be reused.
45  *
46  * <p> Instances of this class are immutable and are safe for use by multiple
47  * concurrent threads. Instances of the {@link Matcher} class are not safe for
48  * such use.
49  *
50  *
51  * <a name="sum">
52  * <h4> Summary of regular-expression constructs </h4>
53  *
54  * <table border="0" cellpadding="1" cellspacing="0"
55  * summary="Regular expression constructs, and what they match">
56  *
57  * <tr align="left">
58  * <th bgcolor="#CCCCFF" align="left" id="construct">Construct</th>
59  * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
60  * </tr>
61  *
62  * <tr><th>&nbsp;</th></tr>
63  * <tr align="left"><th colspan="2" id="characters">Characters</th></tr>
64  *
65  * <tr><td valign="top" headers="construct characters"><i>x</i></td>
66  * <td headers="matches">The character <i>x</i></td></tr>
67  * <tr><td valign="top" headers="construct characters"><tt>\\</tt></td>
68  * <td headers="matches">The backslash character</td></tr>
69  * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>n</i></td>
70  * <td headers="matches">The character with octal value <tt>0</tt><i>n</i>
71  * (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
72  * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>nn</i></td>
73  * <td headers="matches">The character with octal value <tt>0</tt><i>nn</i>
74  * (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
75  * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>mnn</i></td>
76  * <td headers="matches">The character with octal value <tt>0</tt><i>mnn</i>
77  * (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>m</i>&nbsp;<tt>&lt;=</tt>&nbsp;3,
78  * 0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
79  * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>hh</i></td>
80  * <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>hh</i></td></tr>
81  * <tr><td valign="top" headers="construct characters"><tt>&#92;u</tt><i>hhhh</i></td>
82  * <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>hhhh</i></td></tr>
83  * <tr><td valign="top" headers="matches"><tt>\t</tt></td>
84  * <td headers="matches">The tab character (<tt>'&#92;u0009'</tt>)</td></tr>
85  * <tr><td valign="top" headers="construct characters"><tt>\n</tt></td>
86  * <td headers="matches">The newline (line feed) character (<tt>'&#92;u000A'</tt>)</td></tr>
87  * <tr><td valign="top" headers="construct characters"><tt>\r</tt></td>
88  * <td headers="matches">The carriage-return character (<tt>'&#92;u000D'</tt>)</td></tr>
89  * <tr><td valign="top" headers="construct characters"><tt>\f</tt></td>
90  * <td headers="matches">The form-feed character (<tt>'&#92;u000C'</tt>)</td></tr>
91  * <tr><td valign="top" headers="construct characters"><tt>\a</tt></td>
92  * <td headers="matches">The alert (bell) character (<tt>'&#92;u0007'</tt>)</td></tr>
93  * <tr><td valign="top" headers="construct characters"><tt>\e</tt></td>
94  * <td headers="matches">The escape character (<tt>'&#92;u001B'</tt>)</td></tr>
95  * <tr><td valign="top" headers="construct characters"><tt>\c</tt><i>x</i></td>
96  * <td headers="matches">The control character corresponding to <i>x</i></td></tr>
97  *
98  * <tr><th>&nbsp;</th></tr>
99  * <tr align="left"><th colspan="2" id="classes">Character classes</th></tr>
100  *
101  * <tr><td valign="top" headers="construct classes"><tt>[abc]</tt></td>
102  * <td headers="matches"><tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (simple class)</td></tr>
103  * <tr><td valign="top" headers="construct classes"><tt>[^abc]</tt></td>
104  * <td headers="matches">Any character except <tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (negation)</td></tr>
105  * <tr><td valign="top" headers="construct classes"><tt>[a-zA-Z]</tt></td>
106  * <td headers="matches"><tt>a</tt> through <tt>z</tt>
107  * or <tt>A</tt> through <tt>Z</tt>, inclusive (range)</td></tr>
108  * <tr><td valign="top" headers="construct classes"><tt>[a-d[m-p]]</tt></td>
109  * <td headers="matches"><tt>a</tt> through <tt>d</tt>,
110  * or <tt>m</tt> through <tt>p</tt>: <tt>[a-dm-p]</tt> (union)</td></tr>
111  * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[def]]</tt></td>
112  * <td headers="matches"><tt>d</tt>, <tt>e</tt>, or <tt>f</tt> (intersection)</tr>
113  * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^bc]]</tt></td>
114  * <td headers="matches"><tt>a</tt> through <tt>z</tt>,
115  * except for <tt>b</tt> and <tt>c</tt>: <tt>[ad-z]</tt> (subtraction)</td></tr>
116  * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^m-p]]</tt></td>
117  * <td headers="matches"><tt>a</tt> through <tt>z</tt>,
118  * and not <tt>m</tt> through <tt>p</tt>: <tt>[a-lq-z]</tt>(subtraction)</td></tr>
119  * <tr><th>&nbsp;</th></tr>
120  *
121  * <tr align="left"><th colspan="2" id="predef">Predefined character classes</th></tr>
122  *
123  * <tr><td valign="top" headers="construct predef"><tt>.</tt></td>
124  * <td headers="matches">Any character (may or may not match <a HREF="#lt">line terminators</a>)</td></tr>
125  * <tr><td valign="top" headers="construct predef"><tt>\d</tt></td>
126  * <td headers="matches">A digit: <tt>[0-9]</tt></td></tr>
127  * <tr><td valign="top" headers="construct predef"><tt>\D</tt></td>
128  * <td headers="matches">A non-digit: <tt>[^0-9]</tt></td></tr>
129  * <tr><td valign="top" headers="construct predef"><tt>\s</tt></td>
130  * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
131  * <tr><td valign="top" headers="construct predef"><tt>\S</tt></td>
132  * <td headers="matches">A non-whitespace character: <tt>[^\s]</tt></td></tr>
133  * <tr><td valign="top" headers="construct predef"><tt>\w</tt></td>
134  * <td headers="matches">A word character: <tt>[a-zA-Z_0-9]</tt></td></tr>
135  * <tr><td valign="top" headers="construct predef"><tt>\W</tt></td>
136  * <td headers="matches">A non-word character: <tt>[^\w]</tt></td></tr>
137  *
138  * <tr><th>&nbsp;</th></tr>
139  * <tr align="left"><th colspan="2" id="posix">POSIX character classes</b> (US-ASCII only)<b></th></tr>
140  *
141  * <tr><td valign="top" headers="construct posix"><tt>\p{Lower}</tt></td>
142  * <td headers="matches">A lower-case alphabetic character: <tt>[a-z]</tt></td></tr>
143  * <tr><td valign="top" headers="construct posix"><tt>\p{Upper}</tt></td>
144  * <td headers="matches">An upper-case alphabetic character:<tt>[A-Z]</tt></td></tr>
145  * <tr><td valign="top" headers="construct posix"><tt>\p{ASCII}</tt></td>
146  * <td headers="matches">All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
147  * <tr><td valign="top" headers="construct posix"><tt>\p{Alpha}</tt></td>
148  * <td headers="matches">An alphabetic character:<tt>[\p{Lower}\p{Upper}]</tt></td></tr>
149  * <tr><td valign="top" headers="construct posix"><tt>\p{Digit}</tt></td>
150  * <td headers="matches">A decimal digit: <tt>[0-9]</tt></td></tr>
151  * <tr><td valign="top" headers="construct posix"><tt>\p{Alnum}</tt></td>
152  * <td headers="matches">An alphanumeric character:<tt>[\p{Alpha}\p{Digit}]</tt></td></tr>
153  * <tr><td valign="top" headers="construct posix"><tt>\p{Punct}</tt></td>
154  * <td headers="matches">Punctuation: One of <tt>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~</tt></td></tr>
155  * <!-- <tt>[\!"#\$%&'\(\)\*\+,\-\./:;\<=\>\?@\[\\\]\^_`\{\|\}~]</tt>
156  * <tt>[\X21-\X2F\X31-\X40\X5B-\X60\X7B-\X7E]</tt> -->
157  * <tr><td valign="top" headers="construct posix"><tt>\p{Graph}</tt></td>
158  * <td headers="matches">A visible character: <tt>[\p{Alnum}\p{Punct}]</tt></td></tr>
159  * <tr><td valign="top" headers="construct posix"><tt>\p{Print}</tt></td>
160  * <td headers="matches">A printable character: <tt>[\p{Graph}\x20]</tt></td></tr>
161  * <tr><td valign="top" headers="construct posix"><tt>\p{Blank}</tt></td>
162  * <td headers="matches">A space or a tab: <tt>[ \t]</tt></td></tr>
163  * <tr><td valign="top" headers="construct posix"><tt>\p{Cntrl}</tt></td>
164  * <td headers="matches">A control character: <tt>[\x00-\x1F\x7F]</td></tr>
165  * <tr><td valign="top" headers="construct posix"><tt>\p{XDigit}</tt></td>
166  * <td headers="matches">A hexadecimal digit: <tt>[0-9a-fA-F]</tt></td></tr>
167  * <tr><td valign="top" headers="construct posix"><tt>\p{Space}</tt></td>
168  * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
169  *
170  * <tr><th>&nbsp;</th></tr>
171  * <tr align="left"><th colspan="2">java.lang.Character classes (simple <a HREF="#jcc">java character type</a>)</th></tr>
172  *
173  * <tr><td valign="top"><tt>\p{javaLowerCase}</tt></td>
174  * <td>Equivalent to java.lang.Character.isLowerCase()</td></tr>
175  * <tr><td valign="top"><tt>\p{javaUpperCase}</tt></td>
176  * <td>Equivalent to java.lang.Character.isUpperCase()</td></tr>
177  * <tr><td valign="top"><tt>\p{javaWhitespace}</tt></td>
178  * <td>Equivalent to java.lang.Character.isWhitespace()</td></tr>
179  * <tr><td valign="top"><tt>\p{javaMirrored}</tt></td>
180  * <td>Equivalent to java.lang.Character.isMirrored()</td></tr>
181  *
182  * <tr><th>&nbsp;</th></tr>
183  * <tr align="left"><th colspan="2" id="unicode">Classes for Unicode blocks and categories</th></tr>
184  *
185  * <tr><td valign="top" headers="construct unicode"><tt>\p{InGreek}</tt></td>
186  * <td headers="matches">A character in the Greek&nbsp;block (simple <a HREF="#ubc">block</a>)</td></tr>
187  * <tr><td valign="top" headers="construct unicode"><tt>\p{Lu}</tt></td>
188  * <td headers="matches">An uppercase letter (simple <a HREF="#ubc">category</a>)</td></tr>
189  * <tr><td valign="top" headers="construct unicode"><tt>\p{Sc}</tt></td>
190  * <td headers="matches">A currency symbol</td></tr>
191  * <tr><td valign="top" headers="construct unicode"><tt>\P{InGreek}</tt></td>
192  * <td headers="matches">Any character except one in the Greek block (negation)</td></tr>
193  * <tr><td valign="top" headers="construct unicode"><tt>[\p{L}&&[^\p{Lu}]]&nbsp;</tt></td>
194  * <td headers="matches">Any letter except an uppercase letter (subtraction)</td></tr>
195  *
196  * <tr><th>&nbsp;</th></tr>
197  * <tr align="left"><th colspan="2" id="bounds">Boundary matchers</th></tr>
198  *
199  * <tr><td valign="top" headers="construct bounds"><tt>^</tt></td>
200  * <td headers="matches">The beginning of a line</td></tr>
201  * <tr><td valign="top" headers="construct bounds"><tt>$</tt></td>
202  * <td headers="matches">The end of a line</td></tr>
203  * <tr><td valign="top" headers="construct bounds"><tt>\b</tt></td>
204  * <td headers="matches">A word boundary</td></tr>
205  * <tr><td valign="top" headers="construct bounds"><tt>\B</tt></td>
206  * <td headers="matches">A non-word boundary</td></tr>
207  * <tr><td valign="top" headers="construct bounds"><tt>\A</tt></td>
208  * <td headers="matches">The beginning of the input</td></tr>
209  * <tr><td valign="top" headers="construct bounds"><tt>\G</tt></td>
210  * <td headers="matches">The end of the previous match</td></tr>
211  * <tr><td valign="top" headers="construct bounds"><tt>\Z</tt></td>
212  * <td headers="matches">The end of the input but for the final
213  * <a HREF="#lt">terminator</a>, if&nbsp;any</td></tr>
214  * <tr><td valign="top" headers="construct bounds"><tt>\z</tt></td>
215  * <td headers="matches">The end of the input</td></tr>
216  *
217  * <tr><th>&nbsp;</th></tr>
218  * <tr align="left"><th colspan="2" id="greedy">Greedy quantifiers</th></tr>
219  *
220  * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>?</tt></td>
221  * <td headers="matches"><i>X</i>, once or not at all</td></tr>
222  * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>*</tt></td>
223  * <td headers="matches"><i>X</i>, zero or more times</td></tr>
224  * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>+</tt></td>
225  * <td headers="matches"><i>X</i>, one or more times</td></tr>
226  * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>}</tt></td>
227  * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
228  * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,}</tt></td>
229  * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
230  * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}</tt></td>
231  * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
232  *
233  * <tr><th>&nbsp;</th></tr>
234  * <tr align="left"><th colspan="2" id="reluc">Reluctant quantifiers</th></tr>
235  *
236  * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>??</tt></td>
237  * <td headers="matches"><i>X</i>, once or not at all</td></tr>
238  * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>*?</tt></td>
239  * <td headers="matches"><i>X</i>, zero or more times</td></tr>
240  * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>+?</tt></td>
241  * <td headers="matches"><i>X</i>, one or more times</td></tr>
242  * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>}?</tt></td>
243  * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
244  * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,}?</tt></td>
245  * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
246  * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}?</tt></td>
247  * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
248  *
249  * <tr><th>&nbsp;</th></tr>
250  * <tr align="left"><th colspan="2" id="poss">Possessive quantifiers</th></tr>
251  *
252  * <tr><td valign="top" headers="construct poss"><i>X</i><tt>?+</tt></td>
253  * <td headers="matches"><i>X</i>, once or not at all</td></tr>
254  * <tr><td valign="top" headers="construct poss"><i>X</i><tt>*+</tt></td>
255  * <td headers="matches"><i>X</i>, zero or more times</td></tr>
256  * <tr><td valign="top" headers="construct poss"><i>X</i><tt>++</tt></td>
257  * <td headers="matches"><i>X</i>, one or more times</td></tr>
258  * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>}+</tt></td>
259  * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
260  * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,}+</tt></td>
261  * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
262  * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}+</tt></td>
263  * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
264  *
265  * <tr><th>&nbsp;</th></tr>
266  * <tr align="left"><th colspan="2" id="logical">Logical operators</th></tr>
267  *
268  * <tr><td valign="top" headers="construct logical"><i>XY</i></td>
269  * <td headers="matches"><i>X</i> followed by <i>Y</i></td></tr>
270  * <tr><td valign="top" headers="construct logical"><i>X</i><tt>|</tt><i>Y</i></td>
271  * <td headers="matches">Either <i>X</i> or <i>Y</i></td></tr>
272  * <tr><td valign="top" headers="construct logical"><tt>(</tt><i>X</i><tt>)</tt></td>
273  * <td headers="matches">X, as a <a HREF="#cg">capturing group</a></td></tr>
274  *
275  * <tr><th>&nbsp;</th></tr>
276  * <tr align="left"><th colspan="2" id="backref">Back references</th></tr>
277  *
278  * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>n</i></td>
279  * <td valign="bottom" headers="matches">Whatever the <i>n</i><sup>th</sup>
280  * <a HREF="#cg">capturing group</a> matched</td></tr>
281  *
282  * <tr><th>&nbsp;</th></tr>
283  * <tr align="left"><th colspan="2" id="quot">Quotation</th></tr>
284  *
285  * <tr><td valign="top" headers="construct quot"><tt>\</tt></td>
286  * <td headers="matches">Nothing, but quotes the following character</tt></td></tr>
287  * <tr><td valign="top" headers="construct quot"><tt>\Q</tt></td>
288  * <td headers="matches">Nothing, but quotes all characters until <tt>\E</tt></td></tr>
289  * <tr><td valign="top" headers="construct quot"><tt>\E</tt></td>
290  * <td headers="matches">Nothing, but ends quoting started by <tt>\Q</tt></td></tr>
291  * <!-- Metachars: !$()*+.<>?[\]^{|} -->
292  *
293  * <tr><th>&nbsp;</th></tr>
294  * <tr align="left"><th colspan="2" id="special">Special constructs (non-capturing)</th></tr>
295  *
296  * <tr><td valign="top" headers="construct special"><tt>(?:</tt><i>X</i><tt>)</tt></td>
297  * <td headers="matches"><i>X</i>, as a non-capturing group</td></tr>
298  * <tr><td valign="top" headers="construct special"><tt>(?idmsux-idmsux)&nbsp;</tt></td>
299  * <td headers="matches">Nothing, but turns match flags on - off</td></tr>
300  * <tr><td valign="top" headers="construct special"><tt>(?idmsux-idmsux:</tt><i>X</i><tt>)</tt>&nbsp;&nbsp;</td>
301  * <td headers="matches"><i>X</i>, as a <a HREF="#cg">non-capturing group</a> with the
302  * given flags on - off</td></tr>
303  * <tr><td valign="top" headers="construct special"><tt>(?=</tt><i>X</i><tt>)</tt></td>
304  * <td headers="matches"><i>X</i>, via zero-width positive lookahead</td></tr>
305  * <tr><td valign="top" headers="construct special"><tt>(?!</tt><i>X</i><tt>)</tt></td>
306  * <td headers="matches"><i>X</i>, via zero-width negative lookahead</td></tr>
307  * <tr><td valign="top" headers="construct special"><tt>(?&lt;=</tt><i>X</i><tt>)</tt></td>
308  * <td headers="matches"><i>X</i>, via zero-width positive lookbehind</td></tr>
309  * <tr><td valign="top" headers="construct special"><tt>(?&lt;!</tt><i>X</i><tt>)</tt></td>
310  * <td headers="matches"><i>X</i>, via zero-width negative lookbehind</td></tr>
311  * <tr><td valign="top" headers="construct special"><tt>(?&gt;</tt><i>X</i><tt>)</tt></td>
312  * <td headers="matches"><i>X</i>, as an independent, non-capturing group</td></tr>
313  *
314  * </table>
315  *
316  * <hr>
317  *
318  *
319  * <a name="bs">
320  * <h4> Backslashes, escapes, and quoting </h4>
321  *
322  * <p> The backslash character (<tt>'\'</tt>) serves to introduce escaped
323  * constructs, as defined in the table above, as well as to quote characters
324  * that otherwise would be interpreted as unescaped constructs. Thus the
325  * expression <tt>\\</tt> matches a single backslash and <tt>\{</tt> matches a
326  * left brace.
327  *
328  * <p> It is an error to use a backslash prior to any alphabetic character that
329  * does not denote an escaped construct; these are reserved for future
330  * extensions to the regular-expression language. A backslash may be used
331  * prior to a non-alphabetic character regardless of whether that character is
332  * part of an unescaped construct.
333  *
334  * <p> Backslashes within string literals in Java source code are interpreted
335  * as required by the <a
336  * HREF="http://java.sun.com/docs/books/jls/second_edition/html/">Java Language
337  * Specification</a> as either <a
338  * HREF="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#100850">Unicode
339  * escapes</a> or other <a
340  * HREF="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101089">character
341  * escapes</a>. It is therefore necessary to double backslashes in string
342  * literals that represent regular expressions to protect them from
343  * interpretation by the Java bytecode compiler. The string literal
344  * <tt>"&#92;b"</tt>, for example, matches a single backspace character when
345  * interpreted as a regular expression, while <tt>"&#92;&#92;b"</tt> matches a
346  * word boundary. The string literal <tt>"&#92;(hello&#92;)"</tt> is illegal
347  * and leads to a compile-time error; in order to match the string
348  * <tt>(hello)</tt> the string literal <tt>"&#92;&#92;(hello&#92;&#92;)"</tt>
349  * must be used.
350  *
351  * <a name="cc">
352  * <h4> Character Classes </h4>
353  *
354  * <p> Character classes may appear within other character classes, and
355  * may be composed by the union operator (implicit) and the intersection
356  * operator (<tt>&amp;&amp;</tt>).
357  * The union operator denotes a class that contains every character that is
358  * in at least one of its operand classes. The intersection operator
359  * denotes a class that contains every character that is in both of its
360  * operand classes.
361  *
362  * <p> The precedence of character-class operators is as follows, from
363  * highest to lowest:
364  *
365  * <blockquote><table border="0" cellpadding="1" cellspacing="0"
366  * summary="Precedence of character class operators.">
367  * <tr><th>1&nbsp;&nbsp;&nbsp;&nbsp;</th>
368  * <td>Literal escape&nbsp;&nbsp;&nbsp;&nbsp;</td>
369  * <td><tt>\x</tt></td></tr>
370  * <tr><th>2&nbsp;&nbsp;&nbsp;&nbsp;</th>
371  * <td>Grouping</td>
372  * <td><tt>[...]</tt></td></tr>
373  * <tr><th>3&nbsp;&nbsp;&nbsp;&nbsp;</th>
374  * <td>Range</td>
375  * <td><tt>a-z</tt></td></tr>
376  * <tr><th>4&nbsp;&nbsp;&nbsp;&nbsp;</th>
377  * <td>Union</td>
378  * <td><tt>[a-e][i-u]<tt></td></tr>
379  * <tr><th>5&nbsp;&nbsp;&nbsp;&nbsp;</th>
380  * <td>Intersection</td>
381  * <td><tt>[a-z&&[aeiou]]</tt></td></tr>
382  * </table></blockquote>
383  *
384  * <p> Note that a different set of metacharacters are in effect inside
385  * a character class than outside a character class. For instance, the
386  * regular expression <tt>.</tt> loses its special meaning inside a
387  * character class, while the expression <tt>-</tt> becomes a range
388  * forming metacharacter.
389  *
390  * <a name="lt">
391  * <h4> Line terminators </h4>
392  *
393  * <p> A <i>line terminator</i> is a one- or two-character sequence that marks
394  * the end of a line of the input character sequence. The following are
395  * recognized as line terminators:
396  *
397  * <ul>
398  *
399  * <li> A newline (line feed) character&nbsp;(<tt>'\n'</tt>),
400  *
401  * <li> A carriage-return character followed immediately by a newline
402  * character&nbsp;(<tt>"\r\n"</tt>),
403  *
404  * <li> A standalone carriage-return character&nbsp;(<tt>'\r'</tt>),
405  *
406  * <li> A next-line character&nbsp;(<tt>'&#92;u0085'</tt>),
407  *
408  * <li> A line-separator character&nbsp;(<tt>'&#92;u2028'</tt>), or
409  *
410  * <li> A paragraph-separator character&nbsp;(<tt>'&#92;u2029</tt>).
411  *
412  * </ul>
413  * <p>If {@link #UNIX_LINES} mode is activated, then the only line terminators
414  * recognized are newline characters.
415  *
416  * <p> The regular expression <tt>.</tt> matches any character except a line
417  * terminator unless the {@link #DOTALL} flag is specified.
418  *
419  * <p> By default, the regular expressions <tt>^</tt> and <tt>$</tt> ignore
420  * line terminators and only match at the beginning and the end, respectively,
421  * of the entire input sequence. If {@link #MULTILINE} mode is activated then
422  * <tt>^</tt> matches at the beginning of input and after any line terminator
423  * except at the end of input. When in {@link #MULTILINE} mode <tt>$</tt>
424  * matches just before a line terminator or the end of the input sequence.
425  *
426  * <a name="cg">
427  * <h4> Groups and capturing </h4>
428  *
429  * <p> Capturing groups are numbered by counting their opening parentheses from
430  * left to right. In the expression <tt>((A)(B(C)))</tt>, for example, there
431  * are four such groups: </p>
432  *
433  * <blockquote><table cellpadding=1 cellspacing=0 summary="Capturing group numberings">
434  * <tr><th>1&nbsp;&nbsp;&nbsp;&nbsp;</th>
435  * <td><tt>((A)(B(C)))</tt></td></tr>
436  * <tr><th>2&nbsp;&nbsp;&nbsp;&nbsp;</th>
437  * <td><tt>(A)</tt></td></tr>
438  * <tr><th>3&nbsp;&nbsp;&nbsp;&nbsp;</th>
439  * <td><tt>(B(C))</tt></td></tr>
440  * <tr><th>4&nbsp;&nbsp;&nbsp;&nbsp;</th>
441  * <td><tt>(C)</tt></td></tr>
442  * </table></blockquote>
443  *
444  * <p> Group zero always stands for the entire expression.
445  *
446  * <p> Capturing groups are so named because, during a match, each subsequence
447  * of the input sequence that matches such a group is saved. The captured
448  * subsequence may be used later in the expression, via a back reference, and
449  * may also be retrieved from the matcher once the match operation is complete.
450  *
451  * <p> The captured input associated with a group is always the subsequence
452  * that the group most recently matched. If a group is evaluated a second time
453  * because of quantification then its previously-captured value, if any, will
454  * be retained if the second evaluation fails. Matching the string
455  * <tt>"aba"</tt> against the expression <tt>(a(b)?)+</tt>, for example, leaves
456  * group two set to <tt>"b"</tt>. All captured input is discarded at the
457  * beginning of each match.
458  *
459  * <p> Groups beginning with <tt>(?</tt> are pure, <i>non-capturing</i> groups
460  * that do not capture text and do not count towards the group total.
461  *
462  *
463  * <h4> Unicode support </h4>
464  *
465  * <p> This class is in conformance with Level 1 of <a
466  * HREF="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
467  * Standard #18: Unicode Regular Expression Guidelines</i></a>, plus RL2.1
468  * Canonical Equivalents.
469  *
470  * <p> Unicode escape sequences such as <tt>&#92;u2014</tt> in Java source code
471  * are processed as described in <a
472  * HREF="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#100850">§3.3</a>
473  * of the Java Language Specification. Such escape sequences are also
474  * implemented directly by the regular-expression parser so that Unicode
475  * escapes can be used in expressions that are read from files or from the
476  * keyboard. Thus the strings <tt>"&#92;u2014"</tt> and <tt>"\\u2014"</tt>,
477  * while not equal, compile into the same pattern, which matches the character
478  * with hexadecimal value <tt>0x2014</tt>.
479  *
480  * <a name="ubc"> <p>Unicode blocks and categories are written with the
481  * <tt>\p</tt> and <tt>\P</tt> constructs as in
482  * Perl. <tt>\p{</tt><i>prop</i><tt>}</tt> matches if the input has the
483  * property <i>prop</i>, while \P{</tt><i>prop</i><tt>}</tt> does not match if
484  * the input has that property. Blocks are specified with the prefix
485  * <tt>In</tt>, as in <tt>InMongolian</tt>. Categories may be specified with
486  * the optional prefix <tt>Is</tt>: Both <tt>\p{L}</tt> and <tt>\p{IsL}</tt>
487  * denote the category of Unicode letters. Blocks and categories can be used
488  * both inside and outside of a character class.
489  *
490  * <p> The supported categories are those of
491  * <a HREF="http://www.unicode.org/unicode/standard/standard.html">
492  * <i>The Unicode Standard</i></a> in the version specified by the
493  * {@link java.lang.Character Character} class. The category names are those
494  * defined in the Standard, both normative and informative.
495  * The block names supported by <code>Pattern</code> are the valid block names
496  * accepted and defined by
497  * {@link java.lang.Character.UnicodeBlock#forName(String) UnicodeBlock.forName}.
498  *
499  * <a name="jcc"> <p>Categories that behave like the java.lang.Character
500  * boolean is<i>methodname</i> methods (except for the deprecated ones) are
501  * available through the same <tt>\p{</tt><i>prop</i><tt>}</tt> syntax where
502  * the specified property has the name <tt>java<i>methodname</i></tt>.
503  *
504  * <h4> Comparison to Perl 5 </h4>
505  *
506  * <p>The <code>Pattern</code> engine performs traditional NFA-based matching
507  * with ordered alternation as occurs in Perl 5.
508  *
509  * <p> Perl constructs not supported by this class: </p>
510  *
511  * <ul>
512  *
513  * <li><p> The conditional constructs <tt>(?{</tt><i>X</i><tt>})</tt> and
514  * <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>|</tt><i>Y</i><tt>)</tt>,
515  * </p></li>
516  *
517  * <li><p> The embedded code constructs <tt>(?{</tt><i>code</i><tt>})</tt>
518  * and <tt>(??{</tt><i>code</i><tt>})</tt>,</p></li>
519  *
520  * <li><p> The embedded comment syntax <tt>(?#comment)</tt>, and </p></li>
521  *
522  * <li><p> The preprocessing operations <tt>\l</tt> <tt>&#92;u</tt>,
523  * <tt>\L</tt>, and <tt>\U</tt>. </p></li>
524  *
525  * </ul>
526  *
527  * <p> Constructs supported by this class but not by Perl: </p>
528  *
529  * <ul>
530  *
531  * <li><p> Possessive quantifiers, which greedily match as much as they can
532  * and do not back off, even when doing so would allow the overall match to
533  * succeed. </p></li>
534  *
535  * <li><p> Character-class union and intersection as described
536  * <a HREF="#cc">above</a>.</p></li>
537  *
538  * </ul>
539  *
540  * <p> Notable differences from Perl: </p>
541  *
542  * <ul>
543  *
544  * <li><p> In Perl, <tt>\1</tt> through <tt>\9</tt> are always interpreted
545  * as back references; a backslash-escaped number greater than <tt>9</tt> is
546  * treated as a back reference if at least that many subexpressions exist,
547  * otherwise it is interpreted, if possible, as an octal escape. In this
548  * class octal escapes must always begin with a zero. In this class,
549  * <tt>\1</tt> through <tt>\9</tt> are always interpreted as back
550  * references, and a larger number is accepted as a back reference if at
551  * least that many subexpressions exist at that point in the regular
552  * expression, otherwise the parser will drop digits until the number is
553  * smaller or equal to the existing number of groups or it is one digit.
554  * </p></li>
555  *
556  * <li><p> Perl uses the <tt>g</tt> flag to request a match that resumes
557  * where the last match left off. This functionality is provided implicitly
558  * by the {@link Matcher} class: Repeated invocations of the {@link
559  * Matcher#find find} method will resume where the last match left off,
560  * unless the matcher is reset. </p></li>
561  *
562  * <li><p> In Perl, embedded flags at the top level of an expression affect
563  * the whole expression. In this class, embedded flags always take effect
564  * at the point at which they appear, whether they are at the top level or
565  * within a group; in the latter case, flags are restored at the end of the
566  * group just as in Perl. </p></li>
567  *
568  * <li><p> Perl is forgiving about malformed matching constructs, as in the
569  * expression <tt>*a</tt>, as well as dangling brackets, as in the
570  * expression <tt>abc]</tt>, and treats them as literals. This
571  * class also accepts dangling brackets but is strict about dangling
572  * metacharacters like +, ? and *, and will throw a
573  * {@link PatternSyntaxException} if it encounters them. </p></li>
574  *
575  * </ul>
576  *
577  *
578  * <p> For a more precise description of the behavior of regular expression
579  * constructs, please see <a HREF="http://www.oreilly.com/catalog/regex2/">
580  * <i>Mastering Regular Expressions, 2nd Edition</i>, Jeffrey E. F. Friedl,
581  * O'Reilly and Associates, 2002.</a>
582  * </p>
583  *
584  * @see java.lang.String#split(String, int)
585  * @see java.lang.String#split(String)
586  *
587  * @author Mike McCloskey
588  * @author Mark Reinhold
589  * @author JSR-51 Expert Group
590  * @version 1.113, 07/05/07
591  * @since 1.4
592  * @spec JSR-51
593  */

594
595 public final class Pattern
596     implements java.io.Serializable JavaDoc
597 {
598
599     /**
600      * Regular expression modifier values. Instead of being passed as
601      * arguments, they can also be passed as inline modifiers.
602      * For example, the following statements have the same effect.
603      * <pre>
604      * RegExp r1 = RegExp.compile("abc", Pattern.I|Pattern.M);
605      * RegExp r2 = RegExp.compile("(?im)abc", 0);
606      * </pre>
607      *
608      * The flags are duplicated so that the familiar Perl match flag
609      * names are available.
610      */

611
612     /**
613      * Enables Unix lines mode.
614      *
615      * <p> In this mode, only the <tt>'\n'</tt> line terminator is recognized
616      * in the behavior of <tt>.</tt>, <tt>^</tt>, and <tt>$</tt>.
617      *
618      * <p> Unix lines mode can also be enabled via the embedded flag
619      * expression&nbsp;<tt>(?d)</tt>.
620      */

621     public static final int UNIX_LINES = 0x01;
622
623     /**
624      * Enables case-insensitive matching.
625      *
626      * <p> By default, case-insensitive matching assumes that only characters
627      * in the US-ASCII charset are being matched. Unicode-aware
628      * case-insensitive matching can be enabled by specifying the {@link
629      * #UNICODE_CASE} flag in conjunction with this flag.
630      *
631      * <p> Case-insensitive matching can also be enabled via the embedded flag
632      * expression&nbsp;<tt>(?i)</tt>.
633      *
634      * <p> Specifying this flag may impose a slight performance penalty. </p>
635      */

636     public static final int CASE_INSENSITIVE = 0x02;
637
638     /**
639      * Permits whitespace and comments in pattern.
640      *
641      * <p> In this mode, whitespace is ignored, and embedded comments starting
642      * with <tt>#</tt> are ignored until the end of a line.
643      *
644      * <p> Comments mode can also be enabled via the embedded flag
645      * expression&nbsp;<tt>(?x)</tt>.
646      */

647     public static final int COMMENTS = 0x04;
648
649     /**
650      * Enables multiline mode.
651      *
652      * <p> In multiline mode the expressions <tt>^</tt> and <tt>$</tt> match
653      * just after or just before, respectively, a line terminator or the end of
654      * the input sequence. By default these expressions only match at the
655      * beginning and the end of the entire input sequence.
656      *
657      * <p> Multiline mode can also be enabled via the embedded flag
658      * expression&nbsp;<tt>(?m)</tt>. </p>
659      */

660     public static final int MULTILINE = 0x08;
661
662     /**
663      * Enables literal parsing of the pattern.
664      *
665      * <p> When this flag is specified then the input string that specifies
666      * the pattern is treated as a sequence of literal characters.
667      * Metacharacters or escape sequences in the input sequence will be
668      * given no special meaning.
669      *
670      * <p>The flags CASE_INSENSITIVE and UNICODE_CASE retain their impact on
671      * matching when used in conjunction with this flag. The other flags
672      * become superfluous.
673      *
674      * <p> There is no embedded flag character for enabling literal parsing.
675      */

676     public static final int LITERAL = 0x10;
677
678     /**
679      * Enables dotall mode.
680      *
681      * <p> In dotall mode, the expression <tt>.</tt> matches any character,
682      * including a line terminator. By default this expression does not match
683      * line terminators.
684      *
685      * <p> Dotall mode can also be enabled via the embedded flag
686      * expression&nbsp;<tt>(?s)</tt>. (The <tt>s</tt> is a mnemonic for
687      * "single-line" mode, which is what this is called in Perl.) </p>
688      */

689     public static final int DOTALL = 0x20;
690
691     /**
692      * Enables Unicode-aware case folding.
693      *
694      * <p> When this flag is specified then case-insensitive matching, when
695      * enabled by the {@link #CASE_INSENSITIVE} flag, is done in a manner
696      * consistent with the Unicode Standard. By default, case-insensitive
697      * matching assumes that only characters in the US-ASCII charset are being
698      * matched.
699      *
700      * <p> Unicode-aware case folding can also be enabled via the embedded flag
701      * expression&nbsp;<tt>(?u)</tt>.
702      *
703      * <p> Specifying this flag may impose a performance penalty. </p>
704      */

705     public static final int UNICODE_CASE = 0x40;
706
707     /**
708      * Enables canonical equivalence.
709      *
710      * <p> When this flag is specified then two characters will be considered
711      * to match if, and only if, their full canonical decompositions match.
712      * The expression <tt>"a&#92;u030A"</tt>, for example, will match the
713      * string <tt>"å"</tt> when this flag is specified. By default,
714      * matching does not take canonical equivalence into account.
715      *
716      * <p> There is no embedded flag character for enabling canonical
717      * equivalence.
718      *
719      * <p> Specifying this flag may impose a performance penalty. </p>
720      */

721     public static final int CANON_EQ = 0x80;
722
723     /* Pattern has only two serialized components: The pattern string
724      * and the flags, which are all that is needed to recompile the pattern
725      * when it is deserialized.
726      */

727
728     /** use serialVersionUID from Merlin b59 for interoperability */
729     private static final long serialVersionUID = 5073258162644648461L;
730
731     /**
732      * The original regular-expression pattern string.
733      *
734      * @serial
735      */

736     private String JavaDoc pattern;
737
738     /**
739      * The original pattern flags.
740      *
741      * @serial
742      */

743     private int flags;
744
745     /**
746      * Boolean indicating this Pattern is compiled; this is necessary in order
747      * to lazily compile deserialized Patterns.
748      */

749     private transient volatile boolean compiled = false;
750
751     /**
752      * The normalized pattern string.
753      */

754     private transient String JavaDoc normalizedPattern;
755
756     /**
757      * The starting point of state machine for the find operation. This allows
758      * a match to start anywhere in the input.
759      */

760     transient Node root;
761
762     /**
763      * The root of object tree for a match operation. The pattern is matched
764      * at the beginning. This may include a find that uses BnM or a First
765      * node.
766      */

767     transient Node matchRoot;
768
769     /**
770      * Temporary storage used by parsing pattern slice.
771      */

772     transient int[] buffer;
773
774     /**
775      * Temporary storage used while parsing group references.
776      */

777     transient GroupHead[] groupNodes;
778
779     /**
780      * Temporary null terminating char array used by pattern compiling.
781      */

782     private transient int[] temp;
783
784     /**
785      * The number of capturing groups in this Pattern. Used by matchers to
786      * allocate storage needed to perform a match.
787      */

788     transient int capturingGroupCount;
789
790     /**
791      * The local variable count used by parsing tree. Used by matchers to
792      * allocate storage needed to perform a match.
793      */

794     transient int localCount;
795
796     /**
797      * Index into the pattern string that keeps track of how much has been
798      * parsed.
799      */

800     private transient int cursor;
801
802     /**
803      * Holds the length of the pattern string.
804      */

805     private transient int patternLength;
806
807     /**
808      * Compiles the given regular expression into a pattern. </p>
809      *
810      * @param regex
811      * The expression to be compiled
812      *
813      * @throws PatternSyntaxException
814      * If the expression's syntax is invalid
815      */

816     public static Pattern JavaDoc compile(String JavaDoc regex) {
817         return new Pattern JavaDoc(regex, 0);
818     }
819
820     /**
821      * Compiles the given regular expression into a pattern with the given
822      * flags. </p>
823      *
824      * @param regex
825      * The expression to be compiled
826      *
827      * @param flags
828      * Match flags, a bit mask that may include
829      * {@link #CASE_INSENSITIVE}, {@link #MULTILINE}, {@link #DOTALL},
830      * {@link #UNICODE_CASE}, and {@link #CANON_EQ}
831      *
832      * @throws IllegalArgumentException
833      * If bit values other than those corresponding to the defined
834      * match flags are set in <tt>flags</tt>
835      *
836      * @throws PatternSyntaxException
837      * If the expression's syntax is invalid
838      */

839     public static Pattern JavaDoc compile(String JavaDoc regex, int flags) {
840         return new Pattern JavaDoc(regex, flags);
841     }
842
843     /**
844      * Returns the regular expression from which this pattern was compiled.
845      * </p>
846      *
847      * @return The source of this pattern
848      */

849     public String JavaDoc pattern() {
850         return pattern;
851     }
852
853     /**
854      * <p>Returns the string representation of this pattern. This
855      * is the regular expression from which this pattern was
856      * compiled.</p>
857      *
858      * @return The string representation of this pattern
859      * @since 1.5
860      */

861     public String JavaDoc toString() {
862         return pattern;
863     }
864
865     /**
866      * Creates a matcher that will match the given input against this pattern.
867      * </p>
868      *
869      * @param input
870      * The character sequence to be matched
871      *
872      * @return A new matcher for this pattern
873      */

874     public Matcher JavaDoc matcher(CharSequence JavaDoc input) {
875         synchronized(this) {
876             if (!compiled)
877                 compile();
878         }
879         Matcher JavaDoc m = new Matcher JavaDoc(this, input);
880         return m;
881     }
882
883     /**
884      * Returns this pattern's match flags. </p>
885      *
886      * @return The match flags specified when this pattern was compiled
887      */

888     public int flags() {
889         return flags;
890     }
891
892     /**
893      * Compiles the given regular expression and attempts to match the given
894      * input against it.
895      *
896      * <p> An invocation of this convenience method of the form
897      *
898      * <blockquote><pre>
899      * Pattern.matches(regex, input);</pre></blockquote>
900      *
901      * behaves in exactly the same way as the expression
902      *
903      * <blockquote><pre>
904      * Pattern.compile(regex).matcher(input).matches()</pre></blockquote>
905      *
906      * <p> If a pattern is to be used multiple times, compiling it once and reusing
907      * it will be more efficient than invoking this method each time. </p>
908      *
909      * @param regex
910      * The expression to be compiled
911      *
912      * @param input
913      * The character sequence to be matched
914      *
915      * @throws PatternSyntaxException
916      * If the expression's syntax is invalid
917      */

918     public static boolean matches(String JavaDoc regex, CharSequence JavaDoc input) {
919         Pattern JavaDoc p = Pattern.compile(regex);
920         Matcher JavaDoc m = p.matcher(input);
921         return m.matches();
922     }
923
924     /**
925      * Splits the given input sequence around matches of this pattern.
926      *
927      * <p> The array returned by this method contains each substring of the
928      * input sequence that is terminated by another subsequence that matches
929      * this pattern or is terminated by the end of the input sequence. The
930      * substrings in the array are in the order in which they occur in the
931      * input. If this pattern does not match any subsequence of the input then
932      * the resulting array has just one element, namely the input sequence in
933      * string form.
934      *
935      * <p> The <tt>limit</tt> parameter controls the number of times the
936      * pattern is applied and therefore affects the length of the resulting
937      * array. If the limit <i>n</i> is greater than zero then the pattern
938      * will be applied at most <i>n</i>&nbsp;-&nbsp;1 times, the array's
939      * length will be no greater than <i>n</i>, and the array's last entry
940      * will contain all input beyond the last matched delimiter. If <i>n</i>
941      * is non-positive then the pattern will be applied as many times as
942      * possible and the array can have any length. If <i>n</i> is zero then
943      * the pattern will be applied as many times as possible, the array can
944      * have any length, and trailing empty strings will be discarded.
945      *
946      * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
947      * results with these parameters:
948      *
949      * <blockquote><table cellpadding=1 cellspacing=0
950      * summary="Split examples showing regex, limit, and result">
951      * <tr><th><P align="left"><i>Regex&nbsp;&nbsp;&nbsp;&nbsp;</i></th>
952      * <th><P align="left"><i>Limit&nbsp;&nbsp;&nbsp;&nbsp;</i></th>
953      * <th><P align="left"><i>Result&nbsp;&nbsp;&nbsp;&nbsp;</i></th></tr>
954      * <tr><td align=center>:</td>
955      * <td align=center>2</td>
956      * <td><tt>{ "boo", "and:foo" }</tt></td></tr>
957      * <tr><td align=center>:</td>
958      * <td align=center>5</td>
959      * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
960      * <tr><td align=center>:</td>
961      * <td align=center>-2</td>
962      * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
963      * <tr><td align=center>o</td>
964      * <td align=center>5</td>
965      * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
966      * <tr><td align=center>o</td>
967      * <td align=center>-2</td>
968      * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
969      * <tr><td align=center>o</td>
970      * <td align=center>0</td>
971      * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
972      * </table></blockquote>
973      *
974      *
975      * @param input
976      * The character sequence to be split
977      *
978      * @param limit
979      * The result threshold, as described above
980      *
981      * @return The array of strings computed by splitting the input
982      * around matches of this pattern
983      */

984     public String JavaDoc[] split(CharSequence JavaDoc input, int limit) {
985         int index = 0;
986         boolean matchLimited = limit > 0;
987         ArrayList JavaDoc matchList = new ArrayList JavaDoc();
988         Matcher JavaDoc m = matcher(input);
989
990         // Add segments before each match found
991
while(m.find()) {
992             if (!matchLimited || matchList.size() < limit - 1) {
993                 String JavaDoc match = input.subSequence(index, m.start()).toString();
994                 matchList.add(match);
995                 index = m.end();
996             } else if (matchList.size() == limit - 1) { // last one
997
String JavaDoc match = input.subSequence(index,
998                                                  input.length()).toString();
999                 matchList.add(match);
1000                index = m.end();
1001            }
1002        }
1003
1004        // If no match was found, return this
1005
if (index == 0)
1006            return new String JavaDoc[] {input.toString()};
1007
1008        // Add remaining segment
1009
if (!matchLimited || matchList.size() < limit)
1010            matchList.add(input.subSequence(index, input.length()).toString());
1011
1012        // Construct result
1013
int resultSize = matchList.size();
1014  &n