Pattern.java revision 85d69e16fe1e59e50c1ad228e754abf325088362
1/* 2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package java.util.regex; 27 28import java.util.ArrayList; 29 30 31/** 32 * A compiled representation of a regular expression. 33 * 34 * <p> A regular expression, specified as a string, must first be compiled into 35 * an instance of this class. The resulting pattern can then be used to create 36 * a {@link Matcher} object that can match arbitrary {@link 37 * java.lang.CharSequence </code>character sequences<code>} against the regular 38 * expression. All of the state involved in performing a match resides in the 39 * matcher, so many matchers can share the same pattern. 40 * 41 * <p> A typical invocation sequence is thus 42 * 43 * <blockquote><pre> 44 * Pattern p = Pattern.{@link #compile compile}("a*b"); 45 * Matcher m = p.{@link #matcher matcher}("aaaaab"); 46 * boolean b = m.{@link Matcher#matches matches}();</pre></blockquote> 47 * 48 * <p> A {@link #matches matches} method is defined by this class as a 49 * convenience for when a regular expression is used just once. This method 50 * compiles an expression and matches an input sequence against it in a single 51 * invocation. The statement 52 * 53 * <blockquote><pre> 54 * boolean b = Pattern.matches("a*b", "aaaaab");</pre></blockquote> 55 * 56 * is equivalent to the three statements above, though for repeated matches it 57 * is less efficient since it does not allow the compiled pattern to be reused. 58 * 59 * <p> Instances of this class are immutable and are safe for use by multiple 60 * concurrent threads. Instances of the {@link Matcher} class are not safe for 61 * such use. 62 * 63 * 64 * <a name="sum"> 65 * <h4> Summary of regular-expression constructs </h4> 66 * 67 * <table border="0" cellpadding="1" cellspacing="0" 68 * summary="Regular expression constructs, and what they match"> 69 * 70 * <tr align="left"> 71 * <th bgcolor="#CCCCFF" align="left" id="construct">Construct</th> 72 * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th> 73 * </tr> 74 * 75 * <tr><th> </th></tr> 76 * <tr align="left"><th colspan="2" id="characters">Characters</th></tr> 77 * 78 * <tr><td valign="top" headers="construct characters"><i>x</i></td> 79 * <td headers="matches">The character <i>x</i></td></tr> 80 * <tr><td valign="top" headers="construct characters"><tt>\\</tt></td> 81 * <td headers="matches">The backslash character</td></tr> 82 * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>n</i></td> 83 * <td headers="matches">The character with octal value <tt>0</tt><i>n</i> 84 * (0 <tt><=</tt> <i>n</i> <tt><=</tt> 7)</td></tr> 85 * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>nn</i></td> 86 * <td headers="matches">The character with octal value <tt>0</tt><i>nn</i> 87 * (0 <tt><=</tt> <i>n</i> <tt><=</tt> 7)</td></tr> 88 * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>mnn</i></td> 89 * <td headers="matches">The character with octal value <tt>0</tt><i>mnn</i> 90 * (0 <tt><=</tt> <i>m</i> <tt><=</tt> 3, 91 * 0 <tt><=</tt> <i>n</i> <tt><=</tt> 7)</td></tr> 92 * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>hh</i></td> 93 * <td headers="matches">The character with hexadecimal value <tt>0x</tt><i>hh</i></td></tr> 94 * <tr><td valign="top" headers="construct characters"><tt>\u</tt><i>hhhh</i></td> 95 * <td headers="matches">The character with hexadecimal value <tt>0x</tt><i>hhhh</i></td></tr> 96 * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>{h...h}</i></td> 97 * <td headers="matches">The character with hexadecimal value <tt>0x</tt><i>h...h</i> 98 * ({@link java.lang.Character#MIN_CODE_POINT Character.MIN_CODE_POINT} 99 * <= <tt>0x</tt><i>h...h</i> <=  100 * {@link java.lang.Character#MAX_CODE_POINT Character.MAX_CODE_POINT})</td></tr> 101 * <tr><td valign="top" headers="matches"><tt>\t</tt></td> 102 * <td headers="matches">The tab character (<tt>'\u0009'</tt>)</td></tr> 103 * <tr><td valign="top" headers="construct characters"><tt>\n</tt></td> 104 * <td headers="matches">The newline (line feed) character (<tt>'\u000A'</tt>)</td></tr> 105 * <tr><td valign="top" headers="construct characters"><tt>\r</tt></td> 106 * <td headers="matches">The carriage-return character (<tt>'\u000D'</tt>)</td></tr> 107 * <tr><td valign="top" headers="construct characters"><tt>\f</tt></td> 108 * <td headers="matches">The form-feed character (<tt>'\u000C'</tt>)</td></tr> 109 * <tr><td valign="top" headers="construct characters"><tt>\a</tt></td> 110 * <td headers="matches">The alert (bell) character (<tt>'\u0007'</tt>)</td></tr> 111 * <tr><td valign="top" headers="construct characters"><tt>\e</tt></td> 112 * <td headers="matches">The escape character (<tt>'\u001B'</tt>)</td></tr> 113 * <tr><td valign="top" headers="construct characters"><tt>\c</tt><i>x</i></td> 114 * <td headers="matches">The control character corresponding to <i>x</i></td></tr> 115 * 116 * <tr><th> </th></tr> 117 * <tr align="left"><th colspan="2" id="classes">Character classes</th></tr> 118 * 119 * <tr><td valign="top" headers="construct classes"><tt>[abc]</tt></td> 120 * <td headers="matches"><tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (simple class)</td></tr> 121 * <tr><td valign="top" headers="construct classes"><tt>[^abc]</tt></td> 122 * <td headers="matches">Any character except <tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (negation)</td></tr> 123 * <tr><td valign="top" headers="construct classes"><tt>[a-zA-Z]</tt></td> 124 * <td headers="matches"><tt>a</tt> through <tt>z</tt> 125 * or <tt>A</tt> through <tt>Z</tt>, inclusive (range)</td></tr> 126 * <tr><td valign="top" headers="construct classes"><tt>[a-d[m-p]]</tt></td> 127 * <td headers="matches"><tt>a</tt> through <tt>d</tt>, 128 * or <tt>m</tt> through <tt>p</tt>: <tt>[a-dm-p]</tt> (union)</td></tr> 129 * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[def]]</tt></td> 130 * <td headers="matches"><tt>d</tt>, <tt>e</tt>, or <tt>f</tt> (intersection)</tr> 131 * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^bc]]</tt></td> 132 * <td headers="matches"><tt>a</tt> through <tt>z</tt>, 133 * except for <tt>b</tt> and <tt>c</tt>: <tt>[ad-z]</tt> (subtraction)</td></tr> 134 * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^m-p]]</tt></td> 135 * <td headers="matches"><tt>a</tt> through <tt>z</tt>, 136 * and not <tt>m</tt> through <tt>p</tt>: <tt>[a-lq-z]</tt>(subtraction)</td></tr> 137 * <tr><th> </th></tr> 138 * 139 * <tr align="left"><th colspan="2" id="predef">Predefined character classes</th></tr> 140 * 141 * <tr><td valign="top" headers="construct predef"><tt>.</tt></td> 142 * <td headers="matches">Any character (may or may not match <a href="#lt">line terminators</a>)</td></tr> 143 * <tr><td valign="top" headers="construct predef"><tt>\d</tt></td> 144 * <td headers="matches">A digit: <tt>[0-9]</tt></td></tr> 145 * <tr><td valign="top" headers="construct predef"><tt>\D</tt></td> 146 * <td headers="matches">A non-digit: <tt>[^0-9]</tt></td></tr> 147 * <tr><td valign="top" headers="construct predef"><tt>\s</tt></td> 148 * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr> 149 * <tr><td valign="top" headers="construct predef"><tt>\S</tt></td> 150 * <td headers="matches">A non-whitespace character: <tt>[^\s]</tt></td></tr> 151 * <tr><td valign="top" headers="construct predef"><tt>\w</tt></td> 152 * <td headers="matches">A word character: <tt>[a-zA-Z_0-9]</tt></td></tr> 153 * <tr><td valign="top" headers="construct predef"><tt>\W</tt></td> 154 * <td headers="matches">A non-word character: <tt>[^\w]</tt></td></tr> 155 * 156 * <tr><th> </th></tr> 157 * <tr align="left"><th colspan="2" id="posix">POSIX character classes</b> (US-ASCII only)<b></th></tr> 158 * 159 * <tr><td valign="top" headers="construct posix"><tt>\p{Lower}</tt></td> 160 * <td headers="matches">A lower-case alphabetic character: <tt>[a-z]</tt></td></tr> 161 * <tr><td valign="top" headers="construct posix"><tt>\p{Upper}</tt></td> 162 * <td headers="matches">An upper-case alphabetic character:<tt>[A-Z]</tt></td></tr> 163 * <tr><td valign="top" headers="construct posix"><tt>\p{ASCII}</tt></td> 164 * <td headers="matches">All ASCII:<tt>[\x00-\x7F]</tt></td></tr> 165 * <tr><td valign="top" headers="construct posix"><tt>\p{Alpha}</tt></td> 166 * <td headers="matches">An alphabetic character:<tt>[\p{Lower}\p{Upper}]</tt></td></tr> 167 * <tr><td valign="top" headers="construct posix"><tt>\p{Digit}</tt></td> 168 * <td headers="matches">A decimal digit: <tt>[0-9]</tt></td></tr> 169 * <tr><td valign="top" headers="construct posix"><tt>\p{Alnum}</tt></td> 170 * <td headers="matches">An alphanumeric character:<tt>[\p{Alpha}\p{Digit}]</tt></td></tr> 171 * <tr><td valign="top" headers="construct posix"><tt>\p{Punct}</tt></td> 172 * <td headers="matches">Punctuation: One of <tt>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~</tt></td></tr> 173 * <!-- <tt>[\!"#\$%&'\(\)\*\+,\-\./:;\<=\>\?@\[\\\]\^_`\{\|\}~]</tt> 174 * <tt>[\X21-\X2F\X31-\X40\X5B-\X60\X7B-\X7E]</tt> --> 175 * <tr><td valign="top" headers="construct posix"><tt>\p{Graph}</tt></td> 176 * <td headers="matches">A visible character: <tt>[\p{Alnum}\p{Punct}]</tt></td></tr> 177 * <tr><td valign="top" headers="construct posix"><tt>\p{Print}</tt></td> 178 * <td headers="matches">A printable character: <tt>[\p{Graph}\x20]</tt></td></tr> 179 * <tr><td valign="top" headers="construct posix"><tt>\p{Blank}</tt></td> 180 * <td headers="matches">A space or a tab: <tt>[ \t]</tt></td></tr> 181 * <tr><td valign="top" headers="construct posix"><tt>\p{Cntrl}</tt></td> 182 * <td headers="matches">A control character: <tt>[\x00-\x1F\x7F]</tt></td></tr> 183 * <tr><td valign="top" headers="construct posix"><tt>\p{XDigit}</tt></td> 184 * <td headers="matches">A hexadecimal digit: <tt>[0-9a-fA-F]</tt></td></tr> 185 * <tr><td valign="top" headers="construct posix"><tt>\p{Space}</tt></td> 186 * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr> 187 * 188 * <tr><th> </th></tr> 189 * <tr align="left"><th colspan="2">java.lang.Character classes (simple <a href="#jcc">java character type</a>)</th></tr> 190 * 191 * <tr><td valign="top"><tt>\p{javaLowerCase}</tt></td> 192 * <td>Equivalent to java.lang.Character.isLowerCase()</td></tr> 193 * <tr><td valign="top"><tt>\p{javaUpperCase}</tt></td> 194 * <td>Equivalent to java.lang.Character.isUpperCase()</td></tr> 195 * <tr><td valign="top"><tt>\p{javaWhitespace}</tt></td> 196 * <td>Equivalent to java.lang.Character.isWhitespace()</td></tr> 197 * <tr><td valign="top"><tt>\p{javaMirrored}</tt></td> 198 * <td>Equivalent to java.lang.Character.isMirrored()</td></tr> 199 * 200 * <tr><th> </th></tr> 201 * <tr align="left"><th colspan="2" id="unicode">Classes for Unicode scripts, blocks, categories and binary properties</th></tr> 202 * * <tr><td valign="top" headers="construct unicode"><tt>\p{IsLatin}</tt></td> 203 * <td headers="matches">A Latin script character (<a href="#usc">script</a>)</td></tr> 204 * <tr><td valign="top" headers="construct unicode"><tt>\p{InGreek}</tt></td> 205 * <td headers="matches">A character in the Greek block (<a href="#ubc">block</a>)</td></tr> 206 * <tr><td valign="top" headers="construct unicode"><tt>\p{Lu}</tt></td> 207 * <td headers="matches">An uppercase letter (<a href="#ucc">category</a>)</td></tr> 208 * <tr><td valign="top" headers="construct unicode"><tt>\p{IsAlphabetic}</tt></td> 209 * <td headers="matches">An alphabetic character (<a href="#ubpc">binary property</a>)</td></tr> 210 * <tr><td valign="top" headers="construct unicode"><tt>\p{Sc}</tt></td> 211 * <td headers="matches">A currency symbol</td></tr> 212 * <tr><td valign="top" headers="construct unicode"><tt>\P{InGreek}</tt></td> 213 * <td headers="matches">Any character except one in the Greek block (negation)</td></tr> 214 * <tr><td valign="top" headers="construct unicode"><tt>[\p{L}&&[^\p{Lu}]] </tt></td> 215 * <td headers="matches">Any letter except an uppercase letter (subtraction)</td></tr> 216 * 217 * <tr><th> </th></tr> 218 * <tr align="left"><th colspan="2" id="bounds">Boundary matchers</th></tr> 219 * 220 * <tr><td valign="top" headers="construct bounds"><tt>^</tt></td> 221 * <td headers="matches">The beginning of a line</td></tr> 222 * <tr><td valign="top" headers="construct bounds"><tt>$</tt></td> 223 * <td headers="matches">The end of a line</td></tr> 224 * <tr><td valign="top" headers="construct bounds"><tt>\b</tt></td> 225 * <td headers="matches">A word boundary</td></tr> 226 * <tr><td valign="top" headers="construct bounds"><tt>\B</tt></td> 227 * <td headers="matches">A non-word boundary</td></tr> 228 * <tr><td valign="top" headers="construct bounds"><tt>\A</tt></td> 229 * <td headers="matches">The beginning of the input</td></tr> 230 * <tr><td valign="top" headers="construct bounds"><tt>\G</tt></td> 231 * <td headers="matches">The end of the previous match</td></tr> 232 * <tr><td valign="top" headers="construct bounds"><tt>\Z</tt></td> 233 * <td headers="matches">The end of the input but for the final 234 * <a href="#lt">terminator</a>, if any</td></tr> 235 * <tr><td valign="top" headers="construct bounds"><tt>\z</tt></td> 236 * <td headers="matches">The end of the input</td></tr> 237 * 238 * <tr><th> </th></tr> 239 * <tr align="left"><th colspan="2" id="greedy">Greedy quantifiers</th></tr> 240 * 241 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>?</tt></td> 242 * <td headers="matches"><i>X</i>, once or not at all</td></tr> 243 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>*</tt></td> 244 * <td headers="matches"><i>X</i>, zero or more times</td></tr> 245 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>+</tt></td> 246 * <td headers="matches"><i>X</i>, one or more times</td></tr> 247 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>}</tt></td> 248 * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr> 249 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,}</tt></td> 250 * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr> 251 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}</tt></td> 252 * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr> 253 * 254 * <tr><th> </th></tr> 255 * <tr align="left"><th colspan="2" id="reluc">Reluctant quantifiers</th></tr> 256 * 257 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>??</tt></td> 258 * <td headers="matches"><i>X</i>, once or not at all</td></tr> 259 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>*?</tt></td> 260 * <td headers="matches"><i>X</i>, zero or more times</td></tr> 261 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>+?</tt></td> 262 * <td headers="matches"><i>X</i>, one or more times</td></tr> 263 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>}?</tt></td> 264 * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr> 265 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,}?</tt></td> 266 * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr> 267 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}?</tt></td> 268 * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr> 269 * 270 * <tr><th> </th></tr> 271 * <tr align="left"><th colspan="2" id="poss">Possessive quantifiers</th></tr> 272 * 273 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>?+</tt></td> 274 * <td headers="matches"><i>X</i>, once or not at all</td></tr> 275 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>*+</tt></td> 276 * <td headers="matches"><i>X</i>, zero or more times</td></tr> 277 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>++</tt></td> 278 * <td headers="matches"><i>X</i>, one or more times</td></tr> 279 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>}+</tt></td> 280 * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr> 281 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,}+</tt></td> 282 * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr> 283 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}+</tt></td> 284 * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr> 285 * 286 * <tr><th> </th></tr> 287 * <tr align="left"><th colspan="2" id="logical">Logical operators</th></tr> 288 * 289 * <tr><td valign="top" headers="construct logical"><i>XY</i></td> 290 * <td headers="matches"><i>X</i> followed by <i>Y</i></td></tr> 291 * <tr><td valign="top" headers="construct logical"><i>X</i><tt>|</tt><i>Y</i></td> 292 * <td headers="matches">Either <i>X</i> or <i>Y</i></td></tr> 293 * <tr><td valign="top" headers="construct logical"><tt>(</tt><i>X</i><tt>)</tt></td> 294 * <td headers="matches">X, as a <a href="#cg">capturing group</a></td></tr> 295 * 296 * <tr><th> </th></tr> 297 * <tr align="left"><th colspan="2" id="backref">Back references</th></tr> 298 * 299 * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>n</i></td> 300 * <td valign="bottom" headers="matches">Whatever the <i>n</i><sup>th</sup> 301 * <a href="#cg">capturing group</a> matched</td></tr> 302 * 303 * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>k</i><<i>name</i>></td> 304 * <td valign="bottom" headers="matches">Whatever the 305 * <a href="#groupname">named-capturing group</a> "name" matched</td></tr> 306 * 307 * <tr><th> </th></tr> 308 * <tr align="left"><th colspan="2" id="quot">Quotation</th></tr> 309 * 310 * <tr><td valign="top" headers="construct quot"><tt>\</tt></td> 311 * <td headers="matches">Nothing, but quotes the following character</td></tr> 312 * <tr><td valign="top" headers="construct quot"><tt>\Q</tt></td> 313 * <td headers="matches">Nothing, but quotes all characters until <tt>\E</tt></td></tr> 314 * <tr><td valign="top" headers="construct quot"><tt>\E</tt></td> 315 * <td headers="matches">Nothing, but ends quoting started by <tt>\Q</tt></td></tr> 316 * <!-- Metachars: !$()*+.<>?[\]^{|} --> 317 * 318 * <tr><th> </th></tr> 319 * <tr align="left"><th colspan="2" id="special">Special constructs (named-capturing and non-capturing)</th></tr> 320 * 321 * <tr><td valign="top" headers="construct special"><tt>(?<<a href="#groupname">name</a>></tt><i>X</i><tt>)</tt></td> 322 * <td headers="matches"><i>X</i>, as a named-capturing group</td></tr> 323 * <tr><td valign="top" headers="construct special"><tt>(?:</tt><i>X</i><tt>)</tt></td> 324 * <td headers="matches"><i>X</i>, as a non-capturing group</td></tr> 325 * <tr><td valign="top" headers="construct special"><tt>(?idmsuxU-idmsuxU) </tt></td> 326 * <td headers="matches">Nothing, but turns match flags <a href="#CASE_INSENSITIVE">i</a> 327 * <a href="#UNIX_LINES">d</a> <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> 328 * <a href="#UNICODE_CASE">u</a> <a href="#COMMENTS">x</a> <a href="#UNICODE_CHARACTER_CLASS">U</a> 329 * on - off</td></tr> 330 * <tr><td valign="top" headers="construct special"><tt>(?idmsux-idmsux:</tt><i>X</i><tt>)</tt> </td> 331 * <td headers="matches"><i>X</i>, as a <a href="#cg">non-capturing group</a> with the 332 * given flags <a href="#CASE_INSENSITIVE">i</a> <a href="#UNIX_LINES">d</a> 333 * <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> <a href="#UNICODE_CASE">u</a > 334 * <a href="#COMMENTS">x</a> on - off</td></tr> 335 * <tr><td valign="top" headers="construct special"><tt>(?=</tt><i>X</i><tt>)</tt></td> 336 * <td headers="matches"><i>X</i>, via zero-width positive lookahead</td></tr> 337 * <tr><td valign="top" headers="construct special"><tt>(?!</tt><i>X</i><tt>)</tt></td> 338 * <td headers="matches"><i>X</i>, via zero-width negative lookahead</td></tr> 339 * <tr><td valign="top" headers="construct special"><tt>(?<=</tt><i>X</i><tt>)</tt></td> 340 * <td headers="matches"><i>X</i>, via zero-width positive lookbehind</td></tr> 341 * <tr><td valign="top" headers="construct special"><tt>(?<!</tt><i>X</i><tt>)</tt></td> 342 * <td headers="matches"><i>X</i>, via zero-width negative lookbehind</td></tr> 343 * <tr><td valign="top" headers="construct special"><tt>(?></tt><i>X</i><tt>)</tt></td> 344 * <td headers="matches"><i>X</i>, as an independent, non-capturing group</td></tr> 345 * 346 * </table> 347 * 348 * <hr> 349 * 350 * 351 * <a name="bs"> 352 * <h4> Backslashes, escapes, and quoting </h4> 353 * 354 * <p> The backslash character (<tt>'\'</tt>) serves to introduce escaped 355 * constructs, as defined in the table above, as well as to quote characters 356 * that otherwise would be interpreted as unescaped constructs. Thus the 357 * expression <tt>\\</tt> matches a single backslash and <tt>\{</tt> matches a 358 * left brace. 359 * 360 * <p> It is an error to use a backslash prior to any alphabetic character that 361 * does not denote an escaped construct; these are reserved for future 362 * extensions to the regular-expression language. A backslash may be used 363 * prior to a non-alphabetic character regardless of whether that character is 364 * part of an unescaped construct. 365 * 366 * <p> Backslashes within string literals in Java source code are interpreted 367 * as required by 368 * <cite>The Java™ Language Specification</cite> 369 * as either Unicode escapes (section 3.3) or other character escapes (section 3.10.6) 370 * It is therefore necessary to double backslashes in string 371 * literals that represent regular expressions to protect them from 372 * interpretation by the Java bytecode compiler. The string literal 373 * <tt>"\b"</tt>, for example, matches a single backspace character when 374 * interpreted as a regular expression, while <tt>"\\b"</tt> matches a 375 * word boundary. The string literal <tt>"\(hello\)"</tt> is illegal 376 * and leads to a compile-time error; in order to match the string 377 * <tt>(hello)</tt> the string literal <tt>"\\(hello\\)"</tt> 378 * must be used. 379 * 380 * <a name="cc"> 381 * <h4> Character Classes </h4> 382 * 383 * <p> Character classes may appear within other character classes, and 384 * may be composed by the union operator (implicit) and the intersection 385 * operator (<tt>&&</tt>). 386 * The union operator denotes a class that contains every character that is 387 * in at least one of its operand classes. The intersection operator 388 * denotes a class that contains every character that is in both of its 389 * operand classes. 390 * 391 * <p> The precedence of character-class operators is as follows, from 392 * highest to lowest: 393 * 394 * <blockquote><table border="0" cellpadding="1" cellspacing="0" 395 * summary="Precedence of character class operators."> 396 * <tr><th>1 </th> 397 * <td>Literal escape </td> 398 * <td><tt>\x</tt></td></tr> 399 * <tr><th>2 </th> 400 * <td>Grouping</td> 401 * <td><tt>[...]</tt></td></tr> 402 * <tr><th>3 </th> 403 * <td>Range</td> 404 * <td><tt>a-z</tt></td></tr> 405 * <tr><th>4 </th> 406 * <td>Union</td> 407 * <td><tt>[a-e][i-u]</tt></td></tr> 408 * <tr><th>5 </th> 409 * <td>Intersection</td> 410 * <td><tt>[a-z&&[aeiou]]</tt></td></tr> 411 * </table></blockquote> 412 * 413 * <p> Note that a different set of metacharacters are in effect inside 414 * a character class than outside a character class. For instance, the 415 * regular expression <tt>.</tt> loses its special meaning inside a 416 * character class, while the expression <tt>-</tt> becomes a range 417 * forming metacharacter. 418 * 419 * <a name="lt"> 420 * <h4> Line terminators </h4> 421 * 422 * <p> A <i>line terminator</i> is a one- or two-character sequence that marks 423 * the end of a line of the input character sequence. The following are 424 * recognized as line terminators: 425 * 426 * <ul> 427 * 428 * <li> A newline (line feed) character (<tt>'\n'</tt>), 429 * 430 * <li> A carriage-return character followed immediately by a newline 431 * character (<tt>"\r\n"</tt>), 432 * 433 * <li> A standalone carriage-return character (<tt>'\r'</tt>), 434 * 435 * <li> A next-line character (<tt>'\u0085'</tt>), 436 * 437 * <li> A line-separator character (<tt>'\u2028'</tt>), or 438 * 439 * <li> A paragraph-separator character (<tt>'\u2029</tt>). 440 * 441 * </ul> 442 * <p>If {@link #UNIX_LINES} mode is activated, then the only line terminators 443 * recognized are newline characters. 444 * 445 * <p> The regular expression <tt>.</tt> matches any character except a line 446 * terminator unless the {@link #DOTALL} flag is specified. 447 * 448 * <p> By default, the regular expressions <tt>^</tt> and <tt>$</tt> ignore 449 * line terminators and only match at the beginning and the end, respectively, 450 * of the entire input sequence. If {@link #MULTILINE} mode is activated then 451 * <tt>^</tt> matches at the beginning of input and after any line terminator 452 * except at the end of input. When in {@link #MULTILINE} mode <tt>$</tt> 453 * matches just before a line terminator or the end of the input sequence. 454 * 455 * <a name="cg"> 456 * <h4> Groups and capturing </h4> 457 * 458 * <a name="gnumber"> 459 * <h5> Group number </h5> 460 * <p> Capturing groups are numbered by counting their opening parentheses from 461 * left to right. In the expression <tt>((A)(B(C)))</tt>, for example, there 462 * are four such groups: </p> 463 * 464 * <blockquote><table cellpadding=1 cellspacing=0 summary="Capturing group numberings"> 465 * <tr><th>1 </th> 466 * <td><tt>((A)(B(C)))</tt></td></tr> 467 * <tr><th>2 </th> 468 * <td><tt>(A)</tt></td></tr> 469 * <tr><th>3 </th> 470 * <td><tt>(B(C))</tt></td></tr> 471 * <tr><th>4 </th> 472 * <td><tt>(C)</tt></td></tr> 473 * </table></blockquote> 474 * 475 * <p> Group zero always stands for the entire expression. 476 * 477 * <p> Capturing groups are so named because, during a match, each subsequence 478 * of the input sequence that matches such a group is saved. The captured 479 * subsequence may be used later in the expression, via a back reference, and 480 * may also be retrieved from the matcher once the match operation is complete. 481 * 482 * <a name="groupname"> 483 * <h5> Group name </h5> 484 * <p>A capturing group can also be assigned a "name", a <tt>named-capturing group</tt>, 485 * and then be back-referenced later by the "name". Group names are composed of 486 * the following characters. The first character must be a <tt>letter</tt>. 487 * 488 * <ul> 489 * <li> The uppercase letters <tt>'A'</tt> through <tt>'Z'</tt> 490 * (<tt>'\u0041'</tt> through <tt>'\u005a'</tt>), 491 * <li> The lowercase letters <tt>'a'</tt> through <tt>'z'</tt> 492 * (<tt>'\u0061'</tt> through <tt>'\u007a'</tt>), 493 * <li> The digits <tt>'0'</tt> through <tt>'9'</tt> 494 * (<tt>'\u0030'</tt> through <tt>'\u0039'</tt>), 495 * </ul> 496 * 497 * <p> A <tt>named-capturing group</tt> is still numbered as described in 498 * <a href="#gnumber">Group number</a>. 499 * 500 * <p> The captured input associated with a group is always the subsequence 501 * that the group most recently matched. If a group is evaluated a second time 502 * because of quantification then its previously-captured value, if any, will 503 * be retained if the second evaluation fails. Matching the string 504 * <tt>"aba"</tt> against the expression <tt>(a(b)?)+</tt>, for example, leaves 505 * group two set to <tt>"b"</tt>. All captured input is discarded at the 506 * beginning of each match. 507 * 508 * <p> Groups beginning with <tt>(?</tt> are either pure, <i>non-capturing</i> groups 509 * that do not capture text and do not count towards the group total, or 510 * <i>named-capturing</i> group. 511 * 512 * <h4> Unicode support </h4> 513 * 514 * <p> This class is in conformance with Level 1 of <a 515 * href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical 516 * Standard #18: Unicode Regular Expression</i></a>, plus RL2.1 517 * Canonical Equivalents. 518 * <p> 519 * <b>Unicode escape sequences</b> such as <tt>\u2014</tt> in Java source code 520 * are processed as described in section 3.3 of 521 * <cite>The Java™ Language Specification</cite>. 522 * Such escape sequences are also implemented directly by the regular-expression 523 * parser so that Unicode escapes can be used in expressions that are read from 524 * files or from the keyboard. Thus the strings <tt>"\u2014"</tt> and 525 * <tt>"\\u2014"</tt>, while not equal, compile into the same pattern, which 526 * matches the character with hexadecimal value <tt>0x2014</tt>. 527 * <p> 528 * A Unicode character can also be represented in a regular-expression by 529 * using its <b>Hex notation</b>(hexadecimal code point value) directly as described in construct 530 * <tt>\x{...}</tt>, for example a supplementary character U+2011F 531 * can be specified as <tt>\x{2011F}</tt>, instead of two consecutive 532 * Unicode escape sequences of the surrogate pair 533 * <tt>\uD840</tt><tt>\uDD1F</tt>. 534 * <p> 535 * Unicode scripts, blocks, categories and binary properties are written with 536 * the <tt>\p</tt> and <tt>\P</tt> constructs as in Perl. 537 * <tt>\p{</tt><i>prop</i><tt>}</tt> matches if 538 * the input has the property <i>prop</i>, while <tt>\P{</tt><i>prop</i><tt>}</tt> 539 * does not match if the input has that property. 540 * <p> 541 * Scripts, blocks, categories and binary properties can be used both inside 542 * and outside of a character class. 543 * <a name="usc"> 544 * <p> 545 * <b>Scripts</b> are specified either with the prefix {@code Is}, as in 546 * {@code IsHiragana}, or by using the {@code script} keyword (or its short 547 * form {@code sc})as in {@code script=Hiragana} or {@code sc=Hiragana}. 548 * <p> 549 * The script names supported by <code>Pattern</code> are the valid script names 550 * accepted and defined by 551 * {@link java.lang.Character.UnicodeScript#forName(String) UnicodeScript.forName}. 552 * <a name="ubc"> 553 * <p> 554 * <b>Blocks</b> are specified with the prefix {@code In}, as in 555 * {@code InMongolian}, or by using the keyword {@code block} (or its short 556 * form {@code blk}) as in {@code block=Mongolian} or {@code blk=Mongolian}. 557 * <p> 558 * The block names supported by <code>Pattern</code> are the valid block names 559 * accepted and defined by 560 * {@link java.lang.Character.UnicodeBlock#forName(String) UnicodeBlock.forName}. 561 * <p> 562 * <a name="ucc"> 563 * <b>Categories</b> may be specified with the optional prefix {@code Is}: 564 * Both {@code \p{L}} and {@code \p{IsL}} denote the category of Unicode 565 * letters. Same as scripts and blocks, categories can also be specified 566 * by using the keyword {@code general_category} (or its short form 567 * {@code gc}) as in {@code general_category=Lu} or {@code gc=Lu}. 568 * <p> 569 * The supported categories are those of 570 * <a href="http://www.unicode.org/unicode/standard/standard.html"> 571 * <i>The Unicode Standard</i></a> in the version specified by the 572 * {@link java.lang.Character Character} class. The category names are those 573 * defined in the Standard, both normative and informative. 574 * <p> 575 * <a name="ubpc"> 576 * <b>Binary properties</b> are specified with the prefix {@code Is}, as in 577 * {@code IsAlphabetic}. The supported binary properties by <code>Pattern</code> 578 * are 579 * <ul> 580 * <li> Alphabetic 581 * <li> Ideographic 582 * <li> Letter 583 * <li> Lowercase 584 * <li> Uppercase 585 * <li> Titlecase 586 * <li> Punctuation 587 * <Li> Control 588 * <li> White_Space 589 * <li> Digit 590 * <li> Hex_Digit 591 * <li> Noncharacter_Code_Point 592 * <li> Assigned 593 * </ul> 594 595 596 * <p> 597 * <b>Predefined Character classes</b> and <b>POSIX character classes</b> are in 598 * conformance with the recommendation of <i>Annex C: Compatibility Properties</i> 599 * of <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Regular Expression 600 * </i></a>. 601 * <p> 602 * <table border="0" cellpadding="1" cellspacing="0" 603 * summary="predefined and posix character classes in Unicode mode"> 604 * <tr align="left"> 605 * <th bgcolor="#CCCCFF" align="left" id="classes">Classes</th> 606 * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th> 607 *</tr> 608 * <tr><td><tt>\p{Lower}</tt></td> 609 * <td>A lowercase character:<tt>\p{IsLowercase}</tt></td></tr> 610 * <tr><td><tt>\p{Upper}</tt></td> 611 * <td>An uppercase character:<tt>\p{IsUppercase}</tt></td></tr> 612 * <tr><td><tt>\p{ASCII}</tt></td> 613 * <td>All ASCII:<tt>[\x00-\x7F]</tt></td></tr> 614 * <tr><td><tt>\p{Alpha}</tt></td> 615 * <td>An alphabetic character:<tt>\p{IsAlphabetic}</tt></td></tr> 616 * <tr><td><tt>\p{Digit}</tt></td> 617 * <td>A decimal digit character:<tt>p{IsDigit}</tt></td></tr> 618 * <tr><td><tt>\p{Alnum}</tt></td> 619 * <td>An alphanumeric character:<tt>[\p{IsAlphabetic}\p{IsDigit}]</tt></td></tr> 620 * <tr><td><tt>\p{Punct}</tt></td> 621 * <td>A punctuation character:<tt>p{IsPunctuation}</tt></td></tr> 622 * <tr><td><tt>\p{Graph}</tt></td> 623 * <td>A visible character: <tt>[^\p{IsWhite_Space}\p{gc=Cc}\p{gc=Cs}\p{gc=Cn}]</tt></td></tr> 624 * <tr><td><tt>\p{Print}</tt></td> 625 * <td>A printable character: <tt>[\p{Graph}\p{Blank}&&[^\p{Cntrl}]]</tt></td></tr> 626 * <tr><td><tt>\p{Blank}</tt></td> 627 * <td>A space or a tab: <tt>[\p{IsWhite_Space}&&[^\p{gc=Zl}\p{gc=Zp}\x0a\x0b\x0c\x0d\x85]]</tt></td></tr> 628 * <tr><td><tt>\p{Cntrl}</tt></td> 629 * <td>A control character: <tt>\p{gc=Cc}</tt></td></tr> 630 * <tr><td><tt>\p{XDigit}</tt></td> 631 * <td>A hexadecimal digit: <tt>[\p{gc=Nd}\p{IsHex_Digit}]</tt></td></tr> 632 * <tr><td><tt>\p{Space}</tt></td> 633 * <td>A whitespace character:<tt>\p{IsWhite_Space}</tt></td></tr> 634 * <tr><td><tt>\d</tt></td> 635 * <td>A digit: <tt>\p{IsDigit}</tt></td></tr> 636 * <tr><td><tt>\D</tt></td> 637 * <td>A non-digit: <tt>[^\d]</tt></td></tr> 638 * <tr><td><tt>\s</tt></td> 639 * <td>A whitespace character: <tt>\p{IsWhite_Space}</tt></td></tr> 640 * <tr><td><tt>\S</tt></td> 641 * <td>A non-whitespace character: <tt>[^\s]</tt></td></tr> 642 * <tr><td><tt>\w</tt></td> 643 * <td>A word character: <tt>[\p{Alpha}\p{gc=Mn}\p{gc=Me}\p{gc=Mc}\p{Digit}\p{gc=Pc}]</tt></td></tr> 644 * <tr><td><tt>\W</tt></td> 645 * <td>A non-word character: <tt>[^\w]</tt></td></tr> 646 * </table> 647 * <p> 648 * <a name="jcc"> 649 * Categories that behave like the java.lang.Character 650 * boolean is<i>methodname</i> methods (except for the deprecated ones) are 651 * available through the same <tt>\p{</tt><i>prop</i><tt>}</tt> syntax where 652 * the specified property has the name <tt>java<i>methodname</i></tt>. 653 * 654 * <h4> Comparison to Perl 5 </h4> 655 * 656 * <p>The <code>Pattern</code> engine performs traditional NFA-based matching 657 * with ordered alternation as occurs in Perl 5. 658 * 659 * <p> Perl constructs not supported by this class: </p> 660 * 661 * <ul> 662 * <li><p> Predefined character classes (Unicode character) 663 * <p><tt>\h </tt>A horizontal whitespace 664 * <p><tt>\H </tt>A non horizontal whitespace 665 * <p><tt>\v </tt>A vertical whitespace 666 * <p><tt>\V </tt>A non vertical whitespace 667 * <p><tt>\R </tt>Any Unicode linebreak sequence 668 * <tt>\u005cu000D\u005cu000A|[\u005cu000A\u005cu000B\u005cu000C\u005cu000D\u005cu0085\u005cu2028\u005cu2029]</tt> 669 * <p><tt>\X </tt>Match Unicode 670 * <a href="http://www.unicode.org/reports/tr18/#Default_Grapheme_Clusters"> 671 * <i>extended grapheme cluster</i></a> 672 * </p></li> 673 * 674 * <li><p> The backreference constructs, <tt>\g{</tt><i>n</i><tt>}</tt> for 675 * the <i>n</i><sup>th</sup><a href="#cg">capturing group</a> and 676 * <tt>\g{</tt><i>name</i><tt>}</tt> for 677 * <a href="#groupname">named-capturing group</a>. 678 * </p></li> 679 * 680 * <li><p> The named character construct, <tt>\N{</tt><i>name</i><tt>}</tt> 681 * for a Unicode character by its name. 682 * </p></li> 683 * 684 * <li><p> The conditional constructs 685 * <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>)</tt> and 686 * <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>|</tt><i>Y</i><tt>)</tt>, 687 * </p></li> 688 * 689 * <li><p> The embedded code constructs <tt>(?{</tt><i>code</i><tt>})</tt> 690 * and <tt>(??{</tt><i>code</i><tt>})</tt>,</p></li> 691 * 692 * <li><p> The embedded comment syntax <tt>(?#comment)</tt>, and </p></li> 693 * 694 * <li><p> The preprocessing operations <tt>\l</tt> <tt>\u</tt>, 695 * <tt>\L</tt>, and <tt>\U</tt>. </p></li> 696 * 697 * </ul> 698 * 699 * <p> Constructs supported by this class but not by Perl: </p> 700 * 701 * <ul> 702 * 703 * <li><p> Character-class union and intersection as described 704 * <a href="#cc">above</a>.</p></li> 705 * 706 * </ul> 707 * 708 * <p> Notable differences from Perl: </p> 709 * 710 * <ul> 711 * 712 * <li><p> In Perl, <tt>\1</tt> through <tt>\9</tt> are always interpreted 713 * as back references; a backslash-escaped number greater than <tt>9</tt> is 714 * treated as a back reference if at least that many subexpressions exist, 715 * otherwise it is interpreted, if possible, as an octal escape. In this 716 * class octal escapes must always begin with a zero. In this class, 717 * <tt>\1</tt> through <tt>\9</tt> are always interpreted as back 718 * references, and a larger number is accepted as a back reference if at 719 * least that many subexpressions exist at that point in the regular 720 * expression, otherwise the parser will drop digits until the number is 721 * smaller or equal to the existing number of groups or it is one digit. 722 * </p></li> 723 * 724 * <li><p> Perl uses the <tt>g</tt> flag to request a match that resumes 725 * where the last match left off. This functionality is provided implicitly 726 * by the {@link Matcher} class: Repeated invocations of the {@link 727 * Matcher#find find} method will resume where the last match left off, 728 * unless the matcher is reset. </p></li> 729 * 730 * <li><p> In Perl, embedded flags at the top level of an expression affect 731 * the whole expression. In this class, embedded flags always take effect 732 * at the point at which they appear, whether they are at the top level or 733 * within a group; in the latter case, flags are restored at the end of the 734 * group just as in Perl. </p></li> 735 * 736 * </ul> 737 * 738 * 739 * <p> For a more precise description of the behavior of regular expression 740 * constructs, please see <a href="http://www.oreilly.com/catalog/regex3/"> 741 * <i>Mastering Regular Expressions, 3nd Edition</i>, Jeffrey E. F. Friedl, 742 * O'Reilly and Associates, 2006.</a> 743 * </p> 744 * 745 * @see java.lang.String#split(String, int) 746 * @see java.lang.String#split(String) 747 * 748 * @author Mike McCloskey 749 * @author Mark Reinhold 750 * @author JSR-51 Expert Group 751 * @since 1.4 752 * @spec JSR-51 753 */ 754 755public final class Pattern implements java.io.Serializable 756{ 757 758 /** 759 * Regular expression modifier values. Instead of being passed as 760 * arguments, they can also be passed as inline modifiers. 761 * For example, the following statements have the same effect. 762 * <pre> 763 * RegExp r1 = RegExp.compile("abc", Pattern.I|Pattern.M); 764 * RegExp r2 = RegExp.compile("(?im)abc", 0); 765 * </pre> 766 * 767 * The flags are duplicated so that the familiar Perl match flag 768 * names are available. 769 */ 770 771 /** 772 * Enables Unix lines mode. 773 * 774 * <p> In this mode, only the <tt>'\n'</tt> line terminator is recognized 775 * in the behavior of <tt>.</tt>, <tt>^</tt>, and <tt>$</tt>. 776 * 777 * <p> Unix lines mode can also be enabled via the embedded flag 778 * expression <tt>(?d)</tt>. 779 */ 780 public static final int UNIX_LINES = 0x01; 781 782 /** 783 * Enables case-insensitive matching. 784 * 785 * <p> By default, case-insensitive matching assumes that only characters 786 * in the US-ASCII charset are being matched. Unicode-aware 787 * case-insensitive matching can be enabled by specifying the {@link 788 * #UNICODE_CASE} flag in conjunction with this flag. 789 * 790 * <p> Case-insensitive matching can also be enabled via the embedded flag 791 * expression <tt>(?i)</tt>. 792 * 793 * <p> Specifying this flag may impose a slight performance penalty. </p> 794 */ 795 public static final int CASE_INSENSITIVE = 0x02; 796 797 /** 798 * Permits whitespace and comments in pattern. 799 * 800 * <p> In this mode, whitespace is ignored, and embedded comments starting 801 * with <tt>#</tt> are ignored until the end of a line. 802 * 803 * <p> Comments mode can also be enabled via the embedded flag 804 * expression <tt>(?x)</tt>. 805 */ 806 public static final int COMMENTS = 0x04; 807 808 /** 809 * Enables multiline mode. 810 * 811 * <p> In multiline mode the expressions <tt>^</tt> and <tt>$</tt> match 812 * just after or just before, respectively, a line terminator or the end of 813 * the input sequence. By default these expressions only match at the 814 * beginning and the end of the entire input sequence. 815 * 816 * <p> Multiline mode can also be enabled via the embedded flag 817 * expression <tt>(?m)</tt>. </p> 818 */ 819 public static final int MULTILINE = 0x08; 820 821 /** 822 * Enables literal parsing of the pattern. 823 * 824 * <p> When this flag is specified then the input string that specifies 825 * the pattern is treated as a sequence of literal characters. 826 * Metacharacters or escape sequences in the input sequence will be 827 * given no special meaning. 828 * 829 * <p>The flags CASE_INSENSITIVE and UNICODE_CASE retain their impact on 830 * matching when used in conjunction with this flag. The other flags 831 * become superfluous. 832 * 833 * <p> There is no embedded flag character for enabling literal parsing. 834 * @since 1.5 835 */ 836 public static final int LITERAL = 0x10; 837 838 /** 839 * Enables dotall mode. 840 * 841 * <p> In dotall mode, the expression <tt>.</tt> matches any character, 842 * including a line terminator. By default this expression does not match 843 * line terminators. 844 * 845 * <p> Dotall mode can also be enabled via the embedded flag 846 * expression <tt>(?s)</tt>. (The <tt>s</tt> is a mnemonic for 847 * "single-line" mode, which is what this is called in Perl.) </p> 848 */ 849 public static final int DOTALL = 0x20; 850 851 /** 852 * Enables Unicode-aware case folding. 853 * 854 * <p> When this flag is specified then case-insensitive matching, when 855 * enabled by the {@link #CASE_INSENSITIVE} flag, is done in a manner 856 * consistent with the Unicode Standard. By default, case-insensitive 857 * matching assumes that only characters in the US-ASCII charset are being 858 * matched. 859 * 860 * <p> Unicode-aware case folding can also be enabled via the embedded flag 861 * expression <tt>(?u)</tt>. 862 * 863 * <p> Specifying this flag may impose a performance penalty. </p> 864 */ 865 public static final int UNICODE_CASE = 0x40; 866 867 /** 868 * Enables canonical equivalence. 869 * 870 * <p> When this flag is specified then two characters will be considered 871 * to match if, and only if, their full canonical decompositions match. 872 * The expression <tt>"a\u030A"</tt>, for example, will match the 873 * string <tt>"\u00E5"</tt> when this flag is specified. By default, 874 * matching does not take canonical equivalence into account. 875 * 876 * <p> There is no embedded flag character for enabling canonical 877 * equivalence. 878 * 879 * <p> Specifying this flag may impose a performance penalty. </p> 880 */ 881 public static final int CANON_EQ = 0x80; 882 883 /** 884 * Enables the Unicode version of <i>Predefined character classes</i> and 885 * <i>POSIX character classes</i> as eefined by <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical 886 * Standard #18: Unicode Regular Expression</i></a> 887 * <i>Annex C: Compatibility Properties</i>. 888 * <p> 889 * 890 * This flag has no effect on Android, unicode character classes are always 891 * used. 892 * 893 * @since 1.7 894 */ 895 public static final int UNICODE_CHARACTER_CLASS = 0x100; 896 897 /* Pattern has only two serialized components: The pattern string 898 * and the flags, which are all that is needed to recompile the pattern 899 * when it is deserialized. 900 */ 901 902 /** use serialVersionUID from Merlin b59 for interoperability */ 903 private static final long serialVersionUID = 5073258162644648461L; 904 905 /** 906 * The original regular-expression pattern string. 907 * 908 * @serial 909 */ 910 private final String pattern; 911 912 /** 913 * The original pattern flags. 914 * 915 * @serial 916 */ 917 private final int flags; 918 919 transient long address; 920 921 922 /** 923 * Compiles the given regular expression into a pattern. </p> 924 * 925 * @param regex 926 * The expression to be compiled 927 * 928 * @throws PatternSyntaxException 929 * If the expression's syntax is invalid 930 */ 931 public static Pattern compile(String regex) { 932 return new Pattern(regex, 0); 933 } 934 935 /** 936 * Compiles the given regular expression into a pattern with the given 937 * flags. </p> 938 * 939 * @param regex 940 * The expression to be compiled 941 * 942 * @param flags 943 * Match flags, a bit mask that may include 944 * {@link #CASE_INSENSITIVE}, {@link #MULTILINE}, {@link #DOTALL}, 945 * {@link #UNICODE_CASE}, {@link #CANON_EQ}, {@link #UNIX_LINES}, 946 * {@link #LITERAL}, {@link #UNICODE_CHARACTER_CLASS} 947 * and {@link #COMMENTS} 948 * 949 * @throws IllegalArgumentException 950 * If bit values other than those corresponding to the defined 951 * match flags are set in <tt>flags</tt> 952 * 953 * @throws PatternSyntaxException 954 * If the expression's syntax is invalid 955 */ 956 public static Pattern compile(String regex, int flags) throws PatternSyntaxException { 957 return new Pattern(regex, flags); 958 } 959 960 /** 961 * Returns the regular expression from which this pattern was compiled. 962 * </p> 963 * 964 * @return The source of this pattern 965 */ 966 public String pattern() { 967 return pattern; 968 } 969 970 /** 971 * <p>Returns the string representation of this pattern. This 972 * is the regular expression from which this pattern was 973 * compiled.</p> 974 * 975 * @return The string representation of this pattern 976 * @since 1.5 977 */ 978 public String toString() { 979 return pattern; 980 } 981 982 /** 983 * Creates a matcher that will match the given input against this pattern. 984 * </p> 985 * 986 * @param input 987 * The character sequence to be matched 988 * 989 * @return A new matcher for this pattern 990 */ 991 public Matcher matcher(CharSequence input) { 992 Matcher m = new Matcher(this, input); 993 return m; 994 } 995 996 /** 997 * Returns this pattern's match flags. </p> 998 * 999 * @return The match flags specified when this pattern was compiled 1000 */ 1001 public int flags() { 1002 return flags; 1003 } 1004 1005 /** 1006 * Compiles the given regular expression and attempts to match the given 1007 * input against it. 1008 * 1009 * <p> An invocation of this convenience method of the form 1010 * 1011 * <blockquote><pre> 1012 * Pattern.matches(regex, input);</pre></blockquote> 1013 * 1014 * behaves in exactly the same way as the expression 1015 * 1016 * <blockquote><pre> 1017 * Pattern.compile(regex).matcher(input).matches()</pre></blockquote> 1018 * 1019 * <p> If a pattern is to be used multiple times, compiling it once and reusing 1020 * it will be more efficient than invoking this method each time. </p> 1021 * 1022 * @param regex 1023 * The expression to be compiled 1024 * 1025 * @param input 1026 * The character sequence to be matched 1027 * 1028 * @throws PatternSyntaxException 1029 * If the expression's syntax is invalid 1030 */ 1031 public static boolean matches(String regex, CharSequence input) { 1032 Pattern p = Pattern.compile(regex); 1033 Matcher m = p.matcher(input); 1034 return m.matches(); 1035 } 1036 1037 /** 1038 * Splits the given input sequence around matches of this pattern. 1039 * 1040 * <p> The array returned by this method contains each substring of the 1041 * input sequence that is terminated by another subsequence that matches 1042 * this pattern or is terminated by the end of the input sequence. The 1043 * substrings in the array are in the order in which they occur in the 1044 * input. If this pattern does not match any subsequence of the input then 1045 * the resulting array has just one element, namely the input sequence in 1046 * string form. 1047 * 1048 * <p> The <tt>limit</tt> parameter controls the number of times the 1049 * pattern is applied and therefore affects the length of the resulting 1050 * array. If the limit <i>n</i> is greater than zero then the pattern 1051 * will be applied at most <i>n</i> - 1 times, the array's 1052 * length will be no greater than <i>n</i>, and the array's last entry 1053 * will contain all input beyond the last matched delimiter. If <i>n</i> 1054 * is non-positive then the pattern will be applied as many times as 1055 * possible and the array can have any length. If <i>n</i> is zero then 1056 * the pattern will be applied as many times as possible, the array can 1057 * have any length, and trailing empty strings will be discarded. 1058 * 1059 * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following 1060 * results with these parameters: 1061 * 1062 * <blockquote><table cellpadding=1 cellspacing=0 1063 * summary="Split examples showing regex, limit, and result"> 1064 * <tr><th><P align="left"><i>Regex </i></th> 1065 * <th><P align="left"><i>Limit </i></th> 1066 * <th><P align="left"><i>Result </i></th></tr> 1067 * <tr><td align=center>:</td> 1068 * <td align=center>2</td> 1069 * <td><tt>{ "boo", "and:foo" }</tt></td></tr> 1070 * <tr><td align=center>:</td> 1071 * <td align=center>5</td> 1072 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr> 1073 * <tr><td align=center>:</td> 1074 * <td align=center>-2</td> 1075 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr> 1076 * <tr><td align=center>o</td> 1077 * <td align=center>5</td> 1078 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr> 1079 * <tr><td align=center>o</td> 1080 * <td align=center>-2</td> 1081 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr> 1082 * <tr><td align=center>o</td> 1083 * <td align=center>0</td> 1084 * <td><tt>{ "b", "", ":and:f" }</tt></td></tr> 1085 * </table></blockquote> 1086 * 1087 * 1088 * @param input 1089 * The character sequence to be split 1090 * 1091 * @param limit 1092 * The result threshold, as described above 1093 * 1094 * @return The array of strings computed by splitting the input 1095 * around matches of this pattern 1096 */ 1097 public String[] split(CharSequence input, int limit) { 1098 int index = 0; 1099 boolean matchLimited = limit > 0; 1100 ArrayList<String> matchList = new ArrayList<>(); 1101 Matcher m = matcher(input); 1102 1103 // Add segments before each match found 1104 while(m.find()) { 1105 if (!matchLimited || matchList.size() < limit - 1) { 1106 String match = input.subSequence(index, m.start()).toString(); 1107 matchList.add(match); 1108 index = m.end(); 1109 } else if (matchList.size() == limit - 1) { // last one 1110 String match = input.subSequence(index, 1111 input.length()).toString(); 1112 matchList.add(match); 1113 index = m.end(); 1114 } 1115 } 1116 1117 // If no match was found, return this 1118 if (index == 0) 1119 return new String[] {input.toString()}; 1120 1121 // Add remaining segment 1122 if (!matchLimited || matchList.size() < limit) 1123 matchList.add(input.subSequence(index, input.length()).toString()); 1124 1125 // Construct result 1126 int resultSize = matchList.size(); 1127 if (limit == 0) 1128 while (resultSize > 0 && matchList.get(resultSize-1).equals("")) 1129 resultSize--; 1130 String[] result = new String[resultSize]; 1131 return matchList.subList(0, resultSize).toArray(result); 1132 } 1133 1134 /** 1135 * Splits the given input sequence around matches of this pattern. 1136 * 1137 * <p> This method works as if by invoking the two-argument {@link 1138 * #split(java.lang.CharSequence, int) split} method with the given input 1139 * sequence and a limit argument of zero. Trailing empty strings are 1140 * therefore not included in the resulting array. </p> 1141 * 1142 * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following 1143 * results with these expressions: 1144 * 1145 * <blockquote><table cellpadding=1 cellspacing=0 1146 * summary="Split examples showing regex and result"> 1147 * <tr><th><P align="left"><i>Regex </i></th> 1148 * <th><P align="left"><i>Result</i></th></tr> 1149 * <tr><td align=center>:</td> 1150 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr> 1151 * <tr><td align=center>o</td> 1152 * <td><tt>{ "b", "", ":and:f" }</tt></td></tr> 1153 * </table></blockquote> 1154 * 1155 * 1156 * @param input 1157 * The character sequence to be split 1158 * 1159 * @return The array of strings computed by splitting the input 1160 * around matches of this pattern 1161 */ 1162 public String[] split(CharSequence input) { 1163 return split(input, 0); 1164 } 1165 1166 /** 1167 * Returns a literal pattern <code>String</code> for the specified 1168 * <code>String</code>. 1169 * 1170 * <p>This method produces a <code>String</code> that can be used to 1171 * create a <code>Pattern</code> that would match the string 1172 * <code>s</code> as if it were a literal pattern.</p> Metacharacters 1173 * or escape sequences in the input sequence will be given no special 1174 * meaning. 1175 * 1176 * @param s The string to be literalized 1177 * @return A literal string replacement 1178 * @since 1.5 1179 */ 1180 public static String quote(String s) { 1181 int slashEIndex = s.indexOf("\\E"); 1182 if (slashEIndex == -1) 1183 return "\\Q" + s + "\\E"; 1184 1185 StringBuilder sb = new StringBuilder(s.length() * 2); 1186 sb.append("\\Q"); 1187 slashEIndex = 0; 1188 int current = 0; 1189 while ((slashEIndex = s.indexOf("\\E", current)) != -1) { 1190 sb.append(s.substring(current, slashEIndex)); 1191 current = slashEIndex + 2; 1192 sb.append("\\E\\\\E\\Q"); 1193 } 1194 sb.append(s.substring(current, s.length())); 1195 sb.append("\\E"); 1196 return sb.toString(); 1197 } 1198 1199 /** 1200 * Recompile the Pattern instance from a stream. The original pattern 1201 * string is read in and the object tree is recompiled from it. 1202 */ 1203 private void readObject(java.io.ObjectInputStream s) 1204 throws java.io.IOException, ClassNotFoundException { 1205 1206 // Read in all fields 1207 s.defaultReadObject(); 1208 compile(); 1209 } 1210 1211 /** 1212 * This private constructor is used to create all Patterns. The pattern 1213 * string and match flags are all that is needed to completely describe 1214 * a Pattern. 1215 */ 1216 private Pattern(String p, int f) { 1217 if ((f & CANON_EQ) != 0) { 1218 throw new UnsupportedOperationException("CANON_EQ flag not supported"); 1219 } 1220 int supportedFlags = CASE_INSENSITIVE | COMMENTS | DOTALL | LITERAL | MULTILINE | UNICODE_CASE | UNIX_LINES; 1221 if ((f & ~supportedFlags) != 0) { 1222 throw new IllegalArgumentException("Unsupported flags: " + (f & ~supportedFlags)); 1223 } 1224 this.pattern = p; 1225 this.flags = f; 1226 compile(); 1227 } 1228 1229 private void compile() throws PatternSyntaxException { 1230 if (pattern == null) { 1231 throw new NullPointerException("pattern == null"); 1232 } 1233 1234 String icuPattern = pattern; 1235 if ((flags & LITERAL) != 0) { 1236 icuPattern = quote(pattern); 1237 } 1238 1239 // These are the flags natively supported by ICU. 1240 // They even have the same value in native code. 1241 int icuFlags = flags & (CASE_INSENSITIVE | COMMENTS | MULTILINE | DOTALL | UNIX_LINES); 1242 address = compileImpl(icuPattern, icuFlags); 1243 } 1244 1245 1246 @Override 1247 protected void finalize() throws Throwable { 1248 try { 1249 closeImpl(address); 1250 } finally { 1251 super.finalize(); 1252 } 1253 } 1254 1255 private static native void closeImpl(long addr); 1256 private static native long compileImpl(String regex, int flags); 1257} 1258