Pattern.java revision 3486
2736N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 2736N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2736N/A * This code is free software; you can redistribute it and/or modify it 2736N/A * under the terms of the GNU General Public License version 2 only, as 2736N/A * published by the Free Software Foundation. Oracle designates this 2736N/A * particular file as subject to the "Classpath" exception as provided 2736N/A * by Oracle in the LICENSE file that accompanied this code. 2736N/A * This code is distributed in the hope that it will be useful, but WITHOUT 2736N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2736N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2736N/A * version 2 for more details (a copy is included in the LICENSE file that 2736N/A * You should have received a copy of the GNU General Public License version 2736N/A * 2 along with this work; if not, write to the Free Software Foundation, 2736N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2736N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2736N/A * or visit www.oracle.com if you need additional information or have any 2736N/A * A compiled representation of a regular expression. 2736N/A * <p> A regular expression, specified as a string, must first be compiled into 2736N/A * an instance of this class. The resulting pattern can then be used to create 2736N/A * a {@link Matcher} object that can match arbitrary {@link 2736N/A * java.lang.CharSequence </code>character sequences<code>} against the regular 2736N/A * expression. All of the state involved in performing a match resides in the 2736N/A * matcher, so many matchers can share the same pattern. 2736N/A * <p> A typical invocation sequence is thus 2736N/A * Pattern p = Pattern.{@link #compile compile}("a*b"); 2736N/A * Matcher m = p.{@link #matcher matcher}("aaaaab"); 2736N/A * boolean b = m.{@link Matcher#matches matches}();</pre></blockquote> 2736N/A * <p> A {@link #matches matches} method is defined by this class as a 2736N/A * convenience for when a regular expression is used just once. This method 2736N/A * compiles an expression and matches an input sequence against it in a single 2736N/A * invocation. The statement 2736N/A * boolean b = Pattern.matches("a*b", "aaaaab");</pre></blockquote> 2736N/A * is equivalent to the three statements above, though for repeated matches it 2736N/A * is less efficient since it does not allow the compiled pattern to be reused. 2736N/A * <p> Instances of this class are immutable and are safe for use by multiple 2736N/A * concurrent threads. Instances of the {@link Matcher} class are not safe for 2736N/A * <h4> Summary of regular-expression constructs </h4> 2736N/A * <table border="0" cellpadding="1" cellspacing="0" 2736N/A * summary="Regular expression constructs, and what they match"> 2736N/A * <th bgcolor="#CCCCFF" align="left" id="construct">Construct</th> 2736N/A * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th> 2736N/A * <tr align="left"><th colspan="2" id="characters">Characters</th></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><i>x</i></td> 2736N/A * <td headers="matches">The character <i>x</i></td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\\</tt></td> 2736N/A * <td headers="matches">The backslash character</td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>n</i></td> 2736N/A * <td headers="matches">The character with octal value <tt>0</tt><i>n</i> 2736N/A * (0 <tt><=</tt> <i>n</i> <tt><=</tt> 7)</td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>nn</i></td> 2736N/A * <td headers="matches">The character with octal value <tt>0</tt><i>nn</i> 2736N/A * (0 <tt><=</tt> <i>n</i> <tt><=</tt> 7)</td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>mnn</i></td> 2736N/A * <td headers="matches">The character with octal value <tt>0</tt><i>mnn</i> 2736N/A * (0 <tt><=</tt> <i>m</i> <tt><=</tt> 3, 2736N/A * 0 <tt><=</tt> <i>n</i> <tt><=</tt> 7)</td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>hh</i></td> 2736N/A * <td headers="matches">The character with hexadecimal value <tt>0x</tt><i>hh</i></td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\u</tt><i>hhhh</i></td> 2736N/A * <td headers="matches">The character with hexadecimal value <tt>0x</tt><i>hhhh</i></td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>{h...h}</i></td> 2736N/A * <td headers="matches">The character with hexadecimal value <tt>0x</tt><i>h...h</i> 2736N/A * ({@link java.lang.Character#MIN_CODE_POINT Character.MIN_CODE_POINT} 2736N/A * <= <tt>0x</tt><i>h...h</i> <=  2736N/A * {@link java.lang.Character#MAX_CODE_POINT Character.MAX_CODE_POINT})</td></tr> 2736N/A * <tr><td valign="top" headers="matches"><tt>\t</tt></td> 2736N/A * <td headers="matches">The tab character (<tt>'\u0009'</tt>)</td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\n</tt></td> 2736N/A * <td headers="matches">The newline (line feed) character (<tt>'\u000A'</tt>)</td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\r</tt></td> 2736N/A * <td headers="matches">The carriage-return character (<tt>'\u000D'</tt>)</td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\f</tt></td> 2736N/A * <td headers="matches">The form-feed character (<tt>'\u000C'</tt>)</td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\a</tt></td> 2736N/A * <td headers="matches">The alert (bell) character (<tt>'\u0007'</tt>)</td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\e</tt></td> 2736N/A * <td headers="matches">The escape character (<tt>'\u001B'</tt>)</td></tr> 2736N/A * <tr><td valign="top" headers="construct characters"><tt>\c</tt><i>x</i></td> 2736N/A * <td headers="matches">The control character corresponding to <i>x</i></td></tr> 2736N/A * <tr align="left"><th colspan="2" id="classes">Character classes</th></tr> 2736N/A * <tr><td valign="top" headers="construct classes"><tt>[abc]</tt></td> 2736N/A * <td headers="matches"><tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (simple class)</td></tr> 2736N/A * <tr><td valign="top" headers="construct classes"><tt>[^abc]</tt></td> 2736N/A * <td headers="matches">Any character except <tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (negation)</td></tr> 2736N/A * <tr><td valign="top" headers="construct classes"><tt>[a-zA-Z]</tt></td> 2736N/A * <td headers="matches"><tt>a</tt> through <tt>z</tt> 2736N/A * or <tt>A</tt> through <tt>Z</tt>, inclusive (range)</td></tr> 2736N/A * <tr><td valign="top" headers="construct classes"><tt>[a-d[m-p]]</tt></td> 2736N/A * <td headers="matches"><tt>a</tt> through <tt>d</tt>, 2736N/A * or <tt>m</tt> through <tt>p</tt>: <tt>[a-dm-p]</tt> (union)</td></tr> 2736N/A * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[def]]</tt></td> 2736N/A * <td headers="matches"><tt>d</tt>, <tt>e</tt>, or <tt>f</tt> (intersection)</tr> 2736N/A * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^bc]]</tt></td> 2736N/A * <td headers="matches"><tt>a</tt> through <tt>z</tt>, 2736N/A * except for <tt>b</tt> and <tt>c</tt>: <tt>[ad-z]</tt> (subtraction)</td></tr> 2736N/A * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^m-p]]</tt></td> 2736N/A * <td headers="matches"><tt>a</tt> through <tt>z</tt>, 2736N/A * and not <tt>m</tt> through <tt>p</tt>: <tt>[a-lq-z]</tt>(subtraction)</td></tr> 2736N/A * <tr align="left"><th colspan="2" id="predef">Predefined character classes</th></tr> 2736N/A * <tr><td valign="top" headers="construct predef"><tt>.</tt></td> 2736N/A * <td headers="matches">Any character (may or may not match <a href="#lt">line terminators</a>)</td></tr> 2736N/A * <tr><td valign="top" headers="construct predef"><tt>\d</tt></td> 2736N/A * <td headers="matches">A digit: <tt>[0-9]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct predef"><tt>\D</tt></td> 2736N/A * <td headers="matches">A non-digit: <tt>[^0-9]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct predef"><tt>\s</tt></td> 2736N/A * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct predef"><tt>\S</tt></td> 2736N/A * <td headers="matches">A non-whitespace character: <tt>[^\s]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct predef"><tt>\w</tt></td> 2736N/A * <td headers="matches">A word character: <tt>[a-zA-Z_0-9]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct predef"><tt>\W</tt></td> 2736N/A * <td headers="matches">A non-word character: <tt>[^\w]</tt></td></tr> 2736N/A * <tr align="left"><th colspan="2" id="posix">POSIX character classes</b> (US-ASCII only)<b></th></tr> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Lower}</tt></td> 2736N/A * <td headers="matches">A lower-case alphabetic character: <tt>[a-z]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Upper}</tt></td> 2736N/A * <td headers="matches">An upper-case alphabetic character:<tt>[A-Z]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{ASCII}</tt></td> 2736N/A * <td headers="matches">All ASCII:<tt>[\x00-\x7F]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Alpha}</tt></td> 2736N/A * <td headers="matches">An alphabetic character:<tt>[\p{Lower}\p{Upper}]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Digit}</tt></td> 2736N/A * <td headers="matches">A decimal digit: <tt>[0-9]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Alnum}</tt></td> 2736N/A * <td headers="matches">An alphanumeric character:<tt>[\p{Alpha}\p{Digit}]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Punct}</tt></td> 2736N/A * <td headers="matches">Punctuation: One of <tt>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~</tt></td></tr> 2736N/A * <!-- <tt>[\!"#\$%&'\(\)\*\+,\-\./:;\<=\>\?@\[\\\]\^_`\{\|\}~]</tt> 2736N/A * <tt>[\X21-\X2F\X31-\X40\X5B-\X60\X7B-\X7E]</tt> --> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Graph}</tt></td> 2736N/A * <td headers="matches">A visible character: <tt>[\p{Alnum}\p{Punct}]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Print}</tt></td> 2736N/A * <td headers="matches">A printable character: <tt>[\p{Graph}\x20]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Blank}</tt></td> 2736N/A * <td headers="matches">A space or a tab: <tt>[ \t]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Cntrl}</tt></td> 2736N/A * <td headers="matches">A control character: <tt>[\x00-\x1F\x7F]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{XDigit}</tt></td> 2736N/A * <td headers="matches">A hexadecimal digit: <tt>[0-9a-fA-F]</tt></td></tr> 2736N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Space}</tt></td> 2736N/A * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr> 2736N/A * <tr align="left"><th colspan="2">java.lang.Character classes (simple <a href="#jcc">java character type</a>)</th></tr> 2736N/A * <tr><td valign="top"><tt>\p{javaLowerCase}</tt></td> 2736N/A * <td>Equivalent to java.lang.Character.isLowerCase()</td></tr> 2736N/A * <tr><td valign="top"><tt>\p{javaUpperCase}</tt></td> * <td>Equivalent to java.lang.Character.isUpperCase()</td></tr> * <tr><td valign="top"><tt>\p{javaWhitespace}</tt></td> * <td>Equivalent to java.lang.Character.isWhitespace()</td></tr> * <tr><td valign="top"><tt>\p{javaMirrored}</tt></td> * <td>Equivalent to java.lang.Character.isMirrored()</td></tr> * <tr><th> </th></tr> * <tr align="left"><th colspan="2" id="unicode">Classes for Unicode scripts, blocks and categories</th></tr> * * <tr><td valign="top" headers="construct unicode"><tt>\p{IsLatin}</tt></td> * <td headers="matches">A Latin script character (simple <a href="#ubc">script</a>)</td></tr> * <tr><td valign="top" headers="construct unicode"><tt>\p{InGreek}</tt></td> * <td headers="matches">A character in the Greek block (simple <a href="#ubc">block</a>)</td></tr> * <tr><td valign="top" headers="construct unicode"><tt>\p{Lu}</tt></td> * <td headers="matches">An uppercase letter (simple <a href="#ubc">category</a>)</td></tr> * <tr><td valign="top" headers="construct unicode"><tt>\p{Sc}</tt></td> * <td headers="matches">A currency symbol</td></tr> * <tr><td valign="top" headers="construct unicode"><tt>\P{InGreek}</tt></td> * <td headers="matches">Any character except one in the Greek block (negation)</td></tr> * <tr><td valign="top" headers="construct unicode"><tt>[\p{L}&&[^\p{Lu}]] </tt></td> * <td headers="matches">Any letter except an uppercase letter (subtraction)</td></tr> * <tr><th> </th></tr> * <tr align="left"><th colspan="2" id="bounds">Boundary matchers</th></tr> * <tr><td valign="top" headers="construct bounds"><tt>^</tt></td> * <td headers="matches">The beginning of a line</td></tr> * <tr><td valign="top" headers="construct bounds"><tt>$</tt></td> * <td headers="matches">The end of a line</td></tr> * <tr><td valign="top" headers="construct bounds"><tt>\b</tt></td> * <td headers="matches">A word boundary</td></tr> * <tr><td valign="top" headers="construct bounds"><tt>\B</tt></td> * <td headers="matches">A non-word boundary</td></tr> * <tr><td valign="top" headers="construct bounds"><tt>\A</tt></td> * <td headers="matches">The beginning of the input</td></tr> * <tr><td valign="top" headers="construct bounds"><tt>\G</tt></td> * <td headers="matches">The end of the previous match</td></tr> * <tr><td valign="top" headers="construct bounds"><tt>\Z</tt></td> * <td headers="matches">The end of the input but for the final * <a href="#lt">terminator</a>, if any</td></tr> * <tr><td valign="top" headers="construct bounds"><tt>\z</tt></td> * <td headers="matches">The end of the input</td></tr> * <tr><th> </th></tr> * <tr align="left"><th colspan="2" id="greedy">Greedy quantifiers</th></tr> * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>?</tt></td> * <td headers="matches"><i>X</i>, once or not at all</td></tr> * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>*</tt></td> * <td headers="matches"><i>X</i>, zero or more times</td></tr> * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>+</tt></td> * <td headers="matches"><i>X</i>, one or more times</td></tr> * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>}</tt></td> * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr> * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,}</tt></td> * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr> * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}</tt></td> * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr> * <tr><th> </th></tr> * <tr align="left"><th colspan="2" id="reluc">Reluctant quantifiers</th></tr> * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>??</tt></td> * <td headers="matches"><i>X</i>, once or not at all</td></tr> * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>*?</tt></td> * <td headers="matches"><i>X</i>, zero or more times</td></tr> * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>+?</tt></td> * <td headers="matches"><i>X</i>, one or more times</td></tr> * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>}?</tt></td> * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr> * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,}?</tt></td> * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr> * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}?</tt></td> * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr> * <tr><th> </th></tr> * <tr align="left"><th colspan="2" id="poss">Possessive quantifiers</th></tr> * <tr><td valign="top" headers="construct poss"><i>X</i><tt>?+</tt></td> * <td headers="matches"><i>X</i>, once or not at all</td></tr> * <tr><td valign="top" headers="construct poss"><i>X</i><tt>*+</tt></td> * <td headers="matches"><i>X</i>, zero or more times</td></tr> * <tr><td valign="top" headers="construct poss"><i>X</i><tt>++</tt></td> * <td headers="matches"><i>X</i>, one or more times</td></tr> * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>}+</tt></td> * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr> * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,}+</tt></td> * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr> * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}+</tt></td> * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr> * <tr><th> </th></tr> * <tr align="left"><th colspan="2" id="logical">Logical operators</th></tr> * <tr><td valign="top" headers="construct logical"><i>XY</i></td> * <td headers="matches"><i>X</i> followed by <i>Y</i></td></tr> * <tr><td valign="top" headers="construct logical"><i>X</i><tt>|</tt><i>Y</i></td> * <td headers="matches">Either <i>X</i> or <i>Y</i></td></tr> * <tr><td valign="top" headers="construct logical"><tt>(</tt><i>X</i><tt>)</tt></td> * <td headers="matches">X, as a <a href="#cg">capturing group</a></td></tr> * <tr><th> </th></tr> * <tr align="left"><th colspan="2" id="backref">Back references</th></tr> * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>n</i></td> * <td valign="bottom" headers="matches">Whatever the <i>n</i><sup>th</sup> * <a href="#cg">capturing group</a> matched</td></tr> * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>k</i><<i>name</i>></td> * <td valign="bottom" headers="matches">Whatever the * <a href="#groupname">named-capturing group</a> "name" matched</td></tr> * <tr><th> </th></tr> * <tr align="left"><th colspan="2" id="quot">Quotation</th></tr> * <tr><td valign="top" headers="construct quot"><tt>\</tt></td> * <td headers="matches">Nothing, but quotes the following character</td></tr> * <tr><td valign="top" headers="construct quot"><tt>\Q</tt></td> * <td headers="matches">Nothing, but quotes all characters until <tt>\E</tt></td></tr> * <tr><td valign="top" headers="construct quot"><tt>\E</tt></td> * <td headers="matches">Nothing, but ends quoting started by <tt>\Q</tt></td></tr> * <!-- Metachars: !$()*+.<>?[\]^{|} --> * <tr><th> </th></tr> * <tr align="left"><th colspan="2" id="special">Special constructs (named-capturing and non-capturing)</th></tr> * <tr><td valign="top" headers="construct special"><tt>(?<<a href="#groupname">name</a>></tt><i>X</i><tt>)</tt></td> * <td headers="matches"><i>X</i>, as a named-capturing group</td></tr> * <tr><td valign="top" headers="construct special"><tt>(?:</tt><i>X</i><tt>)</tt></td> * <td headers="matches"><i>X</i>, as a non-capturing group</td></tr> * <tr><td valign="top" headers="construct special"><tt>(?idmsux-idmsux) </tt></td> * <td headers="matches">Nothing, but turns match flags <a href="#CASE_INSENSITIVE">i</a> * <a href="#UNIX_LINES">d</a> <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> * <a href="#UNICODE_CASE">u</a> <a href="#COMMENTS">x</a> on - off</td></tr> * <tr><td valign="top" headers="construct special"><tt>(?idmsux-idmsux:</tt><i>X</i><tt>)</tt> </td> * <td headers="matches"><i>X</i>, as a <a href="#cg">non-capturing group</a> with the * given flags <a href="#CASE_INSENSITIVE">i</a> <a href="#UNIX_LINES">d</a> * <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> <a href="#UNICODE_CASE">u</a > * <a href="#COMMENTS">x</a> on - off</td></tr> * <tr><td valign="top" headers="construct special"><tt>(?=</tt><i>X</i><tt>)</tt></td> * <td headers="matches"><i>X</i>, via zero-width positive lookahead</td></tr> * <tr><td valign="top" headers="construct special"><tt>(?!</tt><i>X</i><tt>)</tt></td> * <td headers="matches"><i>X</i>, via zero-width negative lookahead</td></tr> * <tr><td valign="top" headers="construct special"><tt>(?<=</tt><i>X</i><tt>)</tt></td> * <td headers="matches"><i>X</i>, via zero-width positive lookbehind</td></tr> * <tr><td valign="top" headers="construct special"><tt>(?<!</tt><i>X</i><tt>)</tt></td> * <td headers="matches"><i>X</i>, via zero-width negative lookbehind</td></tr> * <tr><td valign="top" headers="construct special"><tt>(?></tt><i>X</i><tt>)</tt></td> * <td headers="matches"><i>X</i>, as an independent, non-capturing group</td></tr> * <h4> Backslashes, escapes, and quoting </h4> * <p> The backslash character (<tt>'\'</tt>) serves to introduce escaped * constructs, as defined in the table above, as well as to quote characters * that otherwise would be interpreted as unescaped constructs. Thus the * expression <tt>\\</tt> matches a single backslash and <tt>\{</tt> matches a * <p> It is an error to use a backslash prior to any alphabetic character that * does not denote an escaped construct; these are reserved for future * extensions to the regular-expression language. A backslash may be used * prior to a non-alphabetic character regardless of whether that character is * part of an unescaped construct. * <p> Backslashes within string literals in Java source code are interpreted * Specification</a> as either <a * escapes</a> or other <a * escapes</a>. It is therefore necessary to double backslashes in string * literals that represent regular expressions to protect them from * interpretation by the Java bytecode compiler. The string literal * <tt>"\b"</tt>, for example, matches a single backspace character when * interpreted as a regular expression, while <tt>"\\b"</tt> matches a * word boundary. The string literal <tt>"\(hello\)"</tt> is illegal * and leads to a compile-time error; in order to match the string * <tt>(hello)</tt> the string literal <tt>"\\(hello\\)"</tt> * <h4> Character Classes </h4> * <p> Character classes may appear within other character classes, and * may be composed by the union operator (implicit) and the intersection * operator (<tt>&&</tt>). * The union operator denotes a class that contains every character that is * in at least one of its operand classes. The intersection operator * denotes a class that contains every character that is in both of its * <p> The precedence of character-class operators is as follows, from * <blockquote><table border="0" cellpadding="1" cellspacing="0" * summary="Precedence of character class operators."> * <tr><th>1 </th> * <td>Literal escape </td> * <td><tt>\x</tt></td></tr> * <tr><th>2 </th> * <td><tt>[...]</tt></td></tr> * <tr><th>3 </th> * <td><tt>a-z</tt></td></tr> * <tr><th>4 </th> * <td><tt>[a-e][i-u]</tt></td></tr> * <tr><th>5 </th> * <td><tt>[a-z&&[aeiou]]</tt></td></tr> * <p> Note that a different set of metacharacters are in effect inside * a character class than outside a character class. For instance, the * regular expression <tt>.</tt> loses its special meaning inside a * character class, while the expression <tt>-</tt> becomes a range * <h4> Line terminators </h4> * <p> A <i>line terminator</i> is a one- or two-character sequence that marks * the end of a line of the input character sequence. The following are * recognized as line terminators: * <li> A newline (line feed) character (<tt>'\n'</tt>), * <li> A carriage-return character followed immediately by a newline * character (<tt>"\r\n"</tt>), * <li> A standalone carriage-return character (<tt>'\r'</tt>), * <li> A next-line character (<tt>'\u0085'</tt>), * <li> A line-separator character (<tt>'\u2028'</tt>), or * <li> A paragraph-separator character (<tt>'\u2029</tt>). * <p>If {@link #UNIX_LINES} mode is activated, then the only line terminators * recognized are newline characters. * <p> The regular expression <tt>.</tt> matches any character except a line * terminator unless the {@link #DOTALL} flag is specified. * <p> By default, the regular expressions <tt>^</tt> and <tt>$</tt> ignore * line terminators and only match at the beginning and the end, respectively, * of the entire input sequence. If {@link #MULTILINE} mode is activated then * <tt>^</tt> matches at the beginning of input and after any line terminator * except at the end of input. When in {@link #MULTILINE} mode <tt>$</tt> * matches just before a line terminator or the end of the input sequence. * <h4> Groups and capturing </h4> * <h5> Group number </h5> * <p> Capturing groups are numbered by counting their opening parentheses from * left to right. In the expression <tt>((A)(B(C)))</tt>, for example, there * are four such groups: </p> * <blockquote><table cellpadding=1 cellspacing=0 summary="Capturing group numberings"> * <tr><th>1 </th> * <td><tt>((A)(B(C)))</tt></td></tr> * <tr><th>2 </th> * <td><tt>(A)</tt></td></tr> * <tr><th>3 </th> * <td><tt>(B(C))</tt></td></tr> * <tr><th>4 </th> * <td><tt>(C)</tt></td></tr> * <p> Group zero always stands for the entire expression. * <p> Capturing groups are so named because, during a match, each subsequence * of the input sequence that matches such a group is saved. The captured * subsequence may be used later in the expression, via a back reference, and * may also be retrieved from the matcher once the match operation is complete. * <p>A capturing group can also be assigned a "name", a <tt>named-capturing group</tt>, * and then be back-referenced later by the "name". Group names are composed of * the following characters. The first character must be a <tt>letter</tt>. * <li> The uppercase letters <tt>'A'</tt> through <tt>'Z'</tt> * (<tt>'\u0041'</tt> through <tt>'\u005a'</tt>), * <li> The lowercase letters <tt>'a'</tt> through <tt>'z'</tt> * (<tt>'\u0061'</tt> through <tt>'\u007a'</tt>), * <li> The digits <tt>'0'</tt> through <tt>'9'</tt> * (<tt>'\u0030'</tt> through <tt>'\u0039'</tt>), * <p> A <tt>named-capturing group</tt> is still numbered as described in * <a href="#gnumber">Group number</a>. * <p> The captured input associated with a group is always the subsequence * that the group most recently matched. If a group is evaluated a second time * because of quantification then its previously-captured value, if any, will * be retained if the second evaluation fails. Matching the string * <tt>"aba"</tt> against the expression <tt>(a(b)?)+</tt>, for example, leaves * group two set to <tt>"b"</tt>. All captured input is discarded at the * beginning of each match. * <p> Groups beginning with <tt>(?</tt> are either pure, <i>non-capturing</i> groups * that do not capture text and do not count towards the group total, or * <i>named-capturing</i> group. * <h4> Unicode support </h4> * <p> This class is in conformance with Level 1 of <a * Standard #18: Unicode Regular Expression Guidelines</i></a>, plus RL2.1 * <p> Unicode escape sequences such as <tt>\u2014</tt> in Java source code * are processed as described in <a * of the Java Language Specification. Such escape sequences are also * implemented directly by the regular-expression parser so that Unicode * escapes can be used in expressions that are read from files or from the * keyboard. Thus the strings <tt>"\u2014"</tt> and <tt>"\\u2014"</tt>, * while not equal, compile into the same pattern, which matches the character * with hexadecimal value <tt>0x2014</tt>. * <p> A Unicode character can also be represented in a regular-expression by * using its hexadecimal code point value directly as described in construct * <tt>\x{...}</tt>, for example a supplementary character U+2011F * can be specified as <tt>\x{2011F}</tt>, instead of two consecutive * Unicode escape sequences of the surrogate pair * <tt>\uD840</tt><tt>\uDD1F</tt>. * <p>Unicode scripts, blocks and categories are written with the <tt>\p</tt> and * <tt>\P</tt> constructs as in Perl. <tt>\p{</tt><i>prop</i><tt>}</tt> matches if * the input has the property <i>prop</i>, while <tt>\P{</tt><i>prop</i><tt>}</tt> * does not match if the input has that property. * Scripts are specified either with the prefix {@code Is}, as in * {@code IsHiragana}, or by using the {@code script} keyword (or its short * form {@code sc})as in {@code script=Hiragana} or {@code sc=Hiragana}. * Blocks are specified with the prefix {@code In}, as in * {@code InMongolian}, or by using the keyword {@code block} (or its short * form {@code blk}) as in {@code block=Mongolian} or {@code blk=Mongolian}. * Categories may be specified with the optional prefix {@code Is}: * Both {@code \p{L}} and {@code \p{IsL}} denote the category of Unicode * letters. Same as scripts and blocks, categories can also be specified * by using the keyword {@code general_category} (or its short form * {@code gc}) as in {@code general_category=Lu} or {@code gc=Lu}. * Scripts, blocks and categories can be used both inside and outside of a * <p> The supported categories are those of * <i>The Unicode Standard</i></a> in the version specified by the * {@link java.lang.Character Character} class. The category names are those * defined in the Standard, both normative and informative. * The script names supported by <code>Pattern</code> are the valid script names * accepted and defined by * {@link java.lang.Character.UnicodeScript#forName(String) UnicodeScript.forName}. * The block names supported by <code>Pattern</code> are the valid block names * accepted and defined by * {@link java.lang.Character.UnicodeBlock#forName(String) UnicodeBlock.forName}. * <a name="jcc"> <p>Categories that behave like the java.lang.Character * boolean is<i>methodname</i> methods (except for the deprecated ones) are * available through the same <tt>\p{</tt><i>prop</i><tt>}</tt> syntax where * the specified property has the name <tt>java<i>methodname</i></tt>. * <h4> Comparison to Perl 5 </h4> * <p>The <code>Pattern</code> engine performs traditional NFA-based matching * with ordered alternation as occurs in Perl 5. * <p> Perl constructs not supported by this class: </p> * <li><p> The conditional constructs <tt>(?{</tt><i>X</i><tt>})</tt> and * <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>|</tt><i>Y</i><tt>)</tt>, * <li><p> The embedded code constructs <tt>(?{</tt><i>code</i><tt>})</tt> * and <tt>(??{</tt><i>code</i><tt>})</tt>,</p></li> * <li><p> The embedded comment syntax <tt>(?#comment)</tt>, and </p></li> * <li><p> The preprocessing operations <tt>\l</tt> <tt>\u</tt>, * <tt>\L</tt>, and <tt>\U</tt>. </p></li> * <p> Constructs supported by this class but not by Perl: </p> * <li><p> Possessive quantifiers, which greedily match as much as they can * and do not back off, even when doing so would allow the overall match to * <li><p> Character-class union and intersection as described * <a href="#cc">above</a>.</p></li> * <p> Notable differences from Perl: </p> * <li><p> In Perl, <tt>\1</tt> through <tt>\9</tt> are always interpreted * as back references; a backslash-escaped number greater than <tt>9</tt> is * treated as a back reference if at least that many subexpressions exist, * otherwise it is interpreted, if possible, as an octal escape. In this * class octal escapes must always begin with a zero. In this class, * <tt>\1</tt> through <tt>\9</tt> are always interpreted as back * references, and a larger number is accepted as a back reference if at * least that many subexpressions exist at that point in the regular * expression, otherwise the parser will drop digits until the number is * smaller or equal to the existing number of groups or it is one digit. * <li><p> Perl uses the <tt>g</tt> flag to request a match that resumes * where the last match left off. This functionality is provided implicitly * by the {@link Matcher} class: Repeated invocations of the {@link * Matcher#find find} method will resume where the last match left off, * unless the matcher is reset. </p></li> * <li><p> In Perl, embedded flags at the top level of an expression affect * the whole expression. In this class, embedded flags always take effect * at the point at which they appear, whether they are at the top level or * within a group; in the latter case, flags are restored at the end of the * group just as in Perl. </p></li> * <li><p> Perl is forgiving about malformed matching constructs, as in the * expression <tt>*a</tt>, as well as dangling brackets, as in the * expression <tt>abc]</tt>, and treats them as literals. This * class also accepts dangling brackets but is strict about dangling * metacharacters like +, ? and *, and will throw a * {@link PatternSyntaxException} if it encounters them. </p></li> * <p> For a more precise description of the behavior of regular expression * <i>Mastering Regular Expressions, 3nd Edition</i>, Jeffrey E. F. Friedl, * O'Reilly and Associates, 2006.</a> * @see java.lang.String#split(String, int) * @see java.lang.String#split(String) * @author JSR-51 Expert Group * Regular expression modifier values. Instead of being passed as * arguments, they can also be passed as inline modifiers. * For example, the following statements have the same effect. * RegExp r1 = RegExp.compile("abc", Pattern.I|Pattern.M); * RegExp r2 = RegExp.compile("(?im)abc", 0); * The flags are duplicated so that the familiar Perl match flag * Enables Unix lines mode. * <p> In this mode, only the <tt>'\n'</tt> line terminator is recognized * in the behavior of <tt>.</tt>, <tt>^</tt>, and <tt>$</tt>. * <p> Unix lines mode can also be enabled via the embedded flag * expression <tt>(?d)</tt>. * Enables case-insensitive matching. * <p> By default, case-insensitive matching assumes that only characters * in the US-ASCII charset are being matched. Unicode-aware * case-insensitive matching can be enabled by specifying the {@link * #UNICODE_CASE} flag in conjunction with this flag. * <p> Case-insensitive matching can also be enabled via the embedded flag * expression <tt>(?i)</tt>. * <p> Specifying this flag may impose a slight performance penalty. </p> * Permits whitespace and comments in pattern. * <p> In this mode, whitespace is ignored, and embedded comments starting * with <tt>#</tt> are ignored until the end of a line. * <p> Comments mode can also be enabled via the embedded flag * expression <tt>(?x)</tt>. public static final int COMMENTS =
0x04;
* Enables multiline mode. * <p> In multiline mode the expressions <tt>^</tt> and <tt>$</tt> match * just after or just before, respectively, a line terminator or the end of * the input sequence. By default these expressions only match at the * beginning and the end of the entire input sequence. * <p> Multiline mode can also be enabled via the embedded flag * expression <tt>(?m)</tt>. </p> * Enables literal parsing of the pattern. * <p> When this flag is specified then the input string that specifies * the pattern is treated as a sequence of literal characters. * Metacharacters or escape sequences in the input sequence will be * given no special meaning. * <p>The flags CASE_INSENSITIVE and UNICODE_CASE retain their impact on * matching when used in conjunction with this flag. The other flags * <p> There is no embedded flag character for enabling literal parsing. public static final int LITERAL =
0x10;
* <p> In dotall mode, the expression <tt>.</tt> matches any character, * including a line terminator. By default this expression does not match * <p> Dotall mode can also be enabled via the embedded flag * expression <tt>(?s)</tt>. (The <tt>s</tt> is a mnemonic for * "single-line" mode, which is what this is called in Perl.) </p> public static final int DOTALL =
0x20;
* Enables Unicode-aware case folding. * <p> When this flag is specified then case-insensitive matching, when * enabled by the {@link #CASE_INSENSITIVE} flag, is done in a manner * consistent with the Unicode Standard. By default, case-insensitive * matching assumes that only characters in the US-ASCII charset are being * <p> Unicode-aware case folding can also be enabled via the embedded flag * expression <tt>(?u)</tt>. * <p> Specifying this flag may impose a performance penalty. </p> * Enables canonical equivalence. * <p> When this flag is specified then two characters will be considered * to match if, and only if, their full canonical decompositions match. * The expression <tt>"a\u030A"</tt>, for example, will match the * string <tt>"\u00E5"</tt> when this flag is specified. By default, * matching does not take canonical equivalence into account. * <p> There is no embedded flag character for enabling canonical * <p> Specifying this flag may impose a performance penalty. </p> public static final int CANON_EQ =
0x80;
/* Pattern has only two serialized components: The pattern string * and the flags, which are all that is needed to recompile the pattern * when it is deserialized. /** use serialVersionUID from Merlin b59 for interoperability */ * The original regular-expression pattern string. * The original pattern flags. * Boolean indicating this Pattern is compiled; this is necessary in order * to lazily compile deserialized Patterns. private transient volatile boolean compiled =
false;
* The normalized pattern string. * The starting point of state machine for the find operation. This allows * a match to start anywhere in the input. * The root of object tree for a match operation. The pattern is matched * at the beginning. This may include a find that uses BnM or a First * Temporary storage used by parsing pattern slice. * Map the "name" of the "named capturing group" to its group id * Temporary storage used while parsing group references. * Temporary null terminated code point array used by pattern compiling. private transient int[]
temp;
* The number of capturing groups in this Pattern. Used by matchers to * allocate storage needed to perform a match. * The local variable count used by parsing tree. Used by matchers to * allocate storage needed to perform a match. * Index into the pattern string that keeps track of how much has been * Holds the length of the pattern string. * If the Start node might possibly match supplementary characters. * It is set to true during compiling if * (1) There is supplementary char in pattern, or * (2) There is complement node of Category or Block * Compiles the given regular expression into a pattern. </p> * The expression to be compiled * @throws PatternSyntaxException * If the expression's syntax is invalid * Compiles the given regular expression into a pattern with the given * The expression to be compiled * Match flags, a bit mask that may include * {@link #CASE_INSENSITIVE}, {@link #MULTILINE}, {@link #DOTALL}, * {@link #UNICODE_CASE}, {@link #CANON_EQ}, {@link #UNIX_LINES}, * {@link #LITERAL} and {@link #COMMENTS} * @throws IllegalArgumentException * If bit values other than those corresponding to the defined * match flags are set in <tt>flags</tt> * @throws PatternSyntaxException * If the expression's syntax is invalid * Returns the regular expression from which this pattern was compiled. * @return The source of this pattern * <p>Returns the string representation of this pattern. This * is the regular expression from which this pattern was * @return The string representation of this pattern * Creates a matcher that will match the given input against this pattern. * The character sequence to be matched * @return A new matcher for this pattern * Returns this pattern's match flags. </p> * @return The match flags specified when this pattern was compiled * Compiles the given regular expression and attempts to match the given * <p> An invocation of this convenience method of the form * Pattern.matches(regex, input);</pre></blockquote> * behaves in exactly the same way as the expression * Pattern.compile(regex).matcher(input).matches()</pre></blockquote> * <p> If a pattern is to be used multiple times, compiling it once and reusing * it will be more efficient than invoking this method each time. </p> * The expression to be compiled * The character sequence to be matched * @throws PatternSyntaxException * If the expression's syntax is invalid * Splits the given input sequence around matches of this pattern. * <p> The array returned by this method contains each substring of the * input sequence that is terminated by another subsequence that matches * this pattern or is terminated by the end of the input sequence. The * substrings in the array are in the order in which they occur in the * input. If this pattern does not match any subsequence of the input then * the resulting array has just one element, namely the input sequence in * <p> The <tt>limit</tt> parameter controls the number of times the * pattern is applied and therefore affects the length of the resulting * array. If the limit <i>n</i> is greater than zero then the pattern * will be applied at most <i>n</i> - 1 times, the array's * length will be no greater than <i>n</i>, and the array's last entry * will contain all input beyond the last matched delimiter. If <i>n</i> * is non-positive then the pattern will be applied as many times as * possible and the array can have any length. If <i>n</i> is zero then * the pattern will be applied as many times as possible, the array can * have any length, and trailing empty strings will be discarded. * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following * results with these parameters: * <blockquote><table cellpadding=1 cellspacing=0 * summary="Split examples showing regex, limit, and result"> * <tr><th><P align="left"><i>Regex </i></th> * <th><P align="left"><i>Limit </i></th> * <th><P align="left"><i>Result </i></th></tr> * <tr><td align=center>:</td> * <td align=center>2</td> * <td><tt>{ "boo", "and:foo" }</tt></td></tr> * <tr><td align=center>:</td> * <td align=center>5</td> * <td><tt>{ "boo", "and", "foo" }</tt></td></tr> * <tr><td align=center>:</td> * <td align=center>-2</td> * <td><tt>{ "boo", "and", "foo" }</tt></td></tr> * <tr><td align=center>o</td> * <td align=center>5</td> * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr> * <tr><td align=center>o</td> * <td align=center>-2</td> * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr> * <tr><td align=center>o</td> * <td align=center>0</td> * <td><tt>{ "b", "", ":and:f" }</tt></td></tr> * The character sequence to be split * The result threshold, as described above * @return The array of strings computed by splitting the input * around matches of this pattern // Add segments before each match found // If no match was found, return this * Splits the given input sequence around matches of this pattern. * <p> This method works as if by invoking the two-argument {@link * #split(java.lang.CharSequence, int) split} method with the given input * sequence and a limit argument of zero. Trailing empty strings are * therefore not included in the resulting array. </p> * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following * results with these expressions: * <blockquote><table cellpadding=1 cellspacing=0 * summary="Split examples showing regex and result"> * <tr><th><P align="left"><i>Regex </i></th> * <th><P align="left"><i>Result</i></th></tr> * <tr><td align=center>:</td> * <td><tt>{ "boo", "and", "foo" }</tt></td></tr> * <tr><td align=center>o</td> * <td><tt>{ "b", "", ":and:f" }</tt></td></tr> * The character sequence to be split * @return The array of strings computed by splitting the input * around matches of this pattern * Returns a literal pattern <code>String</code> for the specified * <p>This method produces a <code>String</code> that can be used to * create a <code>Pattern</code> that would match the string * <code>s</code> as if it were a literal pattern.</p> Metacharacters * or escape sequences in the input sequence will be given no special * @param s The string to be literalized * @return A literal string replacement return "\\Q" + s +
"\\E";
* Recompile the Pattern instance from a stream. The original pattern * string is read in and the object tree is recompiled from it. // if length > 0, the Pattern is lazily compiled * This private constructor is used to create all Patterns. The pattern * string and match flags are all that is needed to completely describe * a Pattern. An empty pattern string results in an object tree with * only a Start node and a LastNode node. // Reset group index count * The pattern is converted to normalizedD form and then a pure group * is constructed to match canonical equivalences of the characters. // Convert pattern into normalizedD form // Modify pattern to match canonical equivalences * Complete the character class being parsed and add a set * of alternations to it that will match the canonical equivalences * of the characters within the class. throw error(
"Unclosed character class");
* Given a specific sequence composed of a regular character and * combining marks that follow it, produce the alternation that will * match all canonical equivalences of that sequence. // source has one character. // Add combined permutations * Returns an array of strings that have all the possible * permutations of the characters in the input string. * This is used to get a list of all possible orderings * of a set of combining marks. Note that some of the permutations * are invalid because of combining class collisions, and these * possibilities must be removed because they are not canonically // For each char, take it out and add the permutations // of the remaining chars // offset maintains the index in code units. for(
int y=x-
1; y>=
0; y--) {
for (
int x=
0; x<
index; x++)
* Attempts to compose input by combining the first character * with the first combining mark following it. Returns a String * that is the composition of the leading character with its first * combining mark followed by the remaining combining marks. Returns * null if the first two characters cannot be further composed. * Preprocess any \Q...\E sequences in `temp', meta-quoting them. * See the description of `quotemeta' in perlfunc(1). else if (
temp[i +
1] !=
'Q')
if (i >=
pLen -
1)
// No \Q sequence found * Copies regular expression to an int array and invokes the parsing * of the expression which will create the object tree. // Handle canonical equivalences // Copy pattern to int array for convenience // Use double zero to terminate pattern // Convert all chars into code points // Allocate all temporary objects here. // Literal pattern handling // Start recursive descent parsing // Check extra pattern characters throw error(
"Unmatched closing ')'");
throw error(
"Unexpected internal error");
// Release temporary storage * Used to print out a subtree of the Pattern to help with debugging. * Used to accumulate information about a subtree of the object graph * so that optimizations can be applied to the subtree. * The following private methods are mainly used to improve the * readability of the code. In order to let the Java compiler easily * inline them, we should not put many assertions or error checks in them. * Indicates whether a particular flag is set or not. private boolean has(
int f) {
* Match next character, signal error if failed. * Mark the end of pattern with a specific character. private void mark(
int c) {
* Peek the next character, and do not advance the cursor. * Read the next character, and advance the cursor by one. * Read the next character, and advance the cursor by one, * ignoring the COMMENTS setting * Advance the cursor by one, and peek the next character. * Advance the cursor by one, and peek the next character, * ignoring the COMMENTS setting * If in xmode peek past whitespace and comments. * If in xmode parse past whitespace and comments. * xmode parse past comment to end of line. * xmode peek past comment to end of line. * Determines if character is a line separator in the current mode * Read the character after the next one, and advance the cursor by two. * Unread one next character, and retreat cursor by one. * Internal method used for handling all syntax errors. The pattern is * displayed with a pointer to aid in locating the syntax error. * Determines if there is any supplementary character or unpaired * surrogate in the specified range. * Determines if the specified code point is a supplementary * character or unpaired surrogate. * The following methods handle the main parsing. They are sorted * according to their precedence order, the lowest one first. * The expression is parsed with branch nodes added for alternations. * This may be called recursively to parse sub expressions that may // if the node returned from sequence() is "end" // we have an empty expr, set a null atom into // the branch to indicate to go "next" directly. // the "tail.next" of each atom goes to branchConn // replace the "end" with "branchConn" at its tail.next // when put the "prev" into the branch as the first atom. * Parsing of sequences between alternations. // Because group handles its own closure, // we need to treat it differently // Check for comment or flag group // Double return: Tail was returned in root if (
ch ==
'p' ||
ch ==
'P') {
ch =
next();
// Consume { if present case ']':
// Now interpreting dangling ] and } as literals throw error(
"Dangling meta character '" + ((
char)
ch) +
"'");
* Parse and add a new Single or Slice. if (
ch ==
'p' ||
ch ==
'P') {
// Property if (
first >
0) {
// Slice is waiting; handle it first }
else {
// No slice; just return the family node ch =
next();
// Consume { if present // Unwind meta escape sequence * Parses a backref greedily, taking as many numbers as it * can. The first digit is always treated as a backref, but * multi digit numbers are only treated as a backref if at * least that many backrefs exist at this point in the regex. // Add another number if it doesn't make a group * Parses an escape sequence to determine the actual value that needs * If -1 is returned and create was true a new object was added to the tree * to handle the escape sequence. * If the returned value is greater than zero, it is the value that * matches the escape sequence. throw error(
"\\k is not followed by '<' for named capturing group");
throw error(
"(named capturing group <"+
name+
"> does not exit");
* Parse a character class, and return the node that matches it. * Consumes a ] on the way out if consume is true. Usually consume * is true except for the case of [abc&&def] where def is a separate * right hand node with "understood" brackets. // Negates if first char in a class, otherwise literal // ^ not first in class, treat as literal while (
ch !=
']' &&
ch !=
'&') {
throw error(
"Bad class syntax");
throw error(
"Unclosed character class");
/* Bits can only handle codepoints in [u+0000-u+00ff] range. Use "single" node instead of bits when dealing with unicode case folding for codepoints listed below. (1)Uppercase out of range: u+00ff, u+00b5 toUpperCase(u+00ff) -> u+0178 toUpperCase(u+00b5) -> u+039c (2)LatinSmallLetterLongS u+17f toUpperCase(u+017f) -> u+0053 (3)LatinSmallLetterDotlessI u+131 toUpperCase(u+0131) -> u+0049 (4)LatinCapitalLetterIWithDotAbove u+0130 toLowerCase(u+0130) -> u+0069 toLowerCase(u+212a) ==> u+006B toLowerCase(u+212b) ==> u+00e5 (
ch ==
0xff ||
ch ==
0xb5 ||
ch ==
0x49 ||
ch ==
0x69 ||
//I and i ch ==
0x53 ||
ch ==
0x73 ||
//S and s ch ==
0x4b ||
ch ==
0x6b ||
//K and k ch ==
0xc5 ||
ch ==
0xe5)))
//A+ring * Parse a single character or a character range in a character class * and return its representative node. if (
ch ==
'p' ||
ch ==
'P') {
// A property }
else {
// ordinary escape throw error(
"Illegal character range");
throw error(
"Unexpected character '"+((
char)
ch)+
"'");
* Parses a Unicode character family and returns its representative node. throw error(
"Unclosed character family");
throw error(
"Empty character family");
// property construct \p{name=value} throw error(
"Unknown Unicode property {name=<" +
name +
">, " +
"value=<" +
value +
">}");
// \p{isGeneralCategory} and \p{isScriptName} * Returns a CharProperty matching all characters belong to throw error(
"Unknown character script name {" +
name +
"}");
* Returns a CharProperty matching all characters in a UnicodeBlock. throw error(
"Unknown character block name {" +
name +
"}");
* Returns a CharProperty matching all characters in a named property. throw error(
"Unknown character property name {" +
name +
"}");
* Parses and returns the name of a "named capturing group", the trailing * ">" is consumed after parsing. throw error(
"named capturing group has 0 length name");
throw error(
"named capturing group is missing trailing '>'");
* Parses a group and returns the head node of a set of nodes that process * the group. Sometimes a double return system is used where the tail is case ':':
// (?:xxx) pure group case '=':
// (?=xxx) and (?!xxx) lookahead case '>':
// (?>xxx) independent group case '<':
// (?<xxx) look behind +
"> is already defined");
throw error(
"Look-behind group does not have " +
"an obvious maximum length");
throw error(
"Unknown look-behind group");
throw error(
"Unknown group type");
default:
// (?xxx:) inlined match flags return null;
// Inline modifier only throw error(
"Unknown inline modifier");
}
else {
// (xxx) a regular group accept(
')',
"Unclosed group");
return node;
// Dual return if (
head ==
tail) {
// Zero length assertion return node;
// Dual return }
else {
// Reluctant quantifier // Discover if the group is deterministic }
else {
// Non-deterministic throw error(
"Internal logic error");
* Create group head and tail nodes using double return. If the group is * created with anonymous true then it is a pure group and should not * Parses inlined match flags and set them appropriately. case '-':
// subFlag then fall through * Parses the second part of inlined match flags and turns off static final int LAZY =
1;
* Processes repetition. If the next character peeked is a quantifier * then new nodes must be appended to handle the repetition. * Prev could be a single or a group, so it could be a chain of nodes. throw error(
"Unclosed counted closure");
throw error(
"Illegal repetition range");
throw error(
"Illegal repetition");
* Utility method for parsing control escape sequences. throw error(
"Illegal control escape sequence");
* Utility method for parsing octal escape sequences. if (((n-
'0')|(
'7'-n)) >=
0) {
if (((m-
'0')|(
'7'-m)) >=
0) {
if ((((o-
'0')|(
'7'-o)) >=
0) && (((n-
'0')|(
'3'-n)) >=
0)) {
return (n -
'0') *
64 + (m -
'0') *
8 + (o -
'0');
return (n -
'0') *
8 + (m -
'0');
throw error(
"Illegal octal escape sequence");
* Utility method for parsing hexadecimal escape sequences. throw error(
"Hexadecimal codepoint is too big");
throw error(
"Unclosed hexadecimal escape sequence");
throw error(
"Illegal hexadecimal escape sequence");
* Utility method for parsing unicode escape sequences. for (
int i =
0; i <
4; i++) {
throw error(
"Illegal Unicode escape sequence");
// Utility methods for code point support for (
int i =
0; x >
0 && i <
len; i++) {
for (
int i =
0; i <
length; ) {
* Creates a bit vector for matching Latin-1 values. A normal BitClass * never matches values above Latin-1, and a complemented BitClass always * matches values above Latin-1. assert c >=
0 && c <=
255;
* Returns a suitably optimized, single character matcher. return new SingleS(
ch);
// Match a given Unicode character return new Single(
ch);
// Match a given BMP character * Utility method for creating a string slice matcher. for (
int i =
0; i <
count; i++) {
for (
int i =
0; i <
count; i++) {
for (
int i =
0; i <
count; i++) {
* The following classes are the building components of the object * tree that represents a compiled regular expression. The object tree * is made of individual elements that handle constructs in the Pattern. * Each type of object knows how to match its equivalent construct with * Base class for all node classes. Subclasses should override the match() * method as appropriate. This class is an accepting node, so its match() * This method implements the classic accept node. * This method is good for all zero length assertions. * This method implements the classic accept node with * the addition of a check to see if the match occurred * using all of the input. * Used for REs that can start anywhere within the input string. * This basically tries to match repeatedly at each spot in the * input string, moving forward after each try. An anchored search * or a BnM will bypass this node completely. for (; i <=
guard; i++) {
* StartS supports supplementary characters, including unpaired surrogates. //if ((ret = next.match(matcher, i, seq)) || i == guard) // Optimization to move to the next character. This is // faster than countChars(seq, i, 1). * Node to anchor at the beginning of input. This object implements the * match for a \A sequence, and the caret anchor will use this if not in * Node to anchor at the end of input. This is the absolute end, so this * should not match at the last newline before the end as $ will. static final class End extends Node {
* Node to anchor at the beginning of a line. This is essentially the * object to match for the multiline ^. // Perl does not match ^ at end of input even after newline if (
ch !=
'\n' &&
ch !=
'\r' // Should treat /r/n as one newline * Node to anchor at the beginning of a line when in unixdot mode. // Perl does not match ^ at end of input even after newline * Node to match the location where the last match ended. * This is used for the \G construct. * Node to anchor at the end of a line or the end of input based on the * When not in multiline mode, the $ can only match at the very end * of the input, unless the input ends in a line terminator in which * it matches right before the last line terminator. * Note that \r\n is considered an atomic line terminator. * Like ^ the $ operator matches at a position, it does not match the * line terminators themselves. // Matches before any line terminator; also matches at the // Before line terminator: // If multiline, we match here no matter what // If not multiline, fall through so that the end // is marked as hit; this must be a /r/n or a /n // at the very end so the end was hit; more input // could make this not match here }
else if (
ch ==
'\r' ||
ch ==
'\u0085' ||
}
else {
// No line terminator, no match // Matched at current end so hit end // If a $ matches because of end of input, then more input // could cause it to fail! * Node to anchor at the end of a line or the end of input based on the * multiline mode when in unix lines mode. // If not multiline, then only possible to // match at very end or one before end // If multiline return next.match without setting // Matching because at the end or 1 before the end; // more input could change this so set hitEnd // If a $ matches because of end of input, then more input // could cause it to fail! * Abstract node class to match one character satisfying some * Optimized version of CharProperty that works only for * properties never satisfied by Supplementary characters. * Node class that matches a Supplementary Unicode character * Optimization -- matches a given BMP character * Case insensitive matches a given BMP character * Unicode case insensitive matches a given Unicode character * Node class that matches a Unicode block. * Node class that matches a Unicode script * Node class that matches a Unicode category. * Node class that matches a POSIX type. * Base class for all Slice nodes for (
int j=
0; j<
len; j++) {
for (
int j=
0; j<
len; j++) {
* literal characters. Uses unicode case folding. for (
int j=
0; j<
len; j++) {
* Node class for a case sensitive sequence of literal characters * including supplementary characters. * Node class for a case insensitive sequence of literal characters * including supplementary characters. * Node class for a case insensitive sequence of literal characters. * Uses unicode case folding. * Returns node for matching characters within an explicit value range. * Returns node for matching characters within an explicit value * range in a case insensitive manner. * Implements the Unicode category ALL and the dot metacharacter when * Node class for the dot metacharacter when dotall is not enabled. return (
ch !=
'\n' &&
ch !=
'\r' * Node class for the dot metacharacter when dotall is not enabled * but UNIX_LINES is enabled. * The 0 or 1 quantifier. This one class implements all three types. * Handles the curly-brace style repetition with a specified minimum and * maximum occurrences. The * quantifier is handled as a special case. * This class handles the three types. for (j =
0; j <
cmin; j++) {
// i is the index to start matching at // j is the number of atoms that have matched // We have matched the maximum... continue with the rest of // the regular expression // k is the length of this match if (k ==
0)
// Zero length match // Move up index and number matched // We are greedy so match as many as we can // Handle backing off if match fails // Reluctant match. At this point, the minimum has been satisfied. // i is the index to start matching at // j is the number of atoms that have matched // Try finishing match without consuming any more // At the maximum, no match found // Okay, must try one more atom // If we haven't moved forward then must break out // Move up index and number matched temp =
0xFFFFFFF;
// arbitrary large number * Handles the curly-brace style repetition with a specified minimum and * maximum occurrences in deterministic cases. This is an iterative * optimization over the Prolog and Loop system which would handle this * in a recursive way. The * quantifier is handled as a special case. * If capture is true then this class saves group settings and ensures * that groups are unset when backing off of a group match. // Notify GroupTail there is no need to setup group info // because it will be set here for (
int j =
0; j <
cmin; j++) {
// Aggressive group match temp =
0xFFFFFFF;
// Arbitrary large number * A Guard node at the end of each atom node in a Branch. It * serves the purpose of chaining the "match" operation to * "next" but not the "study", so we can collect the TreeInfo * of each atom node without including the TreeInfo of the * Handles the branching of alternations. Note this is also used for * the ? quantifier to branch between the case where it matches once * and where it does not occur. for (
int n =
0; n <
size; n++) {
for (
int n =
0; n <
size; n++) {
* The GroupHead saves the location where the group begins in the locals * and restores them when the match is done. * The matchRef is used when a reference to this group is accessed later * in the expression. The locals will have a negative value in them to * indicate that we do not want to unset the group if the reference * Recursive reference to a group in the regular expression. It calls * matchRef because if the reference fails to match we would not unset * The GroupTail handles the setting of group beginning and ending * locations when groups are successfully matched. It must also be able to * unset groups that have to be backed off of. * The GroupTail node is also used when a previous group is referenced, * and in that case no group information needs to be set. if (
tmp >=
0) {
// This is the normal group case. // Save the group so we can unset it if it // This is a group reference case. We don't need to save any // group info because it isn't really a group. * This sets up a loop to handle a recursive quantifier structure. * Handles the repetition count for a greedy Curly. The matchInit * is called from the Prolog to save the index of where the group * beginning is stored. A zero length group check occurs in the * normal match but is skipped in the matchInit. int countIndex;
// local count index in matcher locals // Avoid infinite loop in zero-length case. // This block is for before we reach the minimum // iterations required for the loop to match // If match failed we must backtrack, so // the loop count should NOT be incremented // Return success or failure since we are under // This block is for after we have the minimum // iterations required for the loop to match // If match failed we must backtrack, so // the loop count should NOT be incremented * Handles the repetition count for a reluctant Curly. The matchInit * is called from the Prolog to save the index of where the group * beginning is stored. A zero length group check occurs in the * normal match but is skipped in the matchInit. // Check for zero length group // If match failed we must backtrack, so // the loop count should NOT be incremented // If match failed we must backtrack, so // the loop count should NOT be incremented * Refers to a group in the regular expression. Attempts to match * whatever the group referred to last matched. // If the referenced group didn't match, neither can this // If there isn't enough input left no match // Check each new char to make sure it matches what the group // referenced matched last time around // If the referenced group didn't match, neither can this // If there isn't enough input left no match // Check each new char to make sure it matches what the group // referenced matched last time around * Searches until the next instance of its atom. This is useful for * finding the atom efficiently without passing an instance of it * (greedy problem) and without a lot of wasted search time (reluctant * Zero width positive lookahead. static final class Pos extends Node {
// Relax transparent region boundaries for lookahead // Reinstate region boundaries * Zero width negative lookahead. static final class Neg extends Node {
// Relax transparent region boundaries for lookahead // If a negative lookahead succeeds then more input // could cause it to fail! // Reinstate region boundaries * For use with lookbehinds; matches the position where the lookbehind * Zero width positive lookbehind. // Relax transparent region boundaries for lookbehind * Zero width positive lookbehind, including supplementary * characters or unpaired surrogates. // Relax transparent region boundaries for lookbehind * Zero width negative lookbehind. // Relax transparent region boundaries for lookbehind // Reinstate region boundaries * Zero width negative lookbehind, including supplementary * characters or unpaired surrogates. // Relax transparent region boundaries for lookbehind //Reinstate region boundaries * Returns the set union of two CharProperty nodes. * Returns the set intersection of two CharProperty nodes. * Returns the set difference of two CharProperty nodes. * Handles word boundaries. Includes a field to allow this one class to * deal with the different types of word boundaries we can match. The word * characters include underscores, letters, and digits. Non spacing marks * can are also part of a word if they have a base character, otherwise * they are ignored for purposes of finding word boundaries. // Tried to access char past the end // The addition of another char could wreck a boundary * Non spacing marks only count as word characters in bounds calculations * if they have a base character. for (
int x=i; x >=
start; x--) {
* Attempts to match a slice in the input using the Boyer-Moore string * matching algorithm. The algorithm is based on the idea that the * pattern can be shifted farther ahead in the search text if it is * The pattern is compared to the input one character at a time, from * the rightmost character in the pattern to the left. If the characters * all match the pattern has been found. If a character does not match, * the pattern is shifted right a distance that is the maximum of two * functions, the bad character shift and the good suffix shift. This * shift moves the attempted match position through the input more * quickly than a naive one position at a time check. * The bad character shift is based on the character from the text that * did not match. If the character does not appear in the pattern, the * pattern can be shifted completely beyond the bad character. If the * character does occur in the pattern, the pattern can be shifted to * line the pattern up with the next occurrence of that character. * The good suffix shift is based on the idea that some subset on the right * side of the pattern has matched. When a bad character is found, the * pattern can be shifted right by the pattern length if the subset does * not occur again in pattern, or by the amount of distance to the * next occurrence of the subset in the pattern. * Boyer-Moore search methods adapted from code by Amy Yu. * Pre calculates arrays needed to generate the bad character * shift and the good suffix shift. Only the last seven bits * are used to see if chars match; This keeps the tables small * and covers the heavily used ASCII range, but occasionally * results in an aliased match for the bad character shift. // The BM algorithm requires a bit of overhead; // If the pattern is short don't use it, since // a shift larger than the pattern length cannot // Precalculate part of the bad character shift // It is a table for where in the pattern each // lower 7-bit value occurs // Precalculate the good suffix shift // i is the shift amount being considered // j is the beginning index of suffix being considered // Testing for good suffix // src[j..len] is a good suffix // No match. The array has already been // filled up with correct values before. // This fills up the remaining of optoSft // any suffix can not have larger shift amount // then its sub-suffix. Why??? // Set the guard value because of unicode compression // Loop over all possible match positions in text // Loop over pattern from right to left // Shift search to the right by the maximum of the // bad character shift and the good suffix shift // Entire pattern matched starting at i // BnM is only used as the leading node in the unanchored case, // and it replaced its Start() which always searches to the end // if it doesn't find what it's looking for, so hitEnd is true. * Supplementary support version of BnM(). Unpaired surrogates are * also handled by this class. static final class BnMS extends BnM {
// Loop over all possible match positions in text // Loop over pattern from right to left // Shift search to the right by the maximum of the // bad character shift and the good suffix shift // Entire pattern matched starting at i /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// * This must be the very first initializer. // Unicode character property aliases, defined in // Posix regular expression character classes, defined in defRange(
"Digit",
'0',
'9');
// Numeric characters defRange(
"Lower",
'a',
'z');
// Lower-case alphabetic defRange(
"Print",
0x20,
0x7E);
// Printable characters defRange(
"Upper",
'A',
'Z');
// Upper-case alphabetic