PageRenderTime 66ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 1ms

/java-1.7.0-openjdk/openjdk/jdk/src/share/classes/java/util/regex/Pattern.java

#
Java | 5648 lines | 3493 code | 292 blank | 1863 comment | 904 complexity | 6489a95a7f4c9de11ec05042f9397af3 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  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. package java.util.regex;
  26. import java.security.AccessController;
  27. import java.security.PrivilegedAction;
  28. import java.text.CharacterIterator;
  29. import java.text.Normalizer;
  30. import java.util.Locale;
  31. import java.util.Map;
  32. import java.util.ArrayList;
  33. import java.util.HashMap;
  34. import java.util.Arrays;
  35. /**
  36. * A compiled representation of a regular expression.
  37. *
  38. * <p> A regular expression, specified as a string, must first be compiled into
  39. * an instance of this class. The resulting pattern can then be used to create
  40. * a {@link Matcher} object that can match arbitrary {@link
  41. * java.lang.CharSequence </code>character sequences<code>} against the regular
  42. * expression. All of the state involved in performing a match resides in the
  43. * matcher, so many matchers can share the same pattern.
  44. *
  45. * <p> A typical invocation sequence is thus
  46. *
  47. * <blockquote><pre>
  48. * Pattern p = Pattern.{@link #compile compile}("a*b");
  49. * Matcher m = p.{@link #matcher matcher}("aaaaab");
  50. * boolean b = m.{@link Matcher#matches matches}();</pre></blockquote>
  51. *
  52. * <p> A {@link #matches matches} method is defined by this class as a
  53. * convenience for when a regular expression is used just once. This method
  54. * compiles an expression and matches an input sequence against it in a single
  55. * invocation. The statement
  56. *
  57. * <blockquote><pre>
  58. * boolean b = Pattern.matches("a*b", "aaaaab");</pre></blockquote>
  59. *
  60. * is equivalent to the three statements above, though for repeated matches it
  61. * is less efficient since it does not allow the compiled pattern to be reused.
  62. *
  63. * <p> Instances of this class are immutable and are safe for use by multiple
  64. * concurrent threads. Instances of the {@link Matcher} class are not safe for
  65. * such use.
  66. *
  67. *
  68. * <a name="sum">
  69. * <h4> Summary of regular-expression constructs </h4>
  70. *
  71. * <table border="0" cellpadding="1" cellspacing="0"
  72. * summary="Regular expression constructs, and what they match">
  73. *
  74. * <tr align="left">
  75. * <th bgcolor="#CCCCFF" align="left" id="construct">Construct</th>
  76. * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
  77. * </tr>
  78. *
  79. * <tr><th>&nbsp;</th></tr>
  80. * <tr align="left"><th colspan="2" id="characters">Characters</th></tr>
  81. *
  82. * <tr><td valign="top" headers="construct characters"><i>x</i></td>
  83. * <td headers="matches">The character <i>x</i></td></tr>
  84. * <tr><td valign="top" headers="construct characters"><tt>\\</tt></td>
  85. * <td headers="matches">The backslash character</td></tr>
  86. * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>n</i></td>
  87. * <td headers="matches">The character with octal value <tt>0</tt><i>n</i>
  88. * (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
  89. * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>nn</i></td>
  90. * <td headers="matches">The character with octal value <tt>0</tt><i>nn</i>
  91. * (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
  92. * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>mnn</i></td>
  93. * <td headers="matches">The character with octal value <tt>0</tt><i>mnn</i>
  94. * (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>m</i>&nbsp;<tt>&lt;=</tt>&nbsp;3,
  95. * 0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
  96. * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>hh</i></td>
  97. * <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>hh</i></td></tr>
  98. * <tr><td valign="top" headers="construct characters"><tt>&#92;u</tt><i>hhhh</i></td>
  99. * <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>hhhh</i></td></tr>
  100. * <tr><td valign="top" headers="construct characters"><tt>&#92;x</tt><i>{h...h}</i></td>
  101. * <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>h...h</i>
  102. * ({@link java.lang.Character#MIN_CODE_POINT Character.MIN_CODE_POINT}
  103. * &nbsp;&lt;=&nbsp;<tt>0x</tt><i>h...h</i>&nbsp;&lt;=&nbsp
  104. * {@link java.lang.Character#MAX_CODE_POINT Character.MAX_CODE_POINT})</td></tr>
  105. * <tr><td valign="top" headers="matches"><tt>\t</tt></td>
  106. * <td headers="matches">The tab character (<tt>'&#92;u0009'</tt>)</td></tr>
  107. * <tr><td valign="top" headers="construct characters"><tt>\n</tt></td>
  108. * <td headers="matches">The newline (line feed) character (<tt>'&#92;u000A'</tt>)</td></tr>
  109. * <tr><td valign="top" headers="construct characters"><tt>\r</tt></td>
  110. * <td headers="matches">The carriage-return character (<tt>'&#92;u000D'</tt>)</td></tr>
  111. * <tr><td valign="top" headers="construct characters"><tt>\f</tt></td>
  112. * <td headers="matches">The form-feed character (<tt>'&#92;u000C'</tt>)</td></tr>
  113. * <tr><td valign="top" headers="construct characters"><tt>\a</tt></td>
  114. * <td headers="matches">The alert (bell) character (<tt>'&#92;u0007'</tt>)</td></tr>
  115. * <tr><td valign="top" headers="construct characters"><tt>\e</tt></td>
  116. * <td headers="matches">The escape character (<tt>'&#92;u001B'</tt>)</td></tr>
  117. * <tr><td valign="top" headers="construct characters"><tt>\c</tt><i>x</i></td>
  118. * <td headers="matches">The control character corresponding to <i>x</i></td></tr>
  119. *
  120. * <tr><th>&nbsp;</th></tr>
  121. * <tr align="left"><th colspan="2" id="classes">Character classes</th></tr>
  122. *
  123. * <tr><td valign="top" headers="construct classes"><tt>[abc]</tt></td>
  124. * <td headers="matches"><tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (simple class)</td></tr>
  125. * <tr><td valign="top" headers="construct classes"><tt>[^abc]</tt></td>
  126. * <td headers="matches">Any character except <tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (negation)</td></tr>
  127. * <tr><td valign="top" headers="construct classes"><tt>[a-zA-Z]</tt></td>
  128. * <td headers="matches"><tt>a</tt> through <tt>z</tt>
  129. * or <tt>A</tt> through <tt>Z</tt>, inclusive (range)</td></tr>
  130. * <tr><td valign="top" headers="construct classes"><tt>[a-d[m-p]]</tt></td>
  131. * <td headers="matches"><tt>a</tt> through <tt>d</tt>,
  132. * or <tt>m</tt> through <tt>p</tt>: <tt>[a-dm-p]</tt> (union)</td></tr>
  133. * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[def]]</tt></td>
  134. * <td headers="matches"><tt>d</tt>, <tt>e</tt>, or <tt>f</tt> (intersection)</tr>
  135. * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^bc]]</tt></td>
  136. * <td headers="matches"><tt>a</tt> through <tt>z</tt>,
  137. * except for <tt>b</tt> and <tt>c</tt>: <tt>[ad-z]</tt> (subtraction)</td></tr>
  138. * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^m-p]]</tt></td>
  139. * <td headers="matches"><tt>a</tt> through <tt>z</tt>,
  140. * and not <tt>m</tt> through <tt>p</tt>: <tt>[a-lq-z]</tt>(subtraction)</td></tr>
  141. * <tr><th>&nbsp;</th></tr>
  142. *
  143. * <tr align="left"><th colspan="2" id="predef">Predefined character classes</th></tr>
  144. *
  145. * <tr><td valign="top" headers="construct predef"><tt>.</tt></td>
  146. * <td headers="matches">Any character (may or may not match <a href="#lt">line terminators</a>)</td></tr>
  147. * <tr><td valign="top" headers="construct predef"><tt>\d</tt></td>
  148. * <td headers="matches">A digit: <tt>[0-9]</tt></td></tr>
  149. * <tr><td valign="top" headers="construct predef"><tt>\D</tt></td>
  150. * <td headers="matches">A non-digit: <tt>[^0-9]</tt></td></tr>
  151. * <tr><td valign="top" headers="construct predef"><tt>\s</tt></td>
  152. * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
  153. * <tr><td valign="top" headers="construct predef"><tt>\S</tt></td>
  154. * <td headers="matches">A non-whitespace character: <tt>[^\s]</tt></td></tr>
  155. * <tr><td valign="top" headers="construct predef"><tt>\w</tt></td>
  156. * <td headers="matches">A word character: <tt>[a-zA-Z_0-9]</tt></td></tr>
  157. * <tr><td valign="top" headers="construct predef"><tt>\W</tt></td>
  158. * <td headers="matches">A non-word character: <tt>[^\w]</tt></td></tr>
  159. *
  160. * <tr><th>&nbsp;</th></tr>
  161. * <tr align="left"><th colspan="2" id="posix">POSIX character classes</b> (US-ASCII only)<b></th></tr>
  162. *
  163. * <tr><td valign="top" headers="construct posix"><tt>\p{Lower}</tt></td>
  164. * <td headers="matches">A lower-case alphabetic character: <tt>[a-z]</tt></td></tr>
  165. * <tr><td valign="top" headers="construct posix"><tt>\p{Upper}</tt></td>
  166. * <td headers="matches">An upper-case alphabetic character:<tt>[A-Z]</tt></td></tr>
  167. * <tr><td valign="top" headers="construct posix"><tt>\p{ASCII}</tt></td>
  168. * <td headers="matches">All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
  169. * <tr><td valign="top" headers="construct posix"><tt>\p{Alpha}</tt></td>
  170. * <td headers="matches">An alphabetic character:<tt>[\p{Lower}\p{Upper}]</tt></td></tr>
  171. * <tr><td valign="top" headers="construct posix"><tt>\p{Digit}</tt></td>
  172. * <td headers="matches">A decimal digit: <tt>[0-9]</tt></td></tr>
  173. * <tr><td valign="top" headers="construct posix"><tt>\p{Alnum}</tt></td>
  174. * <td headers="matches">An alphanumeric character:<tt>[\p{Alpha}\p{Digit}]</tt></td></tr>
  175. * <tr><td valign="top" headers="construct posix"><tt>\p{Punct}</tt></td>
  176. * <td headers="matches">Punctuation: One of <tt>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~</tt></td></tr>
  177. * <!-- <tt>[\!"#\$%&'\(\)\*\+,\-\./:;\<=\>\?@\[\\\]\^_`\{\|\}~]</tt>
  178. * <tt>[\X21-\X2F\X31-\X40\X5B-\X60\X7B-\X7E]</tt> -->
  179. * <tr><td valign="top" headers="construct posix"><tt>\p{Graph}</tt></td>
  180. * <td headers="matches">A visible character: <tt>[\p{Alnum}\p{Punct}]</tt></td></tr>
  181. * <tr><td valign="top" headers="construct posix"><tt>\p{Print}</tt></td>
  182. * <td headers="matches">A printable character: <tt>[\p{Graph}\x20]</tt></td></tr>
  183. * <tr><td valign="top" headers="construct posix"><tt>\p{Blank}</tt></td>
  184. * <td headers="matches">A space or a tab: <tt>[ \t]</tt></td></tr>
  185. * <tr><td valign="top" headers="construct posix"><tt>\p{Cntrl}</tt></td>
  186. * <td headers="matches">A control character: <tt>[\x00-\x1F\x7F]</tt></td></tr>
  187. * <tr><td valign="top" headers="construct posix"><tt>\p{XDigit}</tt></td>
  188. * <td headers="matches">A hexadecimal digit: <tt>[0-9a-fA-F]</tt></td></tr>
  189. * <tr><td valign="top" headers="construct posix"><tt>\p{Space}</tt></td>
  190. * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
  191. *
  192. * <tr><th>&nbsp;</th></tr>
  193. * <tr align="left"><th colspan="2">java.lang.Character classes (simple <a href="#jcc">java character type</a>)</th></tr>
  194. *
  195. * <tr><td valign="top"><tt>\p{javaLowerCase}</tt></td>
  196. * <td>Equivalent to java.lang.Character.isLowerCase()</td></tr>
  197. * <tr><td valign="top"><tt>\p{javaUpperCase}</tt></td>
  198. * <td>Equivalent to java.lang.Character.isUpperCase()</td></tr>
  199. * <tr><td valign="top"><tt>\p{javaWhitespace}</tt></td>
  200. * <td>Equivalent to java.lang.Character.isWhitespace()</td></tr>
  201. * <tr><td valign="top"><tt>\p{javaMirrored}</tt></td>
  202. * <td>Equivalent to java.lang.Character.isMirrored()</td></tr>
  203. *
  204. * <tr><th>&nbsp;</th></tr>
  205. * <tr align="left"><th colspan="2" id="unicode">Classes for Unicode scripts, blocks, categories and binary properties</th></tr>
  206. * * <tr><td valign="top" headers="construct unicode"><tt>\p{IsLatin}</tt></td>
  207. * <td headers="matches">A Latin&nbsp;script character (<a href="#usc">script</a>)</td></tr>
  208. * <tr><td valign="top" headers="construct unicode"><tt>\p{InGreek}</tt></td>
  209. * <td headers="matches">A character in the Greek&nbsp;block (<a href="#ubc">block</a>)</td></tr>
  210. * <tr><td valign="top" headers="construct unicode"><tt>\p{Lu}</tt></td>
  211. * <td headers="matches">An uppercase letter (<a href="#ucc">category</a>)</td></tr>
  212. * <tr><td valign="top" headers="construct unicode"><tt>\p{IsAlphabetic}</tt></td>
  213. * <td headers="matches">An alphabetic character (<a href="#ubpc">binary property</a>)</td></tr>
  214. * <tr><td valign="top" headers="construct unicode"><tt>\p{Sc}</tt></td>
  215. * <td headers="matches">A currency symbol</td></tr>
  216. * <tr><td valign="top" headers="construct unicode"><tt>\P{InGreek}</tt></td>
  217. * <td headers="matches">Any character except one in the Greek block (negation)</td></tr>
  218. * <tr><td valign="top" headers="construct unicode"><tt>[\p{L}&&[^\p{Lu}]]&nbsp;</tt></td>
  219. * <td headers="matches">Any letter except an uppercase letter (subtraction)</td></tr>
  220. *
  221. * <tr><th>&nbsp;</th></tr>
  222. * <tr align="left"><th colspan="2" id="bounds">Boundary matchers</th></tr>
  223. *
  224. * <tr><td valign="top" headers="construct bounds"><tt>^</tt></td>
  225. * <td headers="matches">The beginning of a line</td></tr>
  226. * <tr><td valign="top" headers="construct bounds"><tt>$</tt></td>
  227. * <td headers="matches">The end of a line</td></tr>
  228. * <tr><td valign="top" headers="construct bounds"><tt>\b</tt></td>
  229. * <td headers="matches">A word boundary</td></tr>
  230. * <tr><td valign="top" headers="construct bounds"><tt>\B</tt></td>
  231. * <td headers="matches">A non-word boundary</td></tr>
  232. * <tr><td valign="top" headers="construct bounds"><tt>\A</tt></td>
  233. * <td headers="matches">The beginning of the input</td></tr>
  234. * <tr><td valign="top" headers="construct bounds"><tt>\G</tt></td>
  235. * <td headers="matches">The end of the previous match</td></tr>
  236. * <tr><td valign="top" headers="construct bounds"><tt>\Z</tt></td>
  237. * <td headers="matches">The end of the input but for the final
  238. * <a href="#lt">terminator</a>, if&nbsp;any</td></tr>
  239. * <tr><td valign="top" headers="construct bounds"><tt>\z</tt></td>
  240. * <td headers="matches">The end of the input</td></tr>
  241. *
  242. * <tr><th>&nbsp;</th></tr>
  243. * <tr align="left"><th colspan="2" id="greedy">Greedy quantifiers</th></tr>
  244. *
  245. * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>?</tt></td>
  246. * <td headers="matches"><i>X</i>, once or not at all</td></tr>
  247. * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>*</tt></td>
  248. * <td headers="matches"><i>X</i>, zero or more times</td></tr>
  249. * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>+</tt></td>
  250. * <td headers="matches"><i>X</i>, one or more times</td></tr>
  251. * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>}</tt></td>
  252. * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
  253. * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,}</tt></td>
  254. * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
  255. * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}</tt></td>
  256. * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
  257. *
  258. * <tr><th>&nbsp;</th></tr>
  259. * <tr align="left"><th colspan="2" id="reluc">Reluctant quantifiers</th></tr>
  260. *
  261. * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>??</tt></td>
  262. * <td headers="matches"><i>X</i>, once or not at all</td></tr>
  263. * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>*?</tt></td>
  264. * <td headers="matches"><i>X</i>, zero or more times</td></tr>
  265. * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>+?</tt></td>
  266. * <td headers="matches"><i>X</i>, one or more times</td></tr>
  267. * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>}?</tt></td>
  268. * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
  269. * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,}?</tt></td>
  270. * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
  271. * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}?</tt></td>
  272. * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
  273. *
  274. * <tr><th>&nbsp;</th></tr>
  275. * <tr align="left"><th colspan="2" id="poss">Possessive quantifiers</th></tr>
  276. *
  277. * <tr><td valign="top" headers="construct poss"><i>X</i><tt>?+</tt></td>
  278. * <td headers="matches"><i>X</i>, once or not at all</td></tr>
  279. * <tr><td valign="top" headers="construct poss"><i>X</i><tt>*+</tt></td>
  280. * <td headers="matches"><i>X</i>, zero or more times</td></tr>
  281. * <tr><td valign="top" headers="construct poss"><i>X</i><tt>++</tt></td>
  282. * <td headers="matches"><i>X</i>, one or more times</td></tr>
  283. * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>}+</tt></td>
  284. * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
  285. * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,}+</tt></td>
  286. * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
  287. * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}+</tt></td>
  288. * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
  289. *
  290. * <tr><th>&nbsp;</th></tr>
  291. * <tr align="left"><th colspan="2" id="logical">Logical operators</th></tr>
  292. *
  293. * <tr><td valign="top" headers="construct logical"><i>XY</i></td>
  294. * <td headers="matches"><i>X</i> followed by <i>Y</i></td></tr>
  295. * <tr><td valign="top" headers="construct logical"><i>X</i><tt>|</tt><i>Y</i></td>
  296. * <td headers="matches">Either <i>X</i> or <i>Y</i></td></tr>
  297. * <tr><td valign="top" headers="construct logical"><tt>(</tt><i>X</i><tt>)</tt></td>
  298. * <td headers="matches">X, as a <a href="#cg">capturing group</a></td></tr>
  299. *
  300. * <tr><th>&nbsp;</th></tr>
  301. * <tr align="left"><th colspan="2" id="backref">Back references</th></tr>
  302. *
  303. * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>n</i></td>
  304. * <td valign="bottom" headers="matches">Whatever the <i>n</i><sup>th</sup>
  305. * <a href="#cg">capturing group</a> matched</td></tr>
  306. *
  307. * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>k</i>&lt;<i>name</i>&gt;</td>
  308. * <td valign="bottom" headers="matches">Whatever the
  309. * <a href="#groupname">named-capturing group</a> "name" matched</td></tr>
  310. *
  311. * <tr><th>&nbsp;</th></tr>
  312. * <tr align="left"><th colspan="2" id="quot">Quotation</th></tr>
  313. *
  314. * <tr><td valign="top" headers="construct quot"><tt>\</tt></td>
  315. * <td headers="matches">Nothing, but quotes the following character</td></tr>
  316. * <tr><td valign="top" headers="construct quot"><tt>\Q</tt></td>
  317. * <td headers="matches">Nothing, but quotes all characters until <tt>\E</tt></td></tr>
  318. * <tr><td valign="top" headers="construct quot"><tt>\E</tt></td>
  319. * <td headers="matches">Nothing, but ends quoting started by <tt>\Q</tt></td></tr>
  320. * <!-- Metachars: !$()*+.<>?[\]^{|} -->
  321. *
  322. * <tr><th>&nbsp;</th></tr>
  323. * <tr align="left"><th colspan="2" id="special">Special constructs (named-capturing and non-capturing)</th></tr>
  324. *
  325. * <tr><td valign="top" headers="construct special"><tt>(?&lt;<a href="#groupname">name</a>&gt;</tt><i>X</i><tt>)</tt></td>
  326. * <td headers="matches"><i>X</i>, as a named-capturing group</td></tr>
  327. * <tr><td valign="top" headers="construct special"><tt>(?:</tt><i>X</i><tt>)</tt></td>
  328. * <td headers="matches"><i>X</i>, as a non-capturing group</td></tr>
  329. * <tr><td valign="top" headers="construct special"><tt>(?idmsuxU-idmsuxU)&nbsp;</tt></td>
  330. * <td headers="matches">Nothing, but turns match flags <a href="#CASE_INSENSITIVE">i</a>
  331. * <a href="#UNIX_LINES">d</a> <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a>
  332. * <a href="#UNICODE_CASE">u</a> <a href="#COMMENTS">x</a> <a href="#UNICODE_CHARACTER_CLASS">U</a>
  333. * on - off</td></tr>
  334. * <tr><td valign="top" headers="construct special"><tt>(?idmsux-idmsux:</tt><i>X</i><tt>)</tt>&nbsp;&nbsp;</td>
  335. * <td headers="matches"><i>X</i>, as a <a href="#cg">non-capturing group</a> with the
  336. * given flags <a href="#CASE_INSENSITIVE">i</a> <a href="#UNIX_LINES">d</a>
  337. * <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> <a href="#UNICODE_CASE">u</a >
  338. * <a href="#COMMENTS">x</a> on - off</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 lookahead</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 lookahead</td></tr>
  343. * <tr><td valign="top" headers="construct special"><tt>(?&lt;=</tt><i>X</i><tt>)</tt></td>
  344. * <td headers="matches"><i>X</i>, via zero-width positive lookbehind</td></tr>
  345. * <tr><td valign="top" headers="construct special"><tt>(?&lt;!</tt><i>X</i><tt>)</tt></td>
  346. * <td headers="matches"><i>X</i>, via zero-width negative lookbehind</td></tr>
  347. * <tr><td valign="top" headers="construct special"><tt>(?&gt;</tt><i>X</i><tt>)</tt></td>
  348. * <td headers="matches"><i>X</i>, as an independent, non-capturing group</td></tr>
  349. *
  350. * </table>
  351. *
  352. * <hr>
  353. *
  354. *
  355. * <a name="bs">
  356. * <h4> Backslashes, escapes, and quoting </h4>
  357. *
  358. * <p> The backslash character (<tt>'\'</tt>) serves to introduce escaped
  359. * constructs, as defined in the table above, as well as to quote characters
  360. * that otherwise would be interpreted as unescaped constructs. Thus the
  361. * expression <tt>\\</tt> matches a single backslash and <tt>\{</tt> matches a
  362. * left brace.
  363. *
  364. * <p> It is an error to use a backslash prior to any alphabetic character that
  365. * does not denote an escaped construct; these are reserved for future
  366. * extensions to the regular-expression language. A backslash may be used
  367. * prior to a non-alphabetic character regardless of whether that character is
  368. * part of an unescaped construct.
  369. *
  370. * <p> Backslashes within string literals in Java source code are interpreted
  371. * as required by
  372. * <cite>The Java&trade; Language Specification</cite>
  373. * as either Unicode escapes (section 3.3) or other character escapes (section 3.10.6)
  374. * It is therefore necessary to double backslashes in string
  375. * literals that represent regular expressions to protect them from
  376. * interpretation by the Java bytecode compiler. The string literal
  377. * <tt>"&#92;b"</tt>, for example, matches a single backspace character when
  378. * interpreted as a regular expression, while <tt>"&#92;&#92;b"</tt> matches a
  379. * word boundary. The string literal <tt>"&#92;(hello&#92;)"</tt> is illegal
  380. * and leads to a compile-time error; in order to match the string
  381. * <tt>(hello)</tt> the string literal <tt>"&#92;&#92;(hello&#92;&#92;)"</tt>
  382. * must be used.
  383. *
  384. * <a name="cc">
  385. * <h4> Character Classes </h4>
  386. *
  387. * <p> Character classes may appear within other character classes, and
  388. * may be composed by the union operator (implicit) and the intersection
  389. * operator (<tt>&amp;&amp;</tt>).
  390. * The union operator denotes a class that contains every character that is
  391. * in at least one of its operand classes. The intersection operator
  392. * denotes a class that contains every character that is in both of its
  393. * operand classes.
  394. *
  395. * <p> The precedence of character-class operators is as follows, from
  396. * highest to lowest:
  397. *
  398. * <blockquote><table border="0" cellpadding="1" cellspacing="0"
  399. * summary="Precedence of character class operators.">
  400. * <tr><th>1&nbsp;&nbsp;&nbsp;&nbsp;</th>
  401. * <td>Literal escape&nbsp;&nbsp;&nbsp;&nbsp;</td>
  402. * <td><tt>\x</tt></td></tr>
  403. * <tr><th>2&nbsp;&nbsp;&nbsp;&nbsp;</th>
  404. * <td>Grouping</td>
  405. * <td><tt>[...]</tt></td></tr>
  406. * <tr><th>3&nbsp;&nbsp;&nbsp;&nbsp;</th>
  407. * <td>Range</td>
  408. * <td><tt>a-z</tt></td></tr>
  409. * <tr><th>4&nbsp;&nbsp;&nbsp;&nbsp;</th>
  410. * <td>Union</td>
  411. * <td><tt>[a-e][i-u]</tt></td></tr>
  412. * <tr><th>5&nbsp;&nbsp;&nbsp;&nbsp;</th>
  413. * <td>Intersection</td>
  414. * <td><tt>[a-z&&[aeiou]]</tt></td></tr>
  415. * </table></blockquote>
  416. *
  417. * <p> Note that a different set of metacharacters are in effect inside
  418. * a character class than outside a character class. For instance, the
  419. * regular expression <tt>.</tt> loses its special meaning inside a
  420. * character class, while the expression <tt>-</tt> becomes a range
  421. * forming metacharacter.
  422. *
  423. * <a name="lt">
  424. * <h4> Line terminators </h4>
  425. *
  426. * <p> A <i>line terminator</i> is a one- or two-character sequence that marks
  427. * the end of a line of the input character sequence. The following are
  428. * recognized as line terminators:
  429. *
  430. * <ul>
  431. *
  432. * <li> A newline (line feed) character&nbsp;(<tt>'\n'</tt>),
  433. *
  434. * <li> A carriage-return character followed immediately by a newline
  435. * character&nbsp;(<tt>"\r\n"</tt>),
  436. *
  437. * <li> A standalone carriage-return character&nbsp;(<tt>'\r'</tt>),
  438. *
  439. * <li> A next-line character&nbsp;(<tt>'&#92;u0085'</tt>),
  440. *
  441. * <li> A line-separator character&nbsp;(<tt>'&#92;u2028'</tt>), or
  442. *
  443. * <li> A paragraph-separator character&nbsp;(<tt>'&#92;u2029</tt>).
  444. *
  445. * </ul>
  446. * <p>If {@link #UNIX_LINES} mode is activated, then the only line terminators
  447. * recognized are newline characters.
  448. *
  449. * <p> The regular expression <tt>.</tt> matches any character except a line
  450. * terminator unless the {@link #DOTALL} flag is specified.
  451. *
  452. * <p> By default, the regular expressions <tt>^</tt> and <tt>$</tt> ignore
  453. * line terminators and only match at the beginning and the end, respectively,
  454. * of the entire input sequence. If {@link #MULTILINE} mode is activated then
  455. * <tt>^</tt> matches at the beginning of input and after any line terminator
  456. * except at the end of input. When in {@link #MULTILINE} mode <tt>$</tt>
  457. * matches just before a line terminator or the end of the input sequence.
  458. *
  459. * <a name="cg">
  460. * <h4> Groups and capturing </h4>
  461. *
  462. * <a name="gnumber">
  463. * <h5> Group number </h5>
  464. * <p> Capturing groups are numbered by counting their opening parentheses from
  465. * left to right. In the expression <tt>((A)(B(C)))</tt>, for example, there
  466. * are four such groups: </p>
  467. *
  468. * <blockquote><table cellpadding=1 cellspacing=0 summary="Capturing group numberings">
  469. * <tr><th>1&nbsp;&nbsp;&nbsp;&nbsp;</th>
  470. * <td><tt>((A)(B(C)))</tt></td></tr>
  471. * <tr><th>2&nbsp;&nbsp;&nbsp;&nbsp;</th>
  472. * <td><tt>(A)</tt></td></tr>
  473. * <tr><th>3&nbsp;&nbsp;&nbsp;&nbsp;</th>
  474. * <td><tt>(B(C))</tt></td></tr>
  475. * <tr><th>4&nbsp;&nbsp;&nbsp;&nbsp;</th>
  476. * <td><tt>(C)</tt></td></tr>
  477. * </table></blockquote>
  478. *
  479. * <p> Group zero always stands for the entire expression.
  480. *
  481. * <p> Capturing groups are so named because, during a match, each subsequence
  482. * of the input sequence that matches such a group is saved. The captured
  483. * subsequence may be used later in the expression, via a back reference, and
  484. * may also be retrieved from the matcher once the match operation is complete.
  485. *
  486. * <a name="groupname">
  487. * <h5> Group name </h5>
  488. * <p>A capturing group can also be assigned a "name", a <tt>named-capturing group</tt>,
  489. * and then be back-referenced later by the "name". Group names are composed of
  490. * the following characters. The first character must be a <tt>letter</tt>.
  491. *
  492. * <ul>
  493. * <li> The uppercase letters <tt>'A'</tt> through <tt>'Z'</tt>
  494. * (<tt>'&#92;u0041'</tt>&nbsp;through&nbsp;<tt>'&#92;u005a'</tt>),
  495. * <li> The lowercase letters <tt>'a'</tt> through <tt>'z'</tt>
  496. * (<tt>'&#92;u0061'</tt>&nbsp;through&nbsp;<tt>'&#92;u007a'</tt>),
  497. * <li> The digits <tt>'0'</tt> through <tt>'9'</tt>
  498. * (<tt>'&#92;u0030'</tt>&nbsp;through&nbsp;<tt>'&#92;u0039'</tt>),
  499. * </ul>
  500. *
  501. * <p> A <tt>named-capturing group</tt> is still numbered as described in
  502. * <a href="#gnumber">Group number</a>.
  503. *
  504. * <p> The captured input associated with a group is always the subsequence
  505. * that the group most recently matched. If a group is evaluated a second time
  506. * because of quantification then its previously-captured value, if any, will
  507. * be retained if the second evaluation fails. Matching the string
  508. * <tt>"aba"</tt> against the expression <tt>(a(b)?)+</tt>, for example, leaves
  509. * group two set to <tt>"b"</tt>. All captured input is discarded at the
  510. * beginning of each match.
  511. *
  512. * <p> Groups beginning with <tt>(?</tt> are either pure, <i>non-capturing</i> groups
  513. * that do not capture text and do not count towards the group total, or
  514. * <i>named-capturing</i> group.
  515. *
  516. * <h4> Unicode support </h4>
  517. *
  518. * <p> This class is in conformance with Level 1 of <a
  519. * href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
  520. * Standard #18: Unicode Regular Expression</i></a>, plus RL2.1
  521. * Canonical Equivalents.
  522. * <p>
  523. * <b>Unicode escape sequences</b> such as <tt>&#92;u2014</tt> in Java source code
  524. * are processed as described in section 3.3 of
  525. * <cite>The Java&trade; Language Specification</cite>.
  526. * Such escape sequences are also implemented directly by the regular-expression
  527. * parser so that Unicode escapes can be used in expressions that are read from
  528. * files or from the keyboard. Thus the strings <tt>"&#92;u2014"</tt> and
  529. * <tt>"\\u2014"</tt>, while not equal, compile into the same pattern, which
  530. * matches the character with hexadecimal value <tt>0x2014</tt>.
  531. * <p>
  532. * A Unicode character can also be represented in a regular-expression by
  533. * using its <b>Hex notation</b>(hexadecimal code point value) directly as described in construct
  534. * <tt>&#92;x{...}</tt>, for example a supplementary character U+2011F
  535. * can be specified as <tt>&#92;x{2011F}</tt>, instead of two consecutive
  536. * Unicode escape sequences of the surrogate pair
  537. * <tt>&#92;uD840</tt><tt>&#92;uDD1F</tt>.
  538. * <p>
  539. * Unicode scripts, blocks, categories and binary properties are written with
  540. * the <tt>\p</tt> and <tt>\P</tt> constructs as in Perl.
  541. * <tt>\p{</tt><i>prop</i><tt>}</tt> matches if
  542. * the input has the property <i>prop</i>, while <tt>\P{</tt><i>prop</i><tt>}</tt>
  543. * does not match if the input has that property.
  544. * <p>
  545. * Scripts, blocks, categories and binary properties can be used both inside
  546. * and outside of a character class.
  547. * <a name="usc">
  548. * <p>
  549. * <b>Scripts</b> are specified either with the prefix {@code Is}, as in
  550. * {@code IsHiragana}, or by using the {@code script} keyword (or its short
  551. * form {@code sc})as in {@code script=Hiragana} or {@code sc=Hiragana}.
  552. * <p>
  553. * The script names supported by <code>Pattern</code> are the valid script names
  554. * accepted and defined by
  555. * {@link java.lang.Character.UnicodeScript#forName(String) UnicodeScript.forName}.
  556. * <a name="ubc">
  557. * <p>
  558. * <b>Blocks</b> are specified with the prefix {@code In}, as in
  559. * {@code InMongolian}, or by using the keyword {@code block} (or its short
  560. * form {@code blk}) as in {@code block=Mongolian} or {@code blk=Mongolian}.
  561. * <p>
  562. * The block names supported by <code>Pattern</code> are the valid block names
  563. * accepted and defined by
  564. * {@link java.lang.Character.UnicodeBlock#forName(String) UnicodeBlock.forName}.
  565. * <p>
  566. * <a name="ucc">
  567. * <b>Categories</b> may be specified with the optional prefix {@code Is}:
  568. * Both {@code \p{L}} and {@code \p{IsL}} denote the category of Unicode
  569. * letters. Same as scripts and blocks, categories can also be specified
  570. * by using the keyword {@code general_category} (or its short form
  571. * {@code gc}) as in {@code general_category=Lu} or {@code gc=Lu}.
  572. * <p>
  573. * The supported categories are those of
  574. * <a href="http://www.unicode.org/unicode/standard/standard.html">
  575. * <i>The Unicode Standard</i></a> in the version specified by the
  576. * {@link java.lang.Character Character} class. The category names are those
  577. * defined in the Standard, both normative and informative.
  578. * <p>
  579. * <a name="ubpc">
  580. * <b>Binary properties</b> are specified with the prefix {@code Is}, as in
  581. * {@code IsAlphabetic}. The supported binary properties by <code>Pattern</code>
  582. * are
  583. * <ul>
  584. * <li> Alphabetic
  585. * <li> Ideographic
  586. * <li> Letter
  587. * <li> Lowercase
  588. * <li> Uppercase
  589. * <li> Titlecase
  590. * <li> Punctuation
  591. * <Li> Control
  592. * <li> White_Space
  593. * <li> Digit
  594. * <li> Hex_Digit
  595. * <li> Noncharacter_Code_Point
  596. * <li> Assigned
  597. * </ul>
  598. * <p>
  599. * <b>Predefined Character classes</b> and <b>POSIX character classes</b> are in
  600. * conformance with the recommendation of <i>Annex C: Compatibility Properties</i>
  601. * of <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Regular Expression
  602. * </i></a>, when {@link #UNICODE_CHARACTER_CLASS} flag is specified.
  603. * <p>
  604. * <table border="0" cellpadding="1" cellspacing="0"
  605. * summary="predefined and posix character classes in Unicode mode">
  606. * <tr align="left">
  607. * <th bgcolor="#CCCCFF" align="left" id="classes">Classes</th>
  608. * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
  609. *</tr>
  610. * <tr><td><tt>\p{Lower}</tt></td>
  611. * <td>A lowercase character:<tt>\p{IsLowercase}</tt></td></tr>
  612. * <tr><td><tt>\p{Upper}</tt></td>
  613. * <td>An uppercase character:<tt>\p{IsUppercase}</tt></td></tr>
  614. * <tr><td><tt>\p{ASCII}</tt></td>
  615. * <td>All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
  616. * <tr><td><tt>\p{Alpha}</tt></td>
  617. * <td>An alphabetic character:<tt>\p{IsAlphabetic}</tt></td></tr>
  618. * <tr><td><tt>\p{Digit}</tt></td>
  619. * <td>A decimal digit character:<tt>p{IsDigit}</tt></td></tr>
  620. * <tr><td><tt>\p{Alnum}</tt></td>
  621. * <td>An alphanumeric character:<tt>[\p{IsAlphabetic}\p{IsDigit}]</tt></td></tr>
  622. * <tr><td><tt>\p{Punct}</tt></td>
  623. * <td>A punctuation character:<tt>p{IsPunctuation}</tt></td></tr>
  624. * <tr><td><tt>\p{Graph}</tt></td>
  625. * <td>A visible character: <tt>[^\p{IsWhite_Space}\p{gc=Cc}\p{gc=Cs}\p{gc=Cn}]</tt></td></tr>
  626. * <tr><td><tt>\p{Print}</tt></td>
  627. * <td>A printable character: <tt>[\p{Graph}\p{Blank}&&[^\p{Cntrl}]]</tt></td></tr>
  628. * <tr><td><tt>\p{Blank}</tt></td>
  629. * <td>A space or a tab: <tt>[\p{IsWhite_Space}&&[^\p{gc=Zl}\p{gc=Zp}\x0a\x0b\x0c\x0d\x85]]</tt></td></tr>
  630. * <tr><td><tt>\p{Cntrl}</tt></td>
  631. * <td>A control character: <tt>\p{gc=Cc}</tt></td></tr>
  632. * <tr><td><tt>\p{XDigit}</tt></td>
  633. * <td>A hexadecimal digit: <tt>[\p{gc=Nd}\p{IsHex_Digit}]</tt></td></tr>
  634. * <tr><td><tt>\p{Space}</tt></td>
  635. * <td>A whitespace character:<tt>\p{IsWhite_Space}</tt></td></tr>
  636. * <tr><td><tt>\d</tt></td>
  637. * <td>A digit: <tt>\p{IsDigit}</tt></td></tr>
  638. * <tr><td><tt>\D</tt></td>
  639. * <td>A non-digit: <tt>[^\d]</tt></td></tr>
  640. * <tr><td><tt>\s</tt></td>
  641. * <td>A whitespace character: <tt>\p{IsWhite_Space}</tt></td></tr>
  642. * <tr><td><tt>\S</tt></td>
  643. * <td>A non-whitespace character: <tt>[^\s]</tt></td></tr>
  644. * <tr><td><tt>\w</tt></td>
  645. * <td>A word character: <tt>[\p{Alpha}\p{gc=Mn}\p{gc=Me}\p{gc=Mc}\p{Digit}\p{gc=Pc}]</tt></td></tr>
  646. * <tr><td><tt>\W</tt></td>
  647. * <td>A non-word character: <tt>[^\w]</tt></td></tr>
  648. * </table>
  649. * <p>
  650. * <a name="jcc">
  651. * Categories that behave like the java.lang.Character
  652. * boolean is<i>methodname</i> methods (except for the deprecated ones) are
  653. * available through the same <tt>\p{</tt><i>prop</i><tt>}</tt> syntax where
  654. * the specified property has the name <tt>java<i>methodname</i></tt>.
  655. *
  656. * <h4> Comparison to Perl 5 </h4>
  657. *
  658. * <p>The <code>Pattern</code> engine performs traditional NFA-based matching
  659. * with ordered alternation as occurs in Perl 5.
  660. *
  661. * <p> Perl constructs not supported by this class: </p>
  662. *
  663. * <ul>
  664. * <li><p> Predefined character classes (Unicode character)
  665. * <p><tt>\h&nbsp;&nbsp;&nbsp;&nbsp;</tt>A horizontal whitespace
  666. * <p><tt>\H&nbsp;&nbsp;&nbsp;&nbsp;</tt>A non horizontal whitespace
  667. * <p><tt>\v&nbsp;&nbsp;&nbsp;&nbsp;</tt>A vertical whitespace
  668. * <p><tt>\V&nbsp;&nbsp;&nbsp;&nbsp;</tt>A non vertical whitespace
  669. * <p><tt>\R&nbsp;&nbsp;&nbsp;&nbsp;</tt>Any Unicode linebreak sequence
  670. * <tt>\u005cu000D\u005cu000A|[\u005cu000A\u005cu000B\u005cu000C\u005cu000D\u005cu0085\u005cu2028\u005cu2029]</tt>
  671. * <p><tt>\X&nbsp;&nbsp;&nbsp;&nbsp;</tt>Match Unicode
  672. * <a href="http://www.unicode.org/reports/tr18/#Default_Grapheme_Clusters">
  673. * <i>extended grapheme cluster</i></a>
  674. * </p></li>
  675. *
  676. * <li><p> The backreference constructs, <tt>\g{</tt><i>n</i><tt>}</tt> for
  677. * the <i>n</i><sup>th</sup><a href="#cg">capturing group</a> and
  678. * <tt>\g{</tt><i>name</i><tt>}</tt> for
  679. * <a href="#groupname">named-capturing group</a>.
  680. * </p></li>
  681. *
  682. * <li><p> The named character construct, <tt>\N{</tt><i>name</i><tt>}</tt>
  683. * for a Unicode character by its name.
  684. * </p></li>
  685. *
  686. * <li><p> The conditional constructs
  687. * <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>)</tt> and
  688. * <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>|</tt><i>Y</i><tt>)</tt>,
  689. * </p></li>
  690. *
  691. * <li><p> The embedded code constructs <tt>(?{</tt><i>code</i><tt>})</tt>
  692. * and <tt>(??{</tt><i>code</i><tt>})</tt>,</p></li>
  693. *
  694. * <li><p> The embedded comment syntax <tt>(?#comment)</tt>, and </p></li>
  695. *
  696. * <li><p> The preprocessing operations <tt>\l</tt> <tt>&#92;u</tt>,
  697. * <tt>\L</tt>, and <tt>\U</tt>. </p></li>
  698. *
  699. * </ul>
  700. *
  701. * <p> Constructs supported by this class but not by Perl: </p>
  702. *
  703. * <ul>
  704. *
  705. * <li><p> Character-class union and intersection as described
  706. * <a href="#cc">above</a>.</p></li>
  707. *
  708. * </ul>
  709. *
  710. * <p> Notable differences from Perl: </p>
  711. *
  712. * <ul>
  713. *
  714. * <li><p> In Perl, <tt>\1</tt> through <tt>\9</tt> are always interpreted
  715. * as back references; a backslash-escaped number greater than <tt>9</tt> is
  716. * treated as a back reference if at least that many subexpressions exist,
  717. * otherwise it is interpreted, if possible, as an octal escape. In this
  718. * class octal escapes must always begin with a zero. In this class,
  719. * <tt>\1</tt> through <tt>\9</tt> are always interpreted as back
  720. * references, and a larger number is accepted as a back reference if at
  721. * least that many subexpressions exist at that point in the regular
  722. * expression, otherwise the parser will drop digits until the number is
  723. * smaller or equal to the existing number of groups or it is one digit.
  724. * </p></li>
  725. *
  726. * <li><p> Perl uses the <tt>g</tt> flag to request a match that resumes
  727. * where the last match left off. This functionality is provided implicitly
  728. * by the {@link Matcher} class: Repeated invocations of the {@link
  729. * Matcher#find find} method will resume where the last match left off,
  730. * unless the matcher is reset. </p></li>
  731. *
  732. * <li><p> In Perl, embedded flags at the top level of an expression affect
  733. * the whole expression. In this class, embedded flags always take effect
  734. * at the point at which they appear, whether they are at the top level or
  735. * within a group; in the latter case, flags are restored at the end of the
  736. * group just as in Perl. </p></li>
  737. *
  738. * </ul>
  739. *
  740. *
  741. * <p> For a more precise description of the behavior of regular expression
  742. * constructs, please see <a href="http://www.oreilly.com/catalog/regex3/">
  743. * <i>Mastering Regular Expressions, 3nd Edition</i>, Jeffrey E. F. Friedl,
  744. * O'Reilly and Associates, 2006.</a>
  745. * </p>
  746. *
  747. * @see java.lang.String#split(String, int)
  748. * @see java.lang.String#split(String)
  749. *
  750. * @author Mike McCloskey
  751. * @author Mark Reinhold
  752. * @author JSR-51 Expert Group
  753. * @since 1.4
  754. * @spec JSR-51
  755. */
  756. public final class Pattern
  757. implements java.io.Serializable
  758. {
  759. /**
  760. * Regular expression modifier values. Instead of being passed as
  761. * arguments, they can also be passed as inline modifiers.
  762. * For example, the following statements have the same effect.
  763. * <pre>
  764. * RegExp r1 = RegExp.compile("abc", Pattern.I|Pattern.M);
  765. * RegExp r2 = RegExp.compile("(?im)abc", 0);
  766. * </pre>
  767. *
  768. * The flags are duplicated so that the familiar Perl match flag
  769. * names are available.
  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&nbsp;<tt>(?d)</tt>.
  779. */
  780. public static final int UNIX_LINES = 0x01;
  781. /**
  782. * Enables case-insensitive matching.
  783. *
  784. * <p> By default, case-insensitive matching assumes that only characters
  785. * in the US-ASCII charset are being matched. Unicode-aware
  786. * case-insensitive matching can be enabled by specifying the {@link
  787. * #UNICODE_CASE} flag in conjunction with this flag.
  788. *
  789. * <p> Case-insensitive matching can also be enabled via the embedded flag
  790. * expression&nbsp;<tt>(?i)</tt>.
  791. *
  792. * <p> Specifying this flag may impose a slight performance penalty. </p>
  793. */
  794. public static final int CASE_INSENSITIVE = 0x02;
  795. /**
  796. * Permits whitespace and comments in pattern.
  797. *
  798. * <p> In this mode, whitespace is ignored, and embedded comments starting
  799. * with <tt>#</tt> are ignored until the end of a line.
  800. *
  801. * <p> Comments mode can also be enabled via the embedded flag
  802. * expression&nbsp;<tt>(?x)</tt>.
  803. */
  804. public static final int COMMENTS = 0x04;
  805. /**
  806. * Enables multiline mode.
  807. *
  808. * <p> In multiline mode the expressions <tt>^</tt> and <tt>$</tt> match
  809. * just after or just before, respectively, a line terminator or the end of
  810. * the input sequence. By default these expressions only match at the
  811. * beginning and the end of the entire input sequence.
  812. *
  813. * <p> Multiline mode can also be enabled via the embedded flag
  814. * expression&nbsp;<tt>(?m)</tt>. </p>
  815. */
  816. public static final int MULTILINE = 0x08;
  817. /**
  818. * Enables literal parsing of the pattern.
  819. *
  820. * <p> When this flag is specified then the input string that specifies
  821. * the pattern is treated as a sequence of literal characters.
  822. * Metacharacters or escape sequences in the input sequence will be
  823. * given no special meaning.
  824. *
  825. * <p>The flags CASE_INSENSITIVE and UNICODE_CASE retain their impact on
  826. * matching when used in conjunction with this flag. The other flags
  827. * become superfluous.
  828. *
  829. * <p> There is no embedded flag character for enabling literal parsing.
  830. * @since 1.5
  831. */
  832. public static final int LITERAL = 0x10;
  833. /**
  834. * Enables dotall mode.
  835. *
  836. * <p> In dotall mode, the expression <tt>.</tt> matches any character,
  837. * including a line terminator. By default this expression does not match
  838. * line terminators.
  839. *
  840. * <p> Dotall mode can also be enabled via the embedded flag
  841. * expression&nbsp;<tt>(?s)</tt>. (The <tt>s</tt> is a mnemonic for
  842. * "single-line" mode, which is what this is called in Perl.) </p>
  843. */
  844. public static final int DOTALL = 0x20;
  845. /**
  846. * Enables Unicode-aware case folding.
  847. *
  848. * <p> When this flag is specified then case-insensitive matching, when
  849. * enabled by the {@link #CASE_INSENSITIVE} flag, is done in a manner
  850. * consistent with the Unicode Standard. By default, case-insensitive
  851. * matching assumes that only characters in the US-ASCII charset are being
  852. * matched.
  853. *
  854. * <p> Unicode-aware case folding can also be enabled via the embedded flag
  855. * expression&nbsp;<tt>(?u)</tt>.
  856. *
  857. * <p> Specifying this flag may impose a performance penalty. </p>
  858. */
  859. public static final int UNICODE_CASE = 0x40;
  860. /**
  861. * Enables canonical equivalence.
  862. *
  863. * <p> When this flag is specified then two characters will be considered
  864. * to match if, and only if, their full canonical decompositions match.
  865. * The expression <tt>"a&#92;u030A"</tt>, for example, will match the
  866. * string <tt>"&#92;u00E5"</tt> when this flag is specified. By default,
  867. * matching does not take canonical equivalence into account.
  868. *
  869. * <p> There is no embedded flag character for enabling canonical
  870. * equivalence.
  871. *
  872. * <p> Specifying this flag may impose a performance penalty. </p>
  873. */
  874. public static final int CANON_EQ = 0x80;
  875. /**
  876. * Enables the Unicode version of <i>Predefined character classes</i> and
  877. * <i>POSIX character classes</i>.
  878. *
  879. * <p> When this flag is specified then the (US-ASCII only)
  880. * <i>Predefined character classes</i> and <i>POSIX character classes</i>
  881. * are in conformance with
  882. * <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
  883. * Standard #18: Unicode Regular Expression</i></a>
  884. * <i>Annex C: Compatibility Properties</i>.
  885. * <p>
  886. * The UNICODE_CHARACTER_CLASS mode can also be enabled via the embedded
  887. * flag expression&nbsp;<tt>(?U)</tt>.
  888. * <p>
  889. * The flag implies UNICODE_CASE, that is, it enables Unicode-aware case
  890. * folding.
  891. * <p>
  892. * Specifying this flag may impose a performance penalty. </p>
  893. * @since 1.7
  894. */
  895. public static final int UNICODE_CHARACTER_CLASS = 0x100;
  896. /* Pattern has only two serialized components: The pattern string
  897. * and the flags, which are all that is needed to recompile the pattern
  898. * when it is deserialized.
  899. */
  900. /** use serialVersionUID from Merlin b59 for interoperability */
  901. private static final long serialVersionUID = 5073258162644648461L;
  902. /**
  903. * The original regular-expression pattern string.
  904. *
  905. * @serial
  906. */
  907. private String pattern;
  908. /**
  909. * The original pattern flags.
  910. *
  911. * @serial
  912. */
  913. private int flags;
  914. /**
  915. * Boolean indicating this Pattern is compiled; this is necessary in order
  916. * to lazily compile deserialized Patterns.
  917. */
  918. private transient volatile boolean compiled = false;
  919. /**
  920. * The normalized pattern string.
  921. */
  922. private transient String normalizedPattern;
  923. /**
  924. * The starting point of state machine for the find operation. This allows
  925. * a match to start anywhere in the input.
  926. */
  927. transient Node root;
  928. /**
  929. * The root of object tree for a match operation. The pattern is matched
  930. * at the beginning. This may include a find that uses BnM or a First
  931. * node.
  932. */
  933. transient Node matchRoot;
  934. /**
  935. * Temporary storage used by parsing pattern slice.
  936. */
  937. transient int[] buffer;
  938. /**
  939. * Map the "name" of the "named capturing group" to its group id
  940. * node.
  941. */
  942. transient volatile Map<String, Integer> namedGroups;
  943. /**
  944. * Temporary storage used while parsing group references.
  945. */
  946. transient GroupHead[] groupNodes;
  947. /**
  948. * Temporary null terminated code point array used by pattern compiling.
  949. */
  950. private transient int[] temp;
  951. /**
  952. * The number of capturing groups in this Pattern. Used by matchers to
  953. * allocate storage needed to perform a match.
  954. */
  955. transient int capturingGroupCount;
  956. /**
  957. * The local variable count used by parsing tree. Used by matchers to
  958. * allocate storage needed to perform a match.
  959. */
  960. transient int localCount;
  961. /**
  962. * Index into the pattern string that keeps track of how much has been
  963. * parsed.
  964. */
  965. private transient int cursor;
  966. /**
  967. * Holds the length of the pattern string.
  968. */
  969. private transient int patternLength;
  970. /**
  971. * If the Start node might possibly match supplementary characters.
  972. * It is set to true during compiling if
  973. * (1) There is supplementary char in pattern, or
  974. * (2) There is complement node of Category or Block
  975. */
  976. private transient boolean hasSupplementary;
  977. /**
  978. * Compiles the given regular expression into a pattern. </p>
  979. *
  980. * @param regex
  981. * The expression to be compiled
  982. *
  983. * @throws PatternSyntaxException
  984. * If the expression's syntax is invalid
  985. */
  986. public static Pattern compile(String regex) {
  987. return new Pattern(regex, 0);
  988. }
  989. /**
  990. * Compiles the given regular expression into a pattern with the given
  991. * flags. </p>
  992. *
  993. * @param regex
  994. * The expression to be compiled
  995. *
  996. * @param flags
  997. * Match flags, a bit mask that may include
  998. * {@link #CASE_INSENSITIVE}, {@link #MULTILINE}, {@link #DOTALL},
  999. * {@link #UNICODE_CASE}, {@link #CANON_EQ}, {@link #UNIX_LINES},
  1000. * {@link #LITERAL}, {@link #UNICODE_CHARACTER_CLASS}
  1001. * and {@link #COMMENTS}
  1002. *
  1003. * @throws IllegalArgumentException
  1004. * If bit values other than those corresponding to the defined
  1005. * match flags are set in <tt>flags</tt>
  1006. *
  1007. * @throws PatternSyntaxException
  1008. * If the expression's syntax is invalid
  1009. */
  1010. public static Pattern compile(String regex, int flags) {
  1011. return new Pattern(regex, flags);
  1012. }
  1013. /**
  1014. * Returns the regular expression from which this pattern was compiled.
  1015. * </p>
  1016. *
  1017. * @return The source of this pattern
  1018. */
  1019. public String pattern() {
  1020. return pattern;
  1021. }
  1022. /**
  1023. * <p>Returns the string representation of this pattern. This
  1024. * is the reg…

Large files files are truncated, but you can click here to view the full file