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