2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.opendaylight.yangtools.xsd.regex;
20 import java.text.CharacterIterator;
21 import java.util.Locale;
22 import java.util.Stack;
25 * A regular expression matching engine using Non-deterministic Finite Automaton (NFA).
26 * This engine does not conform to the POSIX regular expression.
35 * RegularExpression re = new RegularExpression(<var>regex</var>);
36 * if (re.matches(text)) { ... }
39 * <dt>B. Capturing groups
42 * RegularExpression re = new RegularExpression(<var>regex</var>);
43 * Match match = new Match();
44 * if (re.matches(text, match)) {
45 * ... // You can refer captured texts with methods of the <code>Match</code> class.
51 * <h4>Case-insensitive matching</h4>
53 * RegularExpression re = new RegularExpression(<var>regex</var>, "i");
54 * if (re.matches(text) >= 0) { ...}
58 * <p>You can specify options to <a href="#RegularExpression(java.lang.String, java.lang.String)"><code>RegularExpression(</code><var>regex</var><code>, </code><var>options</var><code>)</code></a>
59 * or <a href="#setPattern(java.lang.String, java.lang.String)"><code>setPattern(</code><var>regex</var><code>, </code><var>options</var><code>)</code></a>.
60 * This <var>options</var> parameter consists of the following characters.
63 * <dt><a name="I_OPTION"><code>"i"</code></a>
64 * <dd>This option indicates case-insensitive matching.
65 * <dt><a name="M_OPTION"><code>"m"</code></a>
66 * <dd class="REGEX"><kbd>^</kbd> and <kbd>$</kbd> consider the EOL characters within the text.
67 * <dt><a name="S_OPTION"><code>"s"</code></a>
68 * <dd class="REGEX"><kbd>.</kbd> matches any one character.
69 * <dt><a name="U_OPTION"><code>"u"</code></a>
70 * <dd class="REGEX">Redefines <Kbd>\d \D \w \W \s \S \b \B \< \></kbd> as becoming to Unicode.
71 * <dt><a name="W_OPTION"><code>"w"</code></a>
72 * <dd class="REGEX">By this option, <kbd>\b \B \< \></kbd> are processed with the method of
73 * 'Unicode Regular Expression Guidelines' Revision 4.
74 * When "w" and "u" are specified at the same time,
75 * <kbd>\b \B \< \></kbd> are processed for the "w" option.
76 * <dt><a name="COMMA_OPTION"><code>","</code></a>
77 * <dd>The parser treats a comma in a character class as a range separator.
78 * <kbd class="REGEX">[a,b]</kbd> matches <kbd>a</kbd> or <kbd>,</kbd> or <kbd>b</kbd> without this option.
79 * <kbd class="REGEX">[a,b]</kbd> matches <kbd>a</kbd> or <kbd>b</kbd> with this option.
81 * <dt><a name="X_OPTION"><code>"X"</code></a>
83 * By this option, the engine confoms to <a href="http://www.w3.org/TR/2000/WD-xmlschema-2-20000407/#regexs">XML Schema: Regular Expression</a>.
84 * The <code>match()</code> method does not do subsring matching
85 * but entire string matching.
91 * <table border="1" bgcolor="#ddeeff">
94 * <h4>Differences from the Perl 5 regular expression</h4>
96 * <li>There is 6-digit hexadecimal character representation (<kbd>\u005cv</kbd><var>HHHHHH</var>.)
97 * <li>Supports subtraction, union, and intersection operations for character classes.
98 * <li>Not supported: <kbd>\</kbd><var>ooo</var> (Octal character representations),
99 * <Kbd>\G</kbd>, <kbd>\C</kbd>, <kbd>\l</kbd><var>c</var>,
100 * <kbd>\u005c u</kbd><var>c</var>, <kbd>\L</kbd>, <kbd>\U</kbd>,
101 * <kbd>\E</kbd>, <kbd>\Q</kbd>, <kbd>\N{</kbd><var>name</var><kbd>}</kbd>,
102 * <Kbd>(?{<kbd><var>code</var><kbd>})</kbd>, <Kbd>(??{<kbd><var>code</var><kbd>})</kbd>
108 * <P>Meta characters are `<KBD>. * + ? { [ ( ) | \ ^ $</KBD>'.</P>
112 * <dt class="REGEX"><kbd>.</kbd> (A period)
113 * <dd>Matches any one character except the following characters.
114 * <dd>LINE FEED (U+000A), CARRIAGE RETURN (U+000D),
115 * PARAGRAPH SEPARATOR (U+2029), LINE SEPARATOR (U+2028)
116 * <dd>This expression matches one code point in Unicode. It can match a pair of surrogates.
117 * <dd>When <a href="#S_OPTION">the "s" option</a> is specified,
118 * it matches any character including the above four characters.
120 * <dt class="REGEX"><Kbd>\e \f \n \r \t</kbd>
121 * <dd>Matches ESCAPE (U+001B), FORM FEED (U+000C), LINE FEED (U+000A),
122 * CARRIAGE RETURN (U+000D), HORIZONTAL TABULATION (U+0009)
124 * <dt class="REGEX"><kbd>\c</kbd><var>C</var>
125 * <dd>Matches a control character.
126 * The <var>C</var> must be one of '<kbd>@</kbd>', '<kbd>A</kbd>'-'<kbd>Z</kbd>',
127 * '<kbd>[</kbd>', '<kbd>\u005c</kbd>', '<kbd>]</kbd>', '<kbd>^</kbd>', '<kbd>_</kbd>'.
128 * It matches a control character of which the character code is less than
129 * the character code of the <var>C</var> by 0x0040.
130 * <dd class="REGEX">For example, a <kbd>\cJ</kbd> matches a LINE FEED (U+000A),
131 * and a <kbd>\c[</kbd> matches an ESCAPE (U+001B).
133 * <dt class="REGEX">a non-meta character
134 * <dd>Matches the character.
136 * <dt class="REGEX"><KBD>\</KBD> + a meta character
137 * <dd>Matches the meta character.
139 * <dt class="REGEX"><kbd>\u005cx</kbd><var>HH</var> <kbd>\u005cx{</kbd><var>HHHH</var><kbd>}</kbd>
140 * <dd>Matches a character of which code point is <var>HH</var> (Hexadecimal) in Unicode.
141 * You can write just 2 digits for <kbd>\u005cx</kbd><var>HH</var>, and
142 * variable length digits for <kbd>\u005cx{</kbd><var>HHHH</var><kbd>}</kbd>.
145 * <dt class="REGEX"><kbd>\u005c u</kbd><var>HHHH</var>
146 * <dd>Matches a character of which code point is <var>HHHH</var> (Hexadecimal) in Unicode.
149 * <dt class="REGEX"><kbd>\u005cv</kbd><var>HHHHHH</var>
150 * <dd>Matches a character of which code point is <var>HHHHHH</var> (Hexadecimal) in Unicode.
152 * <dt class="REGEX"><kbd>\g</kbd>
153 * <dd>Matches a grapheme.
154 * <dd class="REGEX">It is equivalent to <kbd>(?[\p{ASSIGNED}]-[\p{M}\p{C}])?(?:\p{M}|[\x{094D}\x{09CD}\x{0A4D}\x{0ACD}\x{0B3D}\x{0BCD}\x{0C4D}\x{0CCD}\x{0D4D}\x{0E3A}\x{0F84}]\p{L}|[\x{1160}-\x{11A7}]|[\x{11A8}-\x{11FF}]|[\x{FF9E}\x{FF9F}])*</kbd>
156 * <dt class="REGEX"><kbd>\X</kbd>
157 * <dd class="REGEX">Matches a combining character sequence.
158 * It is equivalent to <kbd>(?:\PM\pM*)</kbd>
162 * <li>Character class
164 + * <dt class="REGEX"><kbd>[</kbd><var>R<sub>1</sub></var><var>R<sub>2</sub></var><var>...</var><var>R<sub>n</sub></var><kbd>]</kbd> (without <a href="#COMMA_OPTION">"," option</a>)
165 + * <dt class="REGEX"><kbd>[</kbd><var>R<sub>1</sub></var><kbd>,</kbd><var>R<sub>2</sub></var><kbd>,</kbd><var>...</var><kbd>,</kbd><var>R<sub>n</sub></var><kbd>]</kbd> (with <a href="#COMMA_OPTION">"," option</a>)
166 * <dd>Positive character class. It matches a character in ranges.
167 * <dd><var>R<sub>n</sub></var>:
169 * <li class="REGEX">A character (including <Kbd>\e \f \n \r \t</kbd> <kbd>\u005cx</kbd><var>HH</var> <kbd>\u005cx{</kbd><var>HHHH</var><kbd>}</kbd> <!--kbd>\u005c u</kbd><var>HHHH</var--> <kbd>\u005cv</kbd><var>HHHHHH</var>)
170 * <p>This range matches the character.
171 * <li class="REGEX"><var>C<sub>1</sub></var><kbd>-</kbd><var>C<sub>2</sub></var>
172 * <p>This range matches a character which has a code point that is >= <var>C<sub>1</sub></var>'s code point and <= <var>C<sub>2</sub></var>'s code point.
173 + * <li class="REGEX">A POSIX character class: <Kbd>[:alpha:] [:alnum:] [:ascii:] [:cntrl:] [:digit:] [:graph:] [:lower:] [:print:] [:punct:] [:space:] [:upper:] [:xdigit:]</kbd>,
174 + * and negative POSIX character classes in Perl like <kbd>[:^alpha:]</kbd>
176 * <li class="REGEX"><kbd>\d \D \s \S \w \W \p{</kbd><var>name</var><kbd>} \P{</kbd><var>name</var><kbd>}</kbd>
177 * <p>These expressions specifies the same ranges as the following expressions.
179 * <p class="REGEX">Enumerated ranges are merged (union operation).
180 * <kbd>[a-ec-z]</kbd> is equivalent to <kbd>[a-z]</kbd>
182 * <dt class="REGEX"><kbd>[^</kbd><var>R<sub>1</sub></var><var>R<sub>2</sub></var><var>...</var><var>R<sub>n</sub></var><kbd>]</kbd> (without a <a href="#COMMA_OPTION">"," option</a>)
183 * <dt class="REGEX"><kbd>[^</kbd><var>R<sub>1</sub></var><kbd>,</kbd><var>R<sub>2</sub></var><kbd>,</kbd><var>...</var><kbd>,</kbd><var>R<sub>n</sub></var><kbd>]</kbd> (with a <a href="#COMMA_OPTION">"," option</a>)
184 * <dd>Negative character class. It matches a character not in ranges.
186 * <dt class="REGEX"><kbd>(?[</kbd><var>ranges</var><kbd>]</kbd><var>op</var><kbd>[</kbd><var>ranges</var><kbd>]</kbd><var>op</var><kbd>[</kbd><var>ranges</var><kbd>]</kbd> ... <Kbd>)</kbd>
187 * (<var>op</var> is <kbd>-</kbd> or <kbd>+</kbd> or <kbd>&</kbd>.)
188 * <dd>Subtraction or union or intersection for character classes.
189 * <dd class="REGEX">For exmaple, <kbd>(?[A-Z]-[CF])</kbd> is equivalent to <kbd>[A-BD-EG-Z]</kbd>, and <kbd>(?[0x00-0x7f]-[K]&[\p{Lu}])</kbd> is equivalent to <kbd>[A-JL-Z]</kbd>.
190 * <dd>The result of this operations is a <u>positive character class</u>
191 * even if an expression includes any negative character classes.
192 * You have to take care on this in case-insensitive matching.
193 * For instance, <kbd>(?[^b])</kbd> is equivalent to <kbd>[\x00-ac-\x{10ffff}]</kbd>,
194 * which is equivalent to <kbd>[^b]</kbd> in case-sensitive matching.
195 * But, in case-insensitive matching, <kbd>(?[^b])</kbd> matches any character because
196 * it includes '<kbd>B</kbd>' and '<kbd>B</kbd>' matches '<kbd>b</kbd>'
197 * though <kbd>[^b]</kbd> is processed as <kbd>[^Bb]</kbd>.
199 * <dt class="REGEX"><kbd>[</kbd><var>R<sub>1</sub>R<sub>2</sub>...</var><kbd>-[</kbd><var>R<sub>n</sub>R<sub>n+1</sub>...</var><kbd>]]</kbd> (with an <a href="#X_OPTION">"X" option</a>)</dt>
200 * <dd>Character class subtraction for the XML Schema.
201 * You can use this syntax when you specify an <a href="#X_OPTION">"X" option</a>.
203 * <dt class="REGEX"><kbd>\d</kbd>
204 * <dd class="REGEX">Equivalent to <kbd>[0-9]</kbd>.
205 * <dd>When <a href="#U_OPTION">a "u" option</a> is set, it is equivalent to
206 * <span class="REGEX"><kbd>\p{Nd}</kbd></span>.
208 * <dt class="REGEX"><kbd>\D</kbd>
209 * <dd class="REGEX">Equivalent to <kbd>[^0-9]</kbd>
210 * <dd>When <a href="#U_OPTION">a "u" option</a> is set, it is equivalent to
211 * <span class="REGEX"><kbd>\P{Nd}</kbd></span>.
213 * <dt class="REGEX"><kbd>\s</kbd>
214 * <dd class="REGEX">Equivalent to <kbd>[ \f\n\r\t]</kbd>
215 * <dd>When <a href="#U_OPTION">a "u" option</a> is set, it is equivalent to
216 * <span class="REGEX"><kbd>[ \f\n\r\t\p{Z}]</kbd></span>.
218 * <dt class="REGEX"><kbd>\S</kbd>
219 * <dd class="REGEX">Equivalent to <kbd>[^ \f\n\r\t]</kbd>
220 * <dd>When <a href="#U_OPTION">a "u" option</a> is set, it is equivalent to
221 * <span class="REGEX"><kbd>[^ \f\n\r\t\p{Z}]</kbd></span>.
223 * <dt class="REGEX"><kbd>\w</kbd>
224 * <dd class="REGEX">Equivalent to <kbd>[a-zA-Z0-9_]</kbd>
225 * <dd>When <a href="#U_OPTION">a "u" option</a> is set, it is equivalent to
226 * <span class="REGEX"><kbd>[\p{Lu}\p{Ll}\p{Lo}\p{Nd}_]</kbd></span>.
228 * <dt class="REGEX"><kbd>\W</kbd>
229 * <dd class="REGEX">Equivalent to <kbd>[^a-zA-Z0-9_]</kbd>
230 * <dd>When <a href="#U_OPTION">a "u" option</a> is set, it is equivalent to
231 * <span class="REGEX"><kbd>[^\p{Lu}\p{Ll}\p{Lo}\p{Nd}_]</kbd></span>.
233 * <dt class="REGEX"><kbd>\p{</kbd><var>name</var><kbd>}</kbd>
234 * <dd>Matches one character in the specified General Category (the second field in <a href="ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt"><kbd>UnicodeData.txt</kbd></a>) or the specified <a href="ftp://ftp.unicode.org/Public/UNIDATA/Blocks.txt">Block</a>.
235 * The following names are available:
237 * <dt>Unicode General Categories:
239 * L, M, N, Z, C, P, S, Lu, Ll, Lt, Lm, Lo, Mn, Me, Mc, Nd, Nl, No, Zs, Zl, Zp,
240 * Cc, Cf, Cn, Co, Cs, Pd, Ps, Pe, Pc, Po, Sm, Sc, Sk, So,
242 * <dd>(Currently the Cn category includes U+10000-U+10FFFF characters)
243 * <dt>Unicode Blocks:
245 * Basic Latin, Latin-1 Supplement, Latin Extended-A, Latin Extended-B,
246 * IPA Extensions, Spacing Modifier Letters, Combining Diacritical Marks, Greek,
247 * Cyrillic, Armenian, Hebrew, Arabic, Devanagari, Bengali, Gurmukhi, Gujarati,
248 * Oriya, Tamil, Telugu, Kannada, Malayalam, Thai, Lao, Tibetan, Georgian,
249 * Hangul Jamo, Latin Extended Additional, Greek Extended, General Punctuation,
250 * Superscripts and Subscripts, Currency Symbols, Combining Marks for Symbols,
251 * Letterlike Symbols, Number Forms, Arrows, Mathematical Operators,
252 * Miscellaneous Technical, Control Pictures, Optical Character Recognition,
253 * Enclosed Alphanumerics, Box Drawing, Block Elements, Geometric Shapes,
254 * Miscellaneous Symbols, Dingbats, CJK Symbols and Punctuation, Hiragana,
255 * Katakana, Bopomofo, Hangul Compatibility Jamo, Kanbun,
256 * Enclosed CJK Letters and Months, CJK Compatibility, CJK Unified Ideographs,
257 * Hangul Syllables, High Surrogates, High Private Use Surrogates, Low Surrogates,
258 * Private Use, CJK Compatibility Ideographs, Alphabetic Presentation Forms,
259 * Arabic Presentation Forms-A, Combining Half Marks, CJK Compatibility Forms,
260 * Small Form Variants, Arabic Presentation Forms-B, Specials,
261 * Halfwidth and Fullwidth Forms
264 * <dd><kbd>ALL</kbd> (Equivalent to <kbd>[\u005cu0000-\u005cv10FFFF]</kbd>)
265 * <dd><kbd>ASSGINED</kbd> (<kbd>\p{ASSIGNED}</kbd> is equivalent to <kbd>\P{Cn}</kbd>)
266 * <dd><kbd>UNASSGINED</kbd>
267 * (<kbd>\p{UNASSIGNED}</kbd> is equivalent to <kbd>\p{Cn}</kbd>)
270 * <dt class="REGEX"><kbd>\P{</kbd><var>name</var><kbd>}</kbd>
271 * <dd>Matches one character not in the specified General Category or the specified Block.
275 * <li>Selection and Quantifier
277 * <dt class="REGEX"><VAR>X</VAR><kbd>|</kbd><VAR>Y</VAR>
280 * <dt class="REGEX"><VAR>X</VAR><kbd>*</KBD>
281 * <dd>Matches 0 or more <var>X</var>.
283 * <dt class="REGEX"><VAR>X</VAR><kbd>+</KBD>
284 * <dd>Matches 1 or more <var>X</var>.
286 * <dt class="REGEX"><VAR>X</VAR><kbd>?</KBD>
287 * <dd>Matches 0 or 1 <var>X</var>.
289 * <dt class="REGEX"><var>X</var><kbd>{</kbd><var>number</var><kbd>}</kbd>
290 * <dd>Matches <var>number</var> times.
292 * <dt class="REGEX"><var>X</var><kbd>{</kbd><var>min</var><kbd>,}</kbd>
295 * <dt class="REGEX"><var>X</var><kbd>{</kbd><var>min</var><kbd>,</kbd><var>max</var><kbd>}</kbd>
298 * <dt class="REGEX"><VAR>X</VAR><kbd>*?</kbd>
299 * <dt class="REGEX"><VAR>X</VAR><kbd>+?</kbd>
300 * <dt class="REGEX"><VAR>X</VAR><kbd>??</kbd>
301 * <dt class="REGEX"><var>X</var><kbd>{</kbd><var>min</var><kbd>,}?</kbd>
302 * <dt class="REGEX"><var>X</var><kbd>{</kbd><var>min</var><kbd>,</kbd><var>max</var><kbd>}?</kbd>
303 * <dd>Non-greedy matching.
307 * <li>Grouping, Capturing, and Back-reference
309 * <dt class="REGEX"><KBD>(?:</kbd><VAR>X</VAR><kbd>)</KBD>
310 * <dd>Grouping. "<KBD>foo+</KBD>" matches "<KBD>foo</KBD>" or "<KBD>foooo</KBD>".
311 * If you want it matches "<KBD>foofoo</KBD>" or "<KBD>foofoofoo</KBD>",
312 * you have to write "<KBD>(?:foo)+</KBD>".
314 * <dt class="REGEX"><KBD>(</kbd><VAR>X</VAR><kbd>)</KBD>
315 * <dd>Grouping with capturing.
316 * It make a group and applications can know
317 * where in target text a group matched with methods of a <code>Match</code> instance
318 * after <code><a href="#matches(java.lang.String, org.apache.xerces.utils.regex.Match)">matches(String,Match)</a></code>.
319 * The 0th group means whole of this regular expression.
320 * The <VAR>N</VAR>th gorup is the inside of the <VAR>N</VAR>th left parenthesis.
322 * <p>For instance, a regular expression is
323 * "<FONT color=blue><KBD> *([^<:]*) +<([^>]*)> *</KBD></FONT>"
325 * "<FONT color=red><KBD>From: TAMURA Kent <kent@trl.ibm.co.jp></KBD></FONT>":
327 * <li><code>Match.getCapturedText(0)</code>:
328 * "<FONT color=red><KBD> TAMURA Kent <kent@trl.ibm.co.jp></KBD></FONT>"
329 * <li><code>Match.getCapturedText(1)</code>: "<FONT color=red><KBD>TAMURA Kent</KBD></FONT>"
330 * <li><code>Match.getCapturedText(2)</code>: "<FONT color=red><KBD>kent@trl.ibm.co.jp</KBD></FONT>"
333 * <dt class="REGEX"><kbd>\1 \2 \3 \4 \5 \6 \7 \8 \9</kbd>
336 * <dt class="REGEX"><kbd>(?></kbd><var>X</var><kbd>)</kbd>
337 * <dd>Independent expression group. ................
339 * <dt class="REGEX"><kbd>(?</kbd><var>options</var><kbd>:</kbd><var>X</var><kbd>)</kbd>
340 * <dt class="REGEX"><kbd>(?</kbd><var>options</var><kbd>-</kbd><var>options2</var><kbd>:</kbd><var>X</var><kbd>)</kbd>
341 * <dd>............................
342 * <dd>The <var>options</var> or the <var>options2</var> consists of 'i' 'm' 's' 'w'.
343 * Note that it can not contain 'u'.
345 * <dt class="REGEX"><kbd>(?</kbd><var>options</var><kbd>)</kbd>
346 * <dt class="REGEX"><kbd>(?</kbd><var>options</var><kbd>-</kbd><var>options2</var><kbd>)</kbd>
348 * <dd>These expressions must be at the beginning of a group.
354 * <dt class="REGEX"><kbd>\A</kbd>
355 * <dd>Matches the beginnig of the text.
357 * <dt class="REGEX"><kbd>\Z</kbd>
358 * <dd>Matches the end of the text, or before an EOL character at the end of the text,
359 * or CARRIAGE RETURN + LINE FEED at the end of the text.
361 * <dt class="REGEX"><kbd>\z</kbd>
362 * <dd>Matches the end of the text.
364 * <dt class="REGEX"><kbd>^</kbd>
365 * <dd>Matches the beginning of the text. It is equivalent to <span class="REGEX"><Kbd>\A</kbd></span>.
366 * <dd>When <a href="#M_OPTION">a "m" option</a> is set,
367 * it matches the beginning of the text, or after one of EOL characters (
368 * LINE FEED (U+000A), CARRIAGE RETURN (U+000D), LINE SEPARATOR (U+2028),
369 * PARAGRAPH SEPARATOR (U+2029).)
371 * <dt class="REGEX"><kbd>$</kbd>
372 * <dd>Matches the end of the text, or before an EOL character at the end of the text,
373 * or CARRIAGE RETURN + LINE FEED at the end of the text.
374 * <dd>When <a href="#M_OPTION">a "m" option</a> is set,
375 * it matches the end of the text, or before an EOL character.
377 * <dt class="REGEX"><kbd>\b</kbd>
378 * <dd>Matches word boundary.
379 * (See <a href="#W_OPTION">a "w" option</a>)
381 * <dt class="REGEX"><kbd>\B</kbd>
382 * <dd>Matches non word boundary.
383 * (See <a href="#W_OPTION">a "w" option</a>)
385 * <dt class="REGEX"><kbd>\<</kbd>
386 * <dd>Matches the beginning of a word.
387 * (See <a href="#W_OPTION">a "w" option</a>)
389 * <dt class="REGEX"><kbd>\></kbd>
390 * <dd>Matches the end of a word.
391 * (See <a href="#W_OPTION">a "w" option</a>)
394 * <li>Lookahead and lookbehind
396 * <dt class="REGEX"><kbd>(?=</kbd><var>X</var><kbd>)</kbd>
399 * <dt class="REGEX"><kbd>(?!</kbd><var>X</var><kbd>)</kbd>
400 * <dd>Negative lookahead.
402 * <dt class="REGEX"><kbd>(?<=</kbd><var>X</var><kbd>)</kbd>
404 * <dd>(Note for text capturing......)
406 * <dt class="REGEX"><kbd>(?<!</kbd><var>X</var><kbd>)</kbd>
407 * <dd>Negative lookbehind.
413 * <dt class="REGEX"><kbd>(?(</Kbd><var>condition</var><Kbd>)</kbd><var>yes-pattern</var><kbd>|</kbd><var>no-pattern</var><kbd>)</kbd>,
414 * <dt class="REGEX"><kbd>(?(</kbd><var>condition</var><kbd>)</kbd><var>yes-pattern</var><kbd>)</kbd>
416 * <dt class="REGEX"><kbd>(?#</kbd><var>comment</var><kbd>)</kbd>
417 * <dd>Comment. A comment string consists of characters except '<kbd>)</kbd>'.
418 * You can not write comments in character classes and before quantifiers.
425 * <h3>BNF for the regular expression</h3>
427 * regex ::= ('(?' options ')')? term ('|' term)*
429 * factor ::= anchors | atom (('*' | '+' | '?' | minmax ) '?'? )?
431 * minmax ::= '{' ([0-9]+ | [0-9]+ ',' | ',' [0-9]+ | [0-9]+ ',' [0-9]+) '}'
432 * atom ::= char | '.' | char-class | '(' regex ')' | '(?:' regex ')' | '\' [0-9]
433 * | '\w' | '\W' | '\d' | '\D' | '\s' | '\S' | category-block | '\X'
434 * | '(?>' regex ')' | '(?' options ':' regex ')'
435 * | '(?' ('(' [0-9] ')' | '(' anchors ')' | looks) term ('|' term)? ')'
436 * options ::= [imsw]* ('-' [imsw]+)?
437 * anchors ::= '^' | '$' | '\A' | '\Z' | '\z' | '\b' | '\B' | '\<' | '\>'
438 * looks ::= '(?=' regex ')' | '(?!' regex ')'
439 * | '(?<=' regex ')' | '(?<!' regex ')'
440 * char ::= '\\' | '\' [efnrtv] | '\c' [@-_] | code-point | character-1
441 * category-block ::= '\' [pP] category-symbol-1
442 * | ('\p{' | '\P{') (category-symbol | block-name
443 * | other-properties) '}'
444 * category-symbol-1 ::= 'L' | 'M' | 'N' | 'Z' | 'C' | 'P' | 'S'
445 * category-symbol ::= category-symbol-1 | 'Lu' | 'Ll' | 'Lt' | 'Lm' | Lo'
446 * | 'Mn' | 'Me' | 'Mc' | 'Nd' | 'Nl' | 'No'
447 * | 'Zs' | 'Zl' | 'Zp' | 'Cc' | 'Cf' | 'Cn' | 'Co' | 'Cs'
448 * | 'Pd' | 'Ps' | 'Pe' | 'Pc' | 'Po'
449 * | 'Sm' | 'Sc' | 'Sk' | 'So'
450 * block-name ::= (See above)
451 * other-properties ::= 'ALL' | 'ASSIGNED' | 'UNASSIGNED'
452 * character-1 ::= (any character except meta-characters)
454 * char-class ::= '[' ranges ']'
455 * | '(?[' ranges ']' ([-+&] '[' ranges ']')? ')'
456 * ranges ::= '^'? (range <a href="#COMMA_OPTION">','?</a>)+
457 * range ::= '\d' | '\w' | '\s' | '\D' | '\W' | '\S' | category-block
458 * | range-char | range-char '-' range-char
459 * range-char ::= '\[' | '\]' | '\\' | '\' [,-efnrtv] | code-point | character-2
460 * code-point ::= '\x' hex-char hex-char
461 * | '\x{' hex-char+ '}'
462 * <!-- | '\u005c u' hex-char hex-char hex-char hex-char
463 * --> | '\v' hex-char hex-char hex-char hex-char hex-char hex-char
464 * hex-char ::= [0-9a-fA-F]
465 * character-2 ::= (any character except \[]-,)
471 * <li><a href="http://www.unicode.org/unicode/reports/tr18/">Unicode Regular Expression Guidelines</a>
473 * <li>2.4 Canonical Equivalents
476 * <li>Parsing performance
483 * @author TAMURA Kent <kent@trl.ibm.co.jp>
484 * @version $Id: RegularExpression.java 961928 2010-07-08 20:43:46Z knoaman $
486 public class RegularExpression implements java.io.Serializable {
488 private static final long serialVersionUID = 6242499334195006401L;
490 static final boolean DEBUG = false;
493 * Compiles a token tree into an operation flow.
495 private synchronized void compile(Token tok) {
496 if (this.operations != null) {
499 this.numberOfClosures = 0;
500 this.operations = this.compile(tok, null, false);
504 * Converts a token to an operation.
506 private Op compile(Token tok, Op next, boolean reverse) {
510 ret = Op.createDot();
515 ret = Op.createChar(tok.getChar());
520 ret = Op.createAnchor(tok.getChar());
526 ret = Op.createRange(tok);
533 for (int i = tok.size()-1; i >= 0; i --) {
534 ret = compile(tok.getChild(i), ret, false);
537 for (int i = 0; i < tok.size(); i ++) {
538 ret = compile(tok.getChild(i), ret, true);
544 Op.UnionOp uni = Op.createUnion(tok.size());
545 for (int i = 0; i < tok.size(); i ++) {
546 uni.addElement(compile(tok.getChild(i), next, reverse));
548 ret = uni; // ret.next is null.
552 case Token.NONGREEDYCLOSURE:
553 Token child = tok.getChild(0);
554 int min = tok.getMin();
555 int max = tok.getMax();
556 if (min >= 0 && min == max) { // {n}
558 for (int i = 0; i < min; i ++) {
559 ret = compile(child, ret, reverse);
563 if (min > 0 && max > 0) {
567 // X{2,6} -> XX(X(X(XX?)?)?)?
569 for (int i = 0; i < max; i ++) {
570 Op.ChildOp q = Op.createQuestion(tok.type == Token.NONGREEDYCLOSURE);
572 q.setChild(compile(child, ret, reverse));
577 if (tok.type == Token.NONGREEDYCLOSURE) {
578 op = Op.createNonGreedyClosure();
579 } else { // Token.CLOSURE
580 op = Op.createClosure(this.numberOfClosures++);
583 op.setChild(compile(child, op, reverse));
587 for (int i = 0; i < min; i ++) {
588 ret = compile(child, ret, reverse);
598 ret = Op.createString(tok.getString());
602 case Token.BACKREFERENCE:
603 ret = Op.createBackReference(tok.getReferenceNumber());
608 if (tok.getParenNumber() == 0) {
609 ret = compile(tok.getChild(0), next, reverse);
610 } else if (reverse) {
611 next = Op.createCapture(tok.getParenNumber(), next);
612 next = compile(tok.getChild(0), next, reverse);
613 ret = Op.createCapture(-tok.getParenNumber(), next);
615 next = Op.createCapture(-tok.getParenNumber(), next);
616 next = compile(tok.getChild(0), next, reverse);
617 ret = Op.createCapture(tok.getParenNumber(), next);
621 case Token.LOOKAHEAD:
622 ret = Op.createLook(Op.LOOKAHEAD, next, compile(tok.getChild(0), null, false));
624 case Token.NEGATIVELOOKAHEAD:
625 ret = Op.createLook(Op.NEGATIVELOOKAHEAD, next, compile(tok.getChild(0), null, false));
627 case Token.LOOKBEHIND:
628 ret = Op.createLook(Op.LOOKBEHIND, next, compile(tok.getChild(0), null, true));
630 case Token.NEGATIVELOOKBEHIND:
631 ret = Op.createLook(Op.NEGATIVELOOKBEHIND, next, compile(tok.getChild(0), null, true));
634 case Token.INDEPENDENT:
635 ret = Op.createIndependent(next, compile(tok.getChild(0), null, reverse));
638 case Token.MODIFIERGROUP:
639 ret = Op.createModifier(next, compile(tok.getChild(0), null, reverse),
640 ((Token.ModifierToken)tok).getOptions(),
641 ((Token.ModifierToken)tok).getOptionsMask());
644 case Token.CONDITION:
645 Token.ConditionToken ctok = (Token.ConditionToken)tok;
646 int ref = ctok.refNumber;
647 Op condition = ctok.condition == null ? null : compile(ctok.condition, null, reverse);
648 Op yes = compile(ctok.yes, next, reverse);
649 Op no = ctok.no == null ? null : compile(ctok.no, next, reverse);
650 ret = Op.createCondition(next, ref, condition, yes, no);
654 throw new RuntimeException("Unknown token type: "+tok.type);
655 } // switch (tok.type)
663 * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.
665 * @return true if the target is matched to this regular expression.
667 public boolean matches(char[] target) {
668 return this.matches(target, 0, target .length , (Match)null);
672 * Checks whether the <var>target</var> text <strong>contains</strong> this pattern
673 * in specified range or not.
675 * @param start Start offset of the range.
676 * @param end End offset +1 of the range.
677 * @return true if the target is matched to this regular expression.
679 public boolean matches(char[] target, int start, int end) {
680 return this.matches(target, start, end, (Match)null);
684 * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.
686 * @param match A Match instance for storing matching result.
687 * @return Offset of the start position in <VAR>target</VAR>; or -1 if not match.
689 public boolean matches(char[] target, Match match) {
690 return this.matches(target, 0, target .length , match);
695 * Checks whether the <var>target</var> text <strong>contains</strong> this pattern
696 * in specified range or not.
698 * @param start Start offset of the range.
699 * @param end End offset +1 of the range.
700 * @param match A Match instance for storing matching result.
701 * @return Offset of the start position in <VAR>target</VAR>; or -1 if not match.
703 public boolean matches(char[] target, int start, int end, Match match) {
705 synchronized (this) {
706 if (this.operations == null) {
709 if (this.context == null) {
710 this.context = new Context();
714 synchronized (this.context) {
715 con = this.context.inuse ? new Context() : this.context;
716 con.reset(target, start, end, this.numberOfClosures);
719 match.setNumberOfGroups(this.nofparen);
720 match.setSource(target);
721 } else if (this.hasBackReferences) {
723 match.setNumberOfGroups(this.nofparen);
724 // Need not to call setSource() because
725 // a caller can not access this match instance.
729 if (RegularExpression.isSet(this.options, XMLSCHEMA_MODE)) {
730 int matchEnd = this. match(con, this.operations, con.start, 1, this.options);
731 //System.err.println("DEBUG: matchEnd="+matchEnd);
732 if (matchEnd == con.limit) {
733 if (con.match != null) {
734 con.match.setBeginning(0, con.start);
735 con.match.setEnd(0, matchEnd);
744 * The pattern has only fixed string.
745 * The engine uses Boyer-Moore.
747 if (this.fixedStringOnly) {
748 //System.err.println("DEBUG: fixed-only: "+this.fixedString);
749 int o = this.fixedStringTable.matches(target, con.start, con.limit);
751 if (con.match != null) {
752 con.match.setBeginning(0, o);
753 con.match.setEnd(0, o+this.fixedString.length());
763 * The pattern contains a fixed string.
764 * The engine checks with Boyer-Moore whether the text contains the fixed string or not.
765 * If not, it return with false.
767 if (this.fixedString != null) {
768 int o = this.fixedStringTable.matches(target, con.start, con.limit);
770 //System.err.println("Non-match in fixed-string search.");
776 int limit = con.limit-this.minlength;
781 * Checks whether the expression starts with ".*".
783 if (this.operations != null
784 && this.operations.type == Op.CLOSURE && this.operations.getChild().type == Op.DOT) {
785 if (isSet(this.options, SINGLE_LINE)) {
786 matchStart = con.start;
787 matchEnd = this. match(con, this.operations, con.start, 1, this.options);
789 boolean previousIsEOL = true;
790 for (matchStart = con.start; matchStart <= limit; matchStart ++) {
791 int ch = target [ matchStart ] ;
793 previousIsEOL = true;
796 if (0 <= (matchEnd = this. match(con, this.operations,
797 matchStart, 1, this.options))) {
801 previousIsEOL = false;
808 * Optimization against the first character.
810 else if (this.firstChar != null) {
811 //System.err.println("DEBUG: with firstchar-matching: "+this.firstChar);
812 RangeToken range = this.firstChar;
813 for (matchStart = con.start; matchStart <= limit; matchStart ++) {
814 int ch = target [matchStart] ;
815 if (REUtil.isHighSurrogate(ch) && matchStart+1 < con.limit) {
816 ch = REUtil.composeFromSurrogates(ch, target[matchStart+1]);
818 if (!range.match(ch)) {
821 if (0 <= (matchEnd = this. match(con, this.operations,
822 matchStart, 1, this.options))) {
829 * Straightforward matching.
832 for (matchStart = con.start; matchStart <= limit; matchStart ++) {
833 if (0 <= (matchEnd = this. match(con, this.operations, matchStart, 1, this.options))) {
840 if (con.match != null) {
841 con.match.setBeginning(0, matchStart);
842 con.match.setEnd(0, matchEnd);
853 * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.
855 * @return true if the target is matched to this regular expression.
857 public boolean matches(String target) {
858 return this.matches(target, 0, target .length() , (Match)null);
862 * Checks whether the <var>target</var> text <strong>contains</strong> this pattern
863 * in specified range or not.
865 * @param start Start offset of the range.
866 * @param end End offset +1 of the range.
867 * @return true if the target is matched to this regular expression.
869 public boolean matches(String target, int start, int end) {
870 return this.matches(target, start, end, (Match)null);
874 * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.
876 * @param match A Match instance for storing matching result.
877 * @return Offset of the start position in <VAR>target</VAR>; or -1 if not match.
879 public boolean matches(String target, Match match) {
880 return this.matches(target, 0, target .length() , match);
884 * Checks whether the <var>target</var> text <strong>contains</strong> this pattern
885 * in specified range or not.
887 * @param start Start offset of the range.
888 * @param end End offset +1 of the range.
889 * @param match A Match instance for storing matching result.
890 * @return Offset of the start position in <VAR>target</VAR>; or -1 if not match.
892 public boolean matches(String target, int start, int end, Match match) {
894 synchronized (this) {
895 if (this.operations == null) {
898 if (this.context == null) {
899 this.context = new Context();
903 synchronized (this.context) {
904 con = this.context.inuse ? new Context() : this.context;
905 con.reset(target, start, end, this.numberOfClosures);
908 match.setNumberOfGroups(this.nofparen);
909 match.setSource(target);
910 } else if (this.hasBackReferences) {
912 match.setNumberOfGroups(this.nofparen);
913 // Need not to call setSource() because
914 // a caller can not access this match instance.
918 if (RegularExpression.isSet(this.options, XMLSCHEMA_MODE)) {
920 System.err.println("target string="+target);
922 int matchEnd = this. match(con, this.operations, con.start, 1, this.options);
924 System.err.println("matchEnd="+matchEnd);
925 System.err.println("con.limit="+con.limit);
927 if (matchEnd == con.limit) {
928 if (con.match != null) {
929 con.match.setBeginning(0, con.start);
930 con.match.setEnd(0, matchEnd);
939 * The pattern has only fixed string.
940 * The engine uses Boyer-Moore.
942 if (this.fixedStringOnly) {
943 //System.err.println("DEBUG: fixed-only: "+this.fixedString);
944 int o = this.fixedStringTable.matches(target, con.start, con.limit);
946 if (con.match != null) {
947 con.match.setBeginning(0, o);
948 con.match.setEnd(0, o+this.fixedString.length());
958 * The pattern contains a fixed string.
959 * The engine checks with Boyer-Moore whether the text contains the fixed string or not.
960 * If not, it return with false.
962 if (this.fixedString != null) {
963 int o = this.fixedStringTable.matches(target, con.start, con.limit);
965 //System.err.println("Non-match in fixed-string search.");
971 int limit = con.limit-this.minlength;
976 * Checks whether the expression starts with ".*".
978 if (this.operations != null
979 && this.operations.type == Op.CLOSURE && this.operations.getChild().type == Op.DOT) {
980 if (isSet(this.options, SINGLE_LINE)) {
981 matchStart = con.start;
982 matchEnd = this.match(con, this.operations, con.start, 1, this.options);
984 boolean previousIsEOL = true;
985 for (matchStart = con.start; matchStart <= limit; matchStart ++) {
986 int ch = target .charAt( matchStart ) ;
988 previousIsEOL = true;
991 if (0 <= (matchEnd = this.match(con, this.operations,
992 matchStart, 1, this.options))) {
996 previousIsEOL = false;
1003 * Optimization against the first character.
1005 else if (this.firstChar != null) {
1006 //System.err.println("DEBUG: with firstchar-matching: "+this.firstChar);
1007 RangeToken range = this.firstChar;
1008 for (matchStart = con.start; matchStart <= limit; matchStart ++) {
1009 int ch = target .charAt( matchStart ) ;
1010 if (REUtil.isHighSurrogate(ch) && matchStart+1 < con.limit) {
1011 ch = REUtil.composeFromSurrogates(ch, target.charAt(matchStart+1));
1013 if (!range.match(ch)) {
1016 if (0 <= (matchEnd = this.match(con, this.operations,
1017 matchStart, 1, this.options))) {
1024 * Straightforward matching.
1027 for (matchStart = con.start; matchStart <= limit; matchStart ++) {
1028 if (0 <= (matchEnd = this.match(con, this.operations, matchStart, 1, this.options))) {
1034 if (matchEnd >= 0) {
1035 if (con.match != null) {
1036 con.match.setBeginning(0, matchStart);
1037 con.match.setEnd(0, matchEnd);
1039 con.setInUse(false);
1042 con.setInUse(false);
1048 * @return -1 when not match; offset of the end of matched string when match.
1050 private int match(Context con, Op op, int offset, int dx, int opts) {
1051 final ExpressionTarget target = con.target;
1052 final Stack<Op> opStack = new Stack<>();
1053 final Stack<Integer> dataStack = new Stack<>();
1054 final boolean isSetIgnoreCase = isSet(opts, IGNORE_CASE);
1056 boolean returned = false;
1059 if (op == null || offset > con.limit || offset < con.start) {
1061 retValue = isSet(opts, XMLSCHEMA_MODE) && offset != con.limit ? -1 : offset;
1070 // dx value is either 1 or -1
1074 final int o1 = (dx > 0) ? offset : offset -1;
1075 if (o1 >= con.limit || o1 < 0 || !matchChar(op.getData(), target.charAt(o1), isSetIgnoreCase)) {
1086 int o1 = (dx > 0) ? offset : offset - 1;
1087 if (o1 >= con.limit || o1 < 0) {
1091 if (isSet(opts, SINGLE_LINE)) {
1092 if (REUtil.isHighSurrogate(target.charAt(o1)) && o1+dx >= 0 && o1+dx < con.limit) {
1097 int ch = target.charAt(o1);
1098 if (REUtil.isHighSurrogate(ch) && o1+dx >= 0 && o1+dx < con.limit) {
1100 ch = REUtil.composeFromSurrogates(ch, target.charAt(o1));
1102 if (isEOLChar(ch)) {
1107 offset = (dx > 0) ? o1 + 1 : o1;
1115 int o1 = (dx > 0) ? offset : offset -1;
1116 if (o1 >= con.limit || o1 < 0) {
1120 int ch = target.charAt(offset);
1121 if (REUtil.isHighSurrogate(ch) && o1+dx < con.limit && o1+dx >=0) {
1123 ch = REUtil.composeFromSurrogates(ch, target.charAt(o1));
1125 final RangeToken tok = op.getToken();
1126 if (!tok.match(ch)) {
1130 offset = (dx > 0) ? o1+1 : o1;
1137 if (!matchAnchor(target, op, con, offset, opts)) {
1145 case Op.BACKREFERENCE:
1147 int refno = op.getData();
1148 if (refno <= 0 || refno >= this.nofparen) {
1149 throw new RuntimeException("Internal Error: Reference number must be more than zero: "+refno);
1151 if (con.match.getBeginning(refno) < 0 || con.match.getEnd(refno) < 0) {
1155 int o2 = con.match.getBeginning(refno);
1156 int literallen = con.match.getEnd(refno)-o2;
1158 if (!target.regionMatches(isSetIgnoreCase, offset, con.limit, o2, literallen)) {
1162 offset += literallen;
1165 if (!target.regionMatches(isSetIgnoreCase, offset-literallen, con.limit, o2, literallen)) {
1169 offset -= literallen;
1177 String literal = op.getString();
1178 int literallen = literal.length();
1180 if (!target.regionMatches(isSetIgnoreCase, offset, con.limit, literal, literallen)) {
1184 offset += literallen;
1187 if (!target.regionMatches(isSetIgnoreCase, offset-literallen, con.limit, literal, literallen)) {
1191 offset -= literallen;
1199 // Saves current position to avoid zero-width repeats.
1200 final int id = op.getData();
1201 if (con.closureContexts[id].contains(offset)) {
1206 con.closureContexts[id].addOffset(offset);
1213 dataStack.push(offset);
1218 case Op.NONGREEDYCLOSURE:
1219 case Op.NONGREEDYQUESTION:
1222 dataStack.push(offset);
1228 if (op.size() == 0) {
1234 dataStack.push(offset);
1235 op = op.elementAt(0);
1241 final int refno = op.getData();
1242 if (con.match != null) {
1244 dataStack.push(con.match.getBeginning(refno));
1245 con.match.setBeginning(refno, offset);
1248 final int index = -refno;
1249 dataStack.push(con.match.getEnd(index));
1250 con.match.setEnd(index, offset);
1253 dataStack.push(offset);
1260 case Op.NEGATIVELOOKAHEAD:
1262 case Op.NEGATIVELOOKBEHIND:
1266 dataStack.push(offset);
1267 dx = (op.type == Op.LOOKAHEAD || op.type == Op.NEGATIVELOOKAHEAD) ? 1 : -1;
1272 case Op.INDEPENDENT:
1275 dataStack.push(offset);
1282 int localopts = opts;
1283 localopts |= op.getData();
1284 localopts &= ~op.getData2();
1286 dataStack.push(opts);
1287 dataStack.push(offset);
1295 Op.ConditionOp cop = (Op.ConditionOp)op;
1296 if (cop.refNumber > 0) {
1297 if (cop.refNumber >= this.nofparen) {
1298 throw new RuntimeException("Internal Error: Reference number must be more than zero: "+cop.refNumber);
1300 if (con.match.getBeginning(cop.refNumber) >= 0
1301 && con.match.getEnd(cop.refNumber) >= 0) {
1304 else if (cop.no != null) {
1313 dataStack.push(offset);
1320 throw new RuntimeException("Unknown operation type: " + op.type);
1324 // handle recursive operations
1326 // exhausted all the operations
1327 if (opStack.isEmpty()) {
1332 offset = dataStack.pop();
1343 case Op.NONGREEDYCLOSURE:
1344 case Op.NONGREEDYQUESTION:
1353 int unionIndex = dataStack.pop();
1355 System.err.println("UNION: "+unionIndex+", ret="+retValue);
1359 if (++unionIndex < op.size()) {
1361 dataStack.push(unionIndex);
1362 dataStack.push(offset);
1363 op = op.elementAt(unionIndex);
1374 final int refno = op.getData();
1375 final int saved = dataStack.pop();
1378 con.match.setBeginning(refno, saved);
1381 con.match.setEnd(-refno, saved);
1389 dx = dataStack.pop();
1390 if (0 <= retValue) {
1398 case Op.NEGATIVELOOKAHEAD:
1399 case Op.NEGATIVELOOKBEHIND:
1401 dx = dataStack.pop();
1411 opts = dataStack.pop();
1414 case Op.INDEPENDENT:
1415 if (retValue >= 0) {
1424 final Op.ConditionOp cop = (Op.ConditionOp)op;
1425 if (0 <= retValue) {
1428 else if (cop.no != null) {
1445 private static boolean matchChar(int ch, int other, boolean ignoreCase) {
1446 return (ignoreCase) ? matchIgnoreCase(ch, other) : ch == other;
1449 boolean matchAnchor(ExpressionTarget target, Op op, Context con, int offset, int opts) {
1451 switch (op.getData()) {
1453 if (isSet(opts, MULTIPLE_LINES)) {
1454 if (!(offset == con.start
1455 || offset > con.start && offset < con.limit && isEOLChar(target.charAt(offset-1)))) {
1459 if (offset != con.start) {
1465 case '@': // Internal use only.
1466 // The @ always matches line beginnings.
1467 if (!(offset == con.start
1468 || offset > con.start && isEOLChar(target.charAt(offset-1)))) {
1474 if (isSet(opts, MULTIPLE_LINES)) {
1475 if (!(offset == con.limit
1476 || offset < con.limit && isEOLChar(target.charAt(offset)))) {
1480 if (!(offset == con.limit
1481 || offset+1 == con.limit && isEOLChar(target.charAt(offset))
1482 || offset+2 == con.limit && target.charAt(offset) == CARRIAGE_RETURN
1483 && target.charAt(offset+1) == LINE_FEED)) {
1490 if (offset != con.start) {
1496 if (!(offset == con.limit
1497 || offset+1 == con.limit && isEOLChar(target.charAt(offset))
1498 || offset+2 == con.limit && target.charAt(offset) == CARRIAGE_RETURN
1499 && target.charAt(offset+1) == LINE_FEED)) {
1505 if (offset != con.limit) {
1511 if (con.length == 0) {
1515 int after = getWordType(target, con.start, con.limit, offset, opts);
1516 if (after == WT_IGNORE) {
1519 int before = getPreviousWordType(target, con.start, con.limit, offset, opts);
1520 if (after == before) {
1527 if (con.length == 0) {
1530 int after = getWordType(target, con.start, con.limit, offset, opts);
1531 go = after == WT_IGNORE
1532 || after == getPreviousWordType(target, con.start, con.limit, offset, opts);
1540 if (con.length == 0 || offset == con.limit) {
1543 if (getWordType(target, con.start, con.limit, offset, opts) != WT_LETTER
1544 || getPreviousWordType(target, con.start, con.limit, offset, opts) != WT_OTHER) {
1550 if (con.length == 0 || offset == con.start) {
1553 if (getWordType(target, con.start, con.limit, offset, opts) != WT_OTHER
1554 || getPreviousWordType(target, con.start, con.limit, offset, opts) != WT_LETTER) {
1558 } // switch anchor type
1563 private static final int getPreviousWordType(ExpressionTarget target, int begin, int end,
1564 int offset, int opts) {
1565 int ret = getWordType(target, begin, end, --offset, opts);
1566 while (ret == WT_IGNORE) {
1567 ret = getWordType(target, begin, end, --offset, opts);
1572 private static final int getWordType(ExpressionTarget target, int begin, int end,
1573 int offset, int opts) {
1574 if (offset < begin || offset >= end) {
1577 return getWordType0(target.charAt(offset) , opts);
1582 * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.
1584 * @return true if the target is matched to this regular expression.
1586 public boolean matches(CharacterIterator target) {
1587 return this.matches(target, (Match)null);
1592 * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.
1594 * @param match A Match instance for storing matching result.
1595 * @return Offset of the start position in <VAR>target</VAR>; or -1 if not match.
1597 public boolean matches(CharacterIterator target, Match match) {
1598 int start = target.getBeginIndex();
1599 int end = target.getEndIndex();
1603 synchronized (this) {
1604 if (this.operations == null) {
1607 if (this.context == null) {
1608 this.context = new Context();
1612 synchronized (this.context) {
1613 con = this.context.inuse ? new Context() : this.context;
1614 con.reset(target, start, end, this.numberOfClosures);
1616 if (match != null) {
1617 match.setNumberOfGroups(this.nofparen);
1618 match.setSource(target);
1619 } else if (this.hasBackReferences) {
1620 match = new Match();
1621 match.setNumberOfGroups(this.nofparen);
1622 // Need not to call setSource() because
1623 // a caller can not access this match instance.
1627 if (RegularExpression.isSet(this.options, XMLSCHEMA_MODE)) {
1628 int matchEnd = this.match(con, this.operations, con.start, 1, this.options);
1629 //System.err.println("DEBUG: matchEnd="+matchEnd);
1630 if (matchEnd == con.limit) {
1631 if (con.match != null) {
1632 con.match.setBeginning(0, con.start);
1633 con.match.setEnd(0, matchEnd);
1635 con.setInUse(false);
1642 * The pattern has only fixed string.
1643 * The engine uses Boyer-Moore.
1645 if (this.fixedStringOnly) {
1646 //System.err.println("DEBUG: fixed-only: "+this.fixedString);
1647 int o = this.fixedStringTable.matches(target, con.start, con.limit);
1649 if (con.match != null) {
1650 con.match.setBeginning(0, o);
1651 con.match.setEnd(0, o+this.fixedString.length());
1653 con.setInUse(false);
1656 con.setInUse(false);
1661 * The pattern contains a fixed string.
1662 * The engine checks with Boyer-Moore whether the text contains the fixed string or not.
1663 * If not, it return with false.
1665 if (this.fixedString != null) {
1666 int o = this.fixedStringTable.matches(target, con.start, con.limit);
1668 //System.err.println("Non-match in fixed-string search.");
1669 con.setInUse(false);
1674 int limit = con.limit-this.minlength;
1679 * Checks whether the expression starts with ".*".
1681 if (this.operations != null
1682 && this.operations.type == Op.CLOSURE && this.operations.getChild().type == Op.DOT) {
1683 if (isSet(this.options, SINGLE_LINE)) {
1684 matchStart = con.start;
1685 matchEnd = this.match(con, this.operations, con.start, 1, this.options);
1687 boolean previousIsEOL = true;
1688 for (matchStart = con.start; matchStart <= limit; matchStart ++) {
1689 int ch = target .setIndex( matchStart ) ;
1690 if (isEOLChar(ch)) {
1691 previousIsEOL = true;
1693 if (previousIsEOL) {
1694 if (0 <= (matchEnd = this.match(con, this.operations,
1695 matchStart, 1, this.options))) {
1699 previousIsEOL = false;
1706 * Optimization against the first character.
1708 else if (this.firstChar != null) {
1709 //System.err.println("DEBUG: with firstchar-matching: "+this.firstChar);
1710 RangeToken range = this.firstChar;
1711 for (matchStart = con.start; matchStart <= limit; matchStart ++) {
1712 int ch = target .setIndex( matchStart ) ;
1713 if (REUtil.isHighSurrogate(ch) && matchStart+1 < con.limit) {
1714 ch = REUtil.composeFromSurrogates(ch, target.setIndex(matchStart+1));
1716 if (!range.match(ch)) {
1719 if (0 <= (matchEnd = this.match(con, this.operations,
1720 matchStart, 1, this.options))) {
1727 * Straightforward matching.
1730 for (matchStart = con.start; matchStart <= limit; matchStart ++) {
1731 if (0 <= (matchEnd = this. match(con, this.operations, matchStart, 1, this.options))) {
1737 if (matchEnd >= 0) {
1738 if (con.match != null) {
1739 con.match.setBeginning(0, matchStart);
1740 con.match.setEnd(0, matchEnd);
1742 con.setInUse(false);
1745 con.setInUse(false);
1750 // ================================================================
1753 * A regular expression.
1763 * The number of parenthesis in the regular expression.
1768 * Internal representation of the regular expression.
1773 boolean hasBackReferences = false;
1775 transient int minlength;
1776 transient Op operations = null;
1777 transient int numberOfClosures;
1778 transient Context context = null;
1779 transient RangeToken firstChar = null;
1781 transient String fixedString = null;
1782 transient int fixedStringOptions;
1783 transient BMPattern fixedStringTable = null;
1784 transient boolean fixedStringOnly = false;
1786 static abstract class ExpressionTarget {
1787 abstract char charAt(int index);
1788 abstract boolean regionMatches(boolean ignoreCase, int offset, int limit, String part, int partlen);
1789 abstract boolean regionMatches(boolean ignoreCase, int offset, int limit, int offset2, int partlen);
1792 static final class StringTarget extends ExpressionTarget {
1794 private String target;
1796 StringTarget(String target) {
1797 this.target = target;
1800 final void resetTarget(String target) {
1801 this.target = target;
1805 final char charAt(int index) {
1806 return target.charAt(index);
1810 final boolean regionMatches(boolean ignoreCase, int offset, int limit,
1811 String part, int partlen) {
1812 if (limit-offset < partlen) {
1815 return (ignoreCase) ? target.regionMatches(true, offset, part, 0, partlen) : target.regionMatches(offset, part, 0, partlen);
1819 final boolean regionMatches(boolean ignoreCase, int offset, int limit,
1820 int offset2, int partlen) {
1821 if (limit-offset < partlen) {
1824 return (ignoreCase) ? target.regionMatches(true, offset, target, offset2, partlen)
1825 : target.regionMatches(offset, target, offset2, partlen);
1829 static final class CharArrayTarget extends ExpressionTarget {
1833 CharArrayTarget(char[] target) {
1834 this.target = target;
1837 final void resetTarget(char[] target) {
1838 this.target = target;
1842 char charAt(int index) {
1843 return target[index];
1847 final boolean regionMatches(boolean ignoreCase, int offset, int limit,
1848 String part, int partlen) {
1849 if (offset < 0 || limit-offset < partlen) {
1852 return (ignoreCase) ? regionMatchesIgnoreCase(offset, limit, part, partlen)
1853 : regionMatches(offset, limit, part, partlen);
1856 private final boolean regionMatches(int offset, int limit, String part, int partlen) {
1858 while (partlen-- > 0) {
1859 if (target[offset++] != part.charAt(i++)) {
1866 private final boolean regionMatchesIgnoreCase(int offset, int limit, String part, int partlen) {
1868 while (partlen-- > 0) {
1869 final char ch1 = target[offset++] ;
1870 final char ch2 = part.charAt(i++);
1874 final char uch1 = Character.toUpperCase(ch1);
1875 final char uch2 = Character.toUpperCase(ch2);
1879 if (Character.toLowerCase(uch1) != Character.toLowerCase(uch2)) {
1887 final boolean regionMatches(boolean ignoreCase, int offset, int limit, int offset2, int partlen) {
1888 if (offset < 0 || limit-offset < partlen) {
1891 return (ignoreCase) ? regionMatchesIgnoreCase(offset, limit, offset2, partlen)
1892 : regionMatches(offset, limit, offset2, partlen);
1895 private final boolean regionMatches(int offset, int limit, int offset2, int partlen) {
1897 while (partlen-- > 0) {
1898 if ( target [ offset++ ] != target [ i++ ] ) {
1905 private final boolean regionMatchesIgnoreCase(int offset, int limit, int offset2, int partlen) {
1907 while (partlen-- > 0) {
1908 final char ch1 = target[offset++] ;
1909 final char ch2 = target[i++] ;
1913 final char uch1 = Character.toUpperCase(ch1);
1914 final char uch2 = Character.toUpperCase(ch2);
1918 if (Character.toLowerCase(uch1) != Character.toLowerCase(uch2)) {
1926 static final class CharacterIteratorTarget extends ExpressionTarget {
1927 CharacterIterator target;
1929 CharacterIteratorTarget(CharacterIterator target) {
1930 this.target = target;
1933 final void resetTarget(CharacterIterator target) {
1934 this.target = target;
1938 final char charAt(int index) {
1939 return target.setIndex(index);
1943 final boolean regionMatches(boolean ignoreCase, int offset, int limit,
1944 String part, int partlen) {
1945 if (offset < 0 || limit-offset < partlen) {
1948 return (ignoreCase) ? regionMatchesIgnoreCase(offset, limit, part, partlen)
1949 : regionMatches(offset, limit, part, partlen);
1952 private final boolean regionMatches(int offset, int limit, String part, int partlen) {
1954 while (partlen-- > 0) {
1955 if (target.setIndex(offset++) != part.charAt(i++)) {
1962 private final boolean regionMatchesIgnoreCase(int offset, int limit, String part, int partlen) {
1964 while (partlen-- > 0) {
1965 final char ch1 = target.setIndex(offset++) ;
1966 final char ch2 = part.charAt(i++);
1970 final char uch1 = Character.toUpperCase(ch1);
1971 final char uch2 = Character.toUpperCase(ch2);
1975 if (Character.toLowerCase(uch1) != Character.toLowerCase(uch2)) {
1983 final boolean regionMatches(boolean ignoreCase, int offset, int limit, int offset2, int partlen) {
1984 if (offset < 0 || limit-offset < partlen) {
1987 return (ignoreCase) ? regionMatchesIgnoreCase(offset, limit, offset2, partlen)
1988 : regionMatches(offset, limit, offset2, partlen);
1991 private final boolean regionMatches(int offset, int limit, int offset2, int partlen) {
1993 while (partlen-- > 0) {
1994 if (target.setIndex(offset++) != target.setIndex(i++)) {
2001 private final boolean regionMatchesIgnoreCase(int offset, int limit, int offset2, int partlen) {
2003 while (partlen-- > 0) {
2004 final char ch1 = target.setIndex(offset++) ;
2005 final char ch2 = target.setIndex(i++) ;
2009 final char uch1 = Character.toUpperCase(ch1);
2010 final char uch2 = Character.toUpperCase(ch2);
2014 if (Character.toLowerCase(uch1) != Character.toLowerCase(uch2)) {
2022 static final class ClosureContext {
2024 int[] offsets = new int[4];
2025 int currentIndex = 0;
2027 boolean contains(int offset) {
2028 for (int i=0; i<currentIndex;++i) {
2029 if (offsets[i] == offset) {
2040 void addOffset(int offset) {
2041 // We do not check for duplicates, caller is responsible for that
2042 if (currentIndex == offsets.length) {
2043 offsets = expandOffsets();
2045 offsets[currentIndex++] = offset;
2048 private int[] expandOffsets() {
2049 final int len = offsets.length;
2050 final int newLen = len << 1;
2051 int[] newOffsets = new int[newLen];
2053 System.arraycopy(offsets, 0, newOffsets, 0, currentIndex);
2058 static final class Context {
2063 boolean inuse = false;
2064 ClosureContext[] closureContexts;
2066 private StringTarget stringTarget;
2067 private CharArrayTarget charArrayTarget;
2068 private CharacterIteratorTarget characterIteratorTarget;
2070 ExpressionTarget target;
2075 private void resetCommon(int nofclosures) {
2076 this.length = this.limit-this.start;
2079 if (this.closureContexts == null || this.closureContexts.length != nofclosures) {
2080 this.closureContexts = new ClosureContext[nofclosures];
2082 for (int i = 0; i < nofclosures; i ++) {
2083 if (this.closureContexts[i] == null) {
2084 this.closureContexts[i] = new ClosureContext();
2087 this.closureContexts[i].reset();
2092 void reset(CharacterIterator target, int start, int limit, int nofclosures) {
2093 if (characterIteratorTarget == null) {
2094 characterIteratorTarget = new CharacterIteratorTarget(target);
2097 characterIteratorTarget.resetTarget(target);
2099 this.target = characterIteratorTarget;
2102 this.resetCommon(nofclosures);
2105 void reset(String target, int start, int limit, int nofclosures) {
2106 if (stringTarget == null) {
2107 stringTarget = new StringTarget(target);
2110 stringTarget.resetTarget(target);
2112 this.target = stringTarget;
2115 this.resetCommon(nofclosures);
2118 void reset(char[] target, int start, int limit, int nofclosures) {
2119 if (charArrayTarget == null) {
2120 charArrayTarget = new CharArrayTarget(target);
2123 charArrayTarget.resetTarget(target);
2125 this.target = charArrayTarget;
2128 this.resetCommon(nofclosures);
2130 synchronized void setInUse(boolean inUse) {
2136 * Prepares for matching. This method is called just before starting matching.
2140 Op.nofinstances = 0;
2142 this.compile(this.tokentree);
2144 if (this.operations.type == Op.CLOSURE && this.operations.getChild().type == Op.DOT) { // .*
2145 Op anchor = Op.createAnchor(isSet(this.options, SINGLE_LINE) ? 'A' : '@');
2146 anchor.next = this.operations;
2147 this.operations = anchor;
2151 System.err.println("DEBUG: The number of operations: "+Op.nofinstances);
2154 this.minlength = this.tokentree.getMinLength();
2156 this.firstChar = null;
2157 if (!isSet(this.options, PROHIBIT_HEAD_CHARACTER_OPTIMIZATION)
2158 && !isSet(this.options, XMLSCHEMA_MODE)) {
2159 RangeToken firstChar = Token.createRange();
2160 int fresult = this.tokentree.analyzeFirstCharacter(firstChar, this.options);
2161 if (fresult == Token.FC_TERMINAL) {
2162 firstChar.compactRanges();
2163 this.firstChar = firstChar;
2165 System.err.println("DEBUG: Use the first character optimization: "+firstChar);
2170 if (this.operations != null
2171 && (this.operations.type == Op.STRING || this.operations.type == Op.CHAR)
2172 && this.operations.next == null) {
2174 System.err.print(" *** Only fixed string! *** ");
2176 this.fixedStringOnly = true;
2177 if (this.operations.type == Op.STRING) {
2178 this.fixedString = this.operations.getString();
2179 } else if (this.operations.getData() >= 0x10000) { // Op.CHAR
2180 this.fixedString = REUtil.decomposeToSurrogates(this.operations.getData());
2182 char[] ac = new char[1];
2183 ac[0] = (char)this.operations.getData();
2184 this.fixedString = new String(ac);
2186 this.fixedStringOptions = this.options;
2187 this.fixedStringTable = new BMPattern(this.fixedString, 256,
2188 isSet(this.fixedStringOptions, IGNORE_CASE));
2189 } else if (!isSet(this.options, PROHIBIT_FIXED_STRING_OPTIMIZATION)
2190 && !isSet(this.options, XMLSCHEMA_MODE)) {
2191 Token.FixedStringContainer container = new Token.FixedStringContainer();
2192 this.tokentree.findFixedString(container, this.options);
2193 this.fixedString = container.token == null ? null : container.token.getString();
2194 this.fixedStringOptions = container.options;
2195 if (this.fixedString != null && this.fixedString.length() < 2) {
2196 this.fixedString = null;
2198 // This pattern has a fixed string of which length is more than one.
2199 if (this.fixedString != null) {
2200 this.fixedStringTable = new BMPattern(this.fixedString, 256,
2201 isSet(this.fixedStringOptions, IGNORE_CASE));
2203 System.err.println("DEBUG: The longest fixed string: "+this.fixedString.length()
2204 +"/" //+this.fixedString
2205 +"/"+REUtil.createOptionString(this.fixedStringOptions));
2206 System.err.print("String: ");
2207 REUtil.dumpString(this.fixedString);
2215 * If you specify this option, <span class="REGEX"><kbd>(</kbd><var>X</var><kbd>)</kbd></span>
2216 * captures matched text, and <span class="REGEX"><kbd>(:?</kbd><var>X</var><kbd>)</kbd></span>
2219 * @see #RegularExpression(java.lang.String,int)
2220 * @see #setPattern(java.lang.String,int)
2221 static final int MARK_PARENS = 1<<0;
2227 static final int IGNORE_CASE = 1<<1;
2232 static final int SINGLE_LINE = 1<<2;
2237 static final int MULTIPLE_LINES = 1<<3;
2242 static final int EXTENDED_COMMENT = 1<<4;
2245 * This option redefines <span class="REGEX"><kbd>\d \D \w \W \s \S</kbd></span>.
2247 * @see #RegularExpression(java.lang.String,int)
2248 * @see #setPattern(java.lang.String,int)
2249 * @see #UNICODE_WORD_BOUNDARY
2251 static final int USE_UNICODE_CATEGORY = 1<<5; // "u"
2255 * This enables to process locale-independent word boundary for <span class="REGEX"><kbd>\b \B \< \></kbd></span>.
2256 * <p>By default, the engine considers a position between a word character
2257 * (<span class="REGEX"><Kbd>\w</kbd></span>) and a non word character
2258 * is a word boundary.
2259 * <p>By this option, the engine checks word boundaries with the method of
2260 * 'Unicode Regular Expression Guidelines' Revision 4.
2262 * @see #RegularExpression(java.lang.String,int)
2263 * @see #setPattern(java.lang.String,int)
2265 static final int UNICODE_WORD_BOUNDARY = 1<<6; // "w"
2270 static final int PROHIBIT_HEAD_CHARACTER_OPTIMIZATION = 1<<7;
2274 static final int PROHIBIT_FIXED_STRING_OPTIMIZATION = 1<<8;
2276 * "X". XML Schema mode.
2278 static final int XMLSCHEMA_MODE = 1<<9;
2282 static final int SPECIAL_COMMA = 1<<10;
2285 private static final boolean isSet(int options, int flag) {
2286 return (options & flag) == flag;
2290 * Creates a new RegularExpression instance.
2292 * @param regex A regular expression
2293 * @exception org.apache.xerces.utils.regex.ParseException <VAR>regex</VAR> is not conforming to the syntax.
2295 public RegularExpression(String regex) throws ParseException {
2300 * Creates a new RegularExpression instance with options.
2302 * @param regex A regular expression
2303 * @param options A String consisted of "i" "m" "s" "u" "w" "," "X"
2304 * @exception org.apache.xerces.utils.regex.ParseException <VAR>regex</VAR> is not conforming to the syntax.
2306 public RegularExpression(String regex, String options) throws ParseException {
2307 this.setPattern(regex, options);
2311 * Creates a new RegularExpression instance with options.
2313 * @param regex A regular expression
2314 * @param options A String consisted of "i" "m" "s" "u" "w" "," "X"
2315 * @exception org.apache.xerces.utils.regex.ParseException <VAR>regex</VAR> is not conforming to the syntax.
2317 public RegularExpression(String regex, String options, Locale locale) throws ParseException {
2318 this.setPattern(regex, options, locale);
2321 RegularExpression(String regex, Token tok, int parens, boolean hasBackReferences, int options) {
2323 this.tokentree = tok;
2324 this.nofparen = parens;
2325 this.options = options;
2326 this.hasBackReferences = hasBackReferences;
2332 public void setPattern(String newPattern) throws ParseException {
2333 this.setPattern(newPattern, Locale.getDefault());
2336 public void setPattern(String newPattern, Locale locale) throws ParseException {
2337 this.setPattern(newPattern, this.options, locale);
2340 private void setPattern(String newPattern, int options, Locale locale) throws ParseException {
2341 this.regex = newPattern;
2342 this.options = options;
2343 RegexParser rp = RegularExpression.isSet(this.options, RegularExpression.XMLSCHEMA_MODE)
2344 ? new ParserForXMLSchema(locale) : new RegexParser(locale);
2345 this.tokentree = rp.parse(this.regex, this.options);
2346 this.nofparen = rp.parennumber;
2347 this.hasBackReferences = rp.hasBackReferences;
2349 this.operations = null;
2350 this.context = null;
2355 public void setPattern(String newPattern, String options) throws ParseException {
2356 this.setPattern(newPattern, options, Locale.getDefault());
2359 public void setPattern(String newPattern, String options, Locale locale) throws ParseException {
2360 this.setPattern(newPattern, REUtil.parseOptions(options), locale);
2366 public String getPattern() {
2371 * Represents this instence in String.
2374 public String toString() {
2375 return this.tokentree.toString(this.options);
2379 * Returns a option string.
2380 * The order of letters in it may be different from a string specified
2381 * in a constructor or <code>setPattern()</code>.
2383 * @see #RegularExpression(java.lang.String,java.lang.String)
2384 * @see #setPattern(java.lang.String,java.lang.String)
2386 public String getOptions() {
2387 return REUtil.createOptionString(this.options);
2391 * Return true if patterns are the same and the options are equivalent.
2394 public boolean equals(Object obj) {
2398 if (!(obj instanceof RegularExpression)) {
2401 RegularExpression r = (RegularExpression)obj;
2402 return this.regex.equals(r.regex) && this.options == r.options;
2405 boolean equals(String pattern, int options) {
2406 return this.regex.equals(pattern) && this.options == options;
2413 public int hashCode() {
2414 return (this.regex+"/"+this.getOptions()).hashCode();
2418 * Return the number of regular expression groups.
2419 * This method returns 1 when the regular expression has no capturing-parenthesis.
2422 public int getNumberOfGroups() {
2423 return this.nofparen;
2426 // ================================================================
2428 private static final int WT_IGNORE = 0;
2429 private static final int WT_LETTER = 1;
2430 private static final int WT_OTHER = 2;
2431 private static final int getWordType0(char ch, int opts) {
2432 if (!isSet(opts, UNICODE_WORD_BOUNDARY)) {
2433 if (isSet(opts, USE_UNICODE_CATEGORY)) {
2434 return (Token.getRange("IsWord", true).match(ch)) ? WT_LETTER : WT_OTHER;
2436 return isWordChar(ch) ? WT_LETTER : WT_OTHER;
2439 switch (Character.getType(ch)) {
2440 case Character.UPPERCASE_LETTER: // L
2441 case Character.LOWERCASE_LETTER: // L
2442 case Character.TITLECASE_LETTER: // L
2443 case Character.MODIFIER_LETTER: // L
2444 case Character.OTHER_LETTER: // L
2445 case Character.LETTER_NUMBER: // N
2446 case Character.DECIMAL_DIGIT_NUMBER: // N
2447 case Character.OTHER_NUMBER: // N
2448 case Character.COMBINING_SPACING_MARK: // Mc
2451 case Character.FORMAT: // Cf
2452 case Character.NON_SPACING_MARK: // Mn
2453 case Character.ENCLOSING_MARK: // Mc
2456 case Character.CONTROL: // Cc
2473 // ================================================================
2475 static final int LINE_FEED = 0x000A;
2476 static final int CARRIAGE_RETURN = 0x000D;
2477 static final int LINE_SEPARATOR = 0x2028;
2478 static final int PARAGRAPH_SEPARATOR = 0x2029;
2480 private static final boolean isEOLChar(int ch) {
2481 return ch == LINE_FEED || ch == CARRIAGE_RETURN || ch == LINE_SEPARATOR
2482 || ch == PARAGRAPH_SEPARATOR;
2485 private static final boolean isWordChar(int ch) { // Legacy word characters
2510 private static final boolean matchIgnoreCase(int chardata, int ch) {
2511 if (chardata == ch) {
2514 if (chardata > 0xffff || ch > 0xffff) {
2517 char uch1 = Character.toUpperCase((char)chardata);
2518 char uch2 = Character.toUpperCase((char)ch);
2522 return Character.toLowerCase(uch1) == Character.toLowerCase(uch2);