jdk/src/share/classes/java/util/regex/Matcher.java
author darcy
Mon, 08 Jul 2013 22:43:36 -0700
changeset 18787 8a57ff107238
parent 17434 4a04d7127e80
child 23734 439905b27f94
permissions -rw-r--r--
8020095: Fix doclint warnings in java.util.regex Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 17434
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4161
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4161
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4161
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4161
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4161
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.regex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
    28
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 17434
diff changeset
    31
 * An engine that performs match operations on a {@linkplain java.lang.CharSequence
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 17434
diff changeset
    32
 * character sequence} by interpreting a {@link Pattern}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p> A matcher is created from a pattern by invoking the pattern's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Pattern#matcher matcher} method.  Once created, a matcher can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * perform three different kinds of match operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *   <li><p> The {@link #matches matches} method attempts to match the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *   input sequence against the pattern.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *   <li><p> The {@link #lookingAt lookingAt} method attempts to match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   input sequence, starting at the beginning, against the pattern.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   <li><p> The {@link #find find} method scans the input sequence looking for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   the next subsequence that matches the pattern.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p> Each of these methods returns a boolean indicating success or failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * More information about a successful match can be obtained by querying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * state of the matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p> A matcher finds matches in a subset of its input called the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <i>region</i>. By default, the region contains all of the matcher's input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * The region can be modified via the{@link #region region} method and queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * via the {@link #regionStart regionStart} and {@link #regionEnd regionEnd}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * methods. The way that the region boundaries interact with some pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * constructs can be changed. See {@link #useAnchoringBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * useAnchoringBounds} and {@link #useTransparentBounds useTransparentBounds}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * for more details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p> This class also defines methods for replacing matched subsequences with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * new strings whose contents can, if desired, be computed from the match
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * result.  The {@link #appendReplacement appendReplacement} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * #appendTail appendTail} methods can be used in tandem in order to collect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * the result into an existing string buffer, or the more convenient {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * #replaceAll replaceAll} method can be used to create a string in which every
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * matching subsequence in the input sequence is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p> The explicit state of a matcher includes the start and end indices of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * the most recent successful match.  It also includes the start and end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * indices of the input subsequence captured by each <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * href="Pattern.html#cg">capturing group</a> in the pattern as well as a total
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * count of such subsequences.  As a convenience, methods are also provided for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * returning these captured subsequences in string form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p> The explicit state of a matcher is initially undefined; attempting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * query any part of it before a successful match will cause an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * IllegalStateException} to be thrown.  The explicit state of a matcher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * recomputed by every match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <p> The implicit state of a matcher includes the input character sequence as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * well as the <i>append position</i>, which is initially zero and is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * by the {@link #appendReplacement appendReplacement} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p> A matcher may be reset explicitly by invoking its {@link #reset()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * method or, if a new input sequence is desired, its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * #reset(java.lang.CharSequence) reset(CharSequence)} method.  Resetting a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * matcher discards its explicit state information and sets the append position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p> Instances of this class are not safe for use by multiple concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * threads. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @author      Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @author      JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @spec        JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
public final class Matcher implements MatchResult {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * The Pattern object that created this Matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    Pattern parentPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * The storage used by groups. They may contain invalid values if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * a group was skipped during the matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    int[] groups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * The range within the sequence that is to be matched. Anchors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * will match at these "hard" boundaries. Changing the region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * changes these values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    int from, to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Lookbehind uses this value to ensure that the subexpression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * match ends at the point where the lookbehind was encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    int lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * The original string being matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    CharSequence text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Matcher state used by the last node. NOANCHOR is used when a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * match does not have to consume all of the input. ENDANCHOR is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * the mode used for matching all the input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static final int ENDANCHOR = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static final int NOANCHOR = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    int acceptMode = NOANCHOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * The range of string that last matched the pattern. If the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * match failed then first is -1; last initially holds 0 then it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * holds the index of the end of the last match (which is where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * next search starts).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    int first = -1, last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * The end index of what matched in the last match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    int oldLast = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * The index of the last position appended in a substitution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    int lastAppendPosition = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Storage used by nodes to tell what repetition they are on in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * a pattern, and where groups begin. The nodes themselves are stateless,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * so they rely on this field to hold state during a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    int[] locals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Boolean indicating whether or not more input could change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * the results of the last match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * If hitEnd is true, and a match was found, then more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * might cause a different match to be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * If hitEnd is true and a match was not found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * input could cause a match to be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * If hitEnd is false and a match was found, then more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * will not change the match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * If hitEnd is false and a match was not found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * input will not cause a match to be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    boolean hitEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Boolean indicating whether or not more input could change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * a positive match into a negative one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * If requireEnd is true, and a match was found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * input could cause the match to be lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * If requireEnd is false and a match was found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * input might change the match but the match won't be lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * If a match was not found, then requireEnd has no meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    boolean requireEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * If transparentBounds is true then the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * matcher's region are transparent to lookahead, lookbehind,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * and boundary matching constructs that try to see beyond them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    boolean transparentBounds = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * If anchoringBounds is true then the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * matcher's region match anchors such as ^ and $.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    boolean anchoringBounds = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * No default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    Matcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * All matchers have the state used by Pattern during a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    Matcher(Pattern parent, CharSequence text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        this.parentPattern = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // Allocate state storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int parentGroupCount = Math.max(parent.capturingGroupCount, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        groups = new int[parentGroupCount * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        locals = new int[parent.localCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // Put fields into initial states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Returns the pattern that is interpreted by this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return  The pattern for which this matcher was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public Pattern pattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return parentPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Returns the match state of this matcher as a {@link MatchResult}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * The result is unaffected by subsequent operations performed upon this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return  a <code>MatchResult</code> with the state of this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public MatchResult toMatchResult() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        Matcher result = new Matcher(this.parentPattern, text.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        result.first = this.first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        result.last = this.last;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   253
        result.groups = this.groups.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
      * Changes the <tt>Pattern</tt> that this <tt>Matcher</tt> uses to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
      * find matches with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
      * <p> This method causes this matcher to lose information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
      * about the groups of the last match that occurred. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
      * matcher's position in the input is maintained and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
      * last append position is unaffected.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
      * @param  newPattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
      *         The new pattern used by this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
      * @return  This matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
      * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
      *          If newPattern is <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
      * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public Matcher usePattern(Pattern newPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (newPattern == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw new IllegalArgumentException("Pattern cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        parentPattern = newPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // Reallocate state storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        int parentGroupCount = Math.max(newPattern.capturingGroupCount, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        groups = new int[parentGroupCount * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        locals = new int[newPattern.localCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        for (int i = 0; i < groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        for (int i = 0; i < locals.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            locals[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Resets this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <p> Resetting a matcher discards all of its explicit state information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * and sets its append position to zero. The matcher's region is set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * default region, which is its entire character sequence. The anchoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * and transparency of this matcher's region boundaries are unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @return  This matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public Matcher reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        first = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        oldLast = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        for(int i=0; i<groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        for(int i=0; i<locals.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            locals[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        lastAppendPosition = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        from = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        to = getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Resets this matcher with a new input sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p> Resetting a matcher discards all of its explicit state information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * and sets its append position to zero.  The matcher's region is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * the default region, which is its entire character sequence.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * anchoring and transparency of this matcher's region boundaries are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @param  input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *         The new input character sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @return  This matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public Matcher reset(CharSequence input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        text = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 17434
diff changeset
   333
     * Returns the start index of the previous match.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return  The index of the first character matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public int start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            throw new IllegalStateException("No match available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Returns the start index of the subsequence captured by the given group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * during the previous match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * to right, starting at one.  Group zero denotes the entire pattern, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * the expression <i>m.</i><tt>start(0)</tt> is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <i>m.</i><tt>start()</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param  group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *         The index of a capturing group in this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @return  The index of the first character captured by the group,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *          or <tt>-1</tt> if the match was successful but the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *          itself did not match anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *          If there is no capturing group in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *          with the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public int start(int group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            throw new IllegalStateException("No match available");
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   374
        if (group < 0 || group > groupCount())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            throw new IndexOutOfBoundsException("No group " + group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return groups[group * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   380
     * Returns the start index of the subsequence captured by the given
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   381
     * <a href="Pattern.html#groupname">named-capturing group</a> during the
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   382
     * previous match operation.
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   383
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   384
     * @param  name
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   385
     *         The name of a named-capturing group in this matcher's pattern
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   386
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   387
     * @return  The index of the first character captured by the group,
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   388
     *          or {@code -1} if the match was successful but the group
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   389
     *          itself did not match anything
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   390
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   391
     * @throws  IllegalStateException
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   392
     *          If no match has yet been attempted,
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   393
     *          or if the previous match operation failed
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   394
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   395
     * @throws  IllegalArgumentException
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   396
     *          If there is no capturing group in the pattern
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   397
     *          with the given name
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   398
     * @since 1.8
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   399
     */
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   400
    public int start(String name) {
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   401
        return groups[getMatchedGroupIndex(name) * 2];
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   402
    }
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   403
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   404
    /**
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 17434
diff changeset
   405
     * Returns the offset after the last character matched.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @return  The offset after the last character matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public int end() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            throw new IllegalStateException("No match available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Returns the offset after the last character of the subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * captured by the given group during the previous match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * to right, starting at one.  Group zero denotes the entire pattern, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * the expression <i>m.</i><tt>end(0)</tt> is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <i>m.</i><tt>end()</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param  group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *         The index of a capturing group in this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @return  The offset after the last character captured by the group,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *          or <tt>-1</tt> if the match was successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *          but the group itself did not match anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *          If there is no capturing group in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *          with the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public int end(int group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            throw new IllegalStateException("No match available");
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   446
        if (group < 0 || group > groupCount())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            throw new IndexOutOfBoundsException("No group " + group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return groups[group * 2 + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   452
     * Returns the offset after the last character of the subsequence
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   453
     * captured by the given <a href="Pattern.html#groupname">named-capturing
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   454
     * group</a> during the previous match operation.
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   455
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   456
     * @param  name
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   457
     *         The name of a named-capturing group in this matcher's pattern
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   458
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   459
     * @return  The offset after the last character captured by the group,
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   460
     *          or {@code -1} if the match was successful
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   461
     *          but the group itself did not match anything
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   462
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   463
     * @throws  IllegalStateException
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   464
     *          If no match has yet been attempted,
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   465
     *          or if the previous match operation failed
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   466
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   467
     * @throws  IllegalArgumentException
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   468
     *          If there is no capturing group in the pattern
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   469
     *          with the given name
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   470
     * @since 1.8
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   471
     */
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   472
    public int end(String name) {
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   473
        return groups[getMatchedGroupIndex(name) * 2 + 1];
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   474
    }
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   475
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   476
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Returns the input subsequence matched by the previous match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * <p> For a matcher <i>m</i> with input sequence <i>s</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * the expressions <i>m.</i><tt>group()</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * <i>s.</i><tt>substring(</tt><i>m.</i><tt>start(),</tt>&nbsp;<i>m.</i><tt>end())</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * are equivalent.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <p> Note that some patterns, for example <tt>a*</tt>, match the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * string.  This method will return the empty string when the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * successfully matches the empty string in the input.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return The (possibly empty) subsequence matched by the previous match,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *         in string form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    public String group() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        return group(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * Returns the input subsequence captured by the given group during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * previous match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * <p> For a matcher <i>m</i>, input sequence <i>s</i>, and group index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * <i>g</i>, the expressions <i>m.</i><tt>group(</tt><i>g</i><tt>)</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * <i>s.</i><tt>substring(</tt><i>m.</i><tt>start(</tt><i>g</i><tt>),</tt>&nbsp;<i>m.</i><tt>end(</tt><i>g</i><tt>))</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * are equivalent.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * to right, starting at one.  Group zero denotes the entire pattern, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * the expression <tt>m.group(0)</tt> is equivalent to <tt>m.group()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * <p> If the match was successful but the group specified failed to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * any part of the input sequence, then <tt>null</tt> is returned. Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * that some groups, for example <tt>(a*)</tt>, match the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * This method will return the empty string when such a group successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * matches the empty string in the input.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @param  group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *         The index of a capturing group in this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @return  The (possibly empty) subsequence captured by the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *          during the previous match, or <tt>null</tt> if the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *          failed to match part of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *          If there is no capturing group in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *          with the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    public String group(int group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            throw new IllegalStateException("No match found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (group < 0 || group > groupCount())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            throw new IndexOutOfBoundsException("No group " + group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    /**
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   545
     * Returns the input subsequence captured by the given
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   546
     * <a href="Pattern.html#groupname">named-capturing group</a> during the previous
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   547
     * match operation.
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   548
     *
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   549
     * <p> If the match was successful but the group specified failed to match
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   550
     * any part of the input sequence, then <tt>null</tt> is returned. Note
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   551
     * that some groups, for example <tt>(a*)</tt>, match the empty string.
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   552
     * This method will return the empty string when such a group successfully
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   553
     * matches the empty string in the input.  </p>
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   554
     *
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   555
     * @param  name
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   556
     *         The name of a named-capturing group in this matcher's pattern
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   557
     *
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   558
     * @return  The (possibly empty) subsequence captured by the named group
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   559
     *          during the previous match, or <tt>null</tt> if the group
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   560
     *          failed to match part of the input
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   561
     *
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   562
     * @throws  IllegalStateException
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   563
     *          If no match has yet been attempted,
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   564
     *          or if the previous match operation failed
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   565
     *
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   566
     * @throws  IllegalArgumentException
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   567
     *          If there is no capturing group in the pattern
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   568
     *          with the given name
10345
b4aebbfc5b3a 7066490: @since 1.7 tag is missing for java.util.regex.Matcher.group(java.lang.String)
sherman
parents: 5506
diff changeset
   569
     * @since 1.7
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   570
     */
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   571
    public String group(String name) {
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   572
        int group = getMatchedGroupIndex(name);
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   573
        if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   574
            return null;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   575
        return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   576
    }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   577
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   578
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Returns the number of capturing groups in this matcher's pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * <p> Group zero denotes the entire pattern by convention. It is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * included in this count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <p> Any non-negative integer smaller than or equal to the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * returned by this method is guaranteed to be a valid group index for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * this matcher.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @return The number of capturing groups in this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    public int groupCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        return parentPattern.capturingGroupCount - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * Attempts to match the entire region against the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * <p> If the match succeeds then more information can be obtained via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @return  <tt>true</tt> if, and only if, the entire region sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *          matches this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public boolean matches() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        return match(from, ENDANCHOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Attempts to find the next subsequence of the input sequence that matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * <p> This method starts at the beginning of this matcher's region, or, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * a previous invocation of the method was successful and the matcher has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * not since been reset, at the first character not matched by the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * <p> If the match succeeds then more information can be obtained via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @return  <tt>true</tt> if, and only if, a subsequence of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *          sequence matches this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    public boolean find() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        int nextSearchIndex = last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (nextSearchIndex == first)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            nextSearchIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        // If next search starts before region, start it at region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (nextSearchIndex < from)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            nextSearchIndex = from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        // If next search starts beyond region then it fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (nextSearchIndex > to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            for (int i = 0; i < groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        return search(nextSearchIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * Resets this matcher and then attempts to find the next subsequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * the input sequence that matches the pattern, starting at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * <p> If the match succeeds then more information can be obtained via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods, and subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * invocations of the {@link #find()} method will start at the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * character not matched by this match.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 17434
diff changeset
   650
     * @param start the index to start searching for a match
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *          If start is less than zero or if start is greater than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *          length of the input sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @return  <tt>true</tt> if, and only if, a subsequence of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *          sequence starting at the given index matches this matcher's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *          pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    public boolean find(int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        int limit = getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if ((start < 0) || (start > limit))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            throw new IndexOutOfBoundsException("Illegal start index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return search(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Attempts to match the input sequence, starting at the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * region, against the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <p> Like the {@link #matches matches} method, this method always starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * at the beginning of the region; unlike that method, it does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * require that the entire region be matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * <p> If the match succeeds then more information can be obtained via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @return  <tt>true</tt> if, and only if, a prefix of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *          sequence matches this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    public boolean lookingAt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        return match(from, NOANCHOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Returns a literal replacement <code>String</code> for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * This method produces a <code>String</code> that will work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * as a literal replacement <code>s</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * <code>appendReplacement</code> method of the {@link Matcher} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * The <code>String</code> produced will match the sequence of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * in <code>s</code> treated as a literal sequence. Slashes ('\') and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * dollar signs ('$') will be given no special meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @param  s The string to be literalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @return  A literal string replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    public static String quoteReplacement(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if ((s.indexOf('\\') == -1) && (s.indexOf('$') == -1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        for (int i=0; i<s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            if (c == '\\' || c == '$') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                sb.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Implements a non-terminal append-and-replace step.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * <p> This method performs the following actions: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *   <li><p> It reads characters from the input sequence, starting at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *   append position, and appends them to the given string buffer.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *   stops after reading the last character preceding the previous match,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *   that is, the character at index {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *   #start()}&nbsp;<tt>-</tt>&nbsp;<tt>1</tt>.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *   <li><p> It appends the given replacement string to the string buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *   <li><p> It sets the append position of this matcher to the index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *   the last character matched, plus one, that is, to {@link #end()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * <p> The replacement string may contain references to subsequences
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * captured during the previous match: Each occurrence of
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
   738
     * <tt>${</tt><i>name</i><tt>}</tt> or <tt>$</tt><i>g</i>
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   739
     * will be replaced by the result of evaluating the corresponding
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 17434
diff changeset
   740
     * {@link #group(String) group(name)} or {@link #group(int) group(g)}
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 17434
diff changeset
   741
     * respectively. For  <tt>$</tt><i>g</i>,
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   742
     * the first number after the <tt>$</tt> is always treated as part of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * the group reference. Subsequent numbers are incorporated into g if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * they would form a legal group reference. Only the numerals '0'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * through '9' are considered as potential components of the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * reference. If the second group matched the string <tt>"foo"</tt>, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * example, then passing the replacement string <tt>"$2bar"</tt> would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * cause <tt>"foobar"</tt> to be appended to the string buffer. A dollar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * sign (<tt>$</tt>) may be included as a literal in the replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * string by preceding it with a backslash (<tt>\$</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * <p> Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * the replacement string may cause the results to be different than if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * were being treated as a literal replacement string. Dollar signs may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * treated as references to captured subsequences as described above, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * backslashes are used to escape literal characters in the replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * <p> This method is intended to be used in a loop together with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * {@link #appendTail appendTail} and {@link #find find} methods.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * following code, for example, writes <tt>one dog two dogs in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * yard</tt> to the standard-output stream: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * Pattern p = Pattern.compile("cat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * Matcher m = p.matcher("one cat two cats in the yard");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * while (m.find()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *     m.appendReplacement(sb, "dog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * m.appendTail(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * System.out.println(sb.toString());</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * @param  sb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     *         The target string buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @param  replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     *         The replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @return  This matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   786
     * @throws  IllegalArgumentException
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   787
     *          If the replacement string refers to a named-capturing
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   788
     *          group that does not exist in the pattern
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   789
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *          If the replacement string refers to a capturing group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *          that does not exist in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    public Matcher appendReplacement(StringBuffer sb, String replacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        // If no match, return error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            throw new IllegalStateException("No match available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        // Process substitution string to replace group references with groups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        int cursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        StringBuilder result = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        while (cursor < replacement.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            char nextChar = replacement.charAt(cursor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            if (nextChar == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                cursor++;
12432
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
   808
                if (cursor == replacement.length())
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
   809
                    throw new IllegalArgumentException(
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
   810
                        "character to be escaped is missing");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                nextChar = replacement.charAt(cursor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                result.append(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                cursor++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            } else if (nextChar == '$') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                // Skip past $
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                cursor++;
12432
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
   817
                // Throw IAE if this "$" is the last character in replacement
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
   818
                if (cursor == replacement.length())
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
   819
                   throw new IllegalArgumentException(
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
   820
                        "Illegal group reference: group index is missing");
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   821
                nextChar = replacement.charAt(cursor);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   822
                int refNum = -1;
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
   823
                if (nextChar == '{') {
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   824
                    cursor++;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   825
                    StringBuilder gsb = new StringBuilder();
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   826
                    while (cursor < replacement.length()) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   827
                        nextChar = replacement.charAt(cursor);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   828
                        if (ASCII.isLower(nextChar) ||
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   829
                            ASCII.isUpper(nextChar) ||
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   830
                            ASCII.isDigit(nextChar)) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   831
                            gsb.append(nextChar);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   832
                            cursor++;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   833
                        } else {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   834
                            break;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   835
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                    }
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   837
                    if (gsb.length() == 0)
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   838
                        throw new IllegalArgumentException(
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   839
                            "named capturing group has 0 length name");
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
   840
                    if (nextChar != '}')
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   841
                        throw new IllegalArgumentException(
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
   842
                            "named capturing group is missing trailing '}'");
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   843
                    String gname = gsb.toString();
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
   844
                    if (ASCII.isDigit(gname.charAt(0)))
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
   845
                        throw new IllegalArgumentException(
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
   846
                            "capturing group name {" + gname +
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
   847
                            "} starts with digit character");
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   848
                    if (!parentPattern.namedGroups().containsKey(gname))
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   849
                        throw new IllegalArgumentException(
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
   850
                            "No group with name {" + gname + "}");
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   851
                    refNum = parentPattern.namedGroups().get(gname);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   852
                    cursor++;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   853
                } else {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   854
                    // The first number is always a group
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   855
                    refNum = (int)nextChar - '0';
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   856
                    if ((refNum < 0)||(refNum > 9))
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   857
                        throw new IllegalArgumentException(
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   858
                            "Illegal group reference");
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   859
                    cursor++;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   860
                    // Capture the largest legal group string
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   861
                    boolean done = false;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   862
                    while (!done) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   863
                        if (cursor >= replacement.length()) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   864
                            break;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   865
                        }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   866
                        int nextDigit = replacement.charAt(cursor) - '0';
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   867
                        if ((nextDigit < 0)||(nextDigit > 9)) { // not a number
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   868
                            break;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   869
                        }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   870
                        int newRefNum = (refNum * 10) + nextDigit;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   871
                        if (groupCount() < newRefNum) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   872
                            done = true;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   873
                        } else {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   874
                            refNum = newRefNum;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   875
                            cursor++;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   876
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                // Append group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                if (start(refNum) != -1 && end(refNum) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                    result.append(text, start(refNum), end(refNum));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                result.append(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                cursor++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        // Append the intervening text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        sb.append(text, lastAppendPosition, first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        // Append the match substitution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        sb.append(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        lastAppendPosition = last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * Implements a terminal append-and-replace step.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * <p> This method reads characters from the input sequence, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * the append position, and appends them to the given string buffer.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * intended to be invoked after one or more invocations of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * #appendReplacement appendReplacement} method in order to copy the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * remainder of the input sequence.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * @param  sb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     *         The target string buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * @return  The target string buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    public StringBuffer appendTail(StringBuffer sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        sb.append(text, lastAppendPosition, getTextLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        return sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * Replaces every subsequence of the input sequence that matches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * pattern with the given replacement string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * <p> This method first resets this matcher.  It then scans the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * sequence looking for matches of the pattern.  Characters that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * part of any match are appended directly to the result string; each match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * is replaced in the result by the replacement string.  The replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * string may contain references to captured subsequences as in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * #appendReplacement appendReplacement} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * <p> Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * the replacement string may cause the results to be different than if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * were being treated as a literal replacement string. Dollar signs may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * treated as references to captured subsequences as described above, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * backslashes are used to escape literal characters in the replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * <p> Given the regular expression <tt>a*b</tt>, the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * <tt>"aabfooaabfooabfoob"</tt>, and the replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * <tt>"-"</tt>, an invocation of this method on a matcher for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * expression would yield the string <tt>"-foo-foo-foo-"</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * <p> Invoking this method changes this matcher's state.  If the matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * is to be used in further matching operations then it should first be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * reset.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * @param  replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     *         The replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @return  The string constructed by replacing each matching subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *          by the replacement string, substituting captured subsequences
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *          as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    public String replaceAll(String replacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        boolean result = find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        if (result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                appendReplacement(sb, replacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                result = find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            } while (result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            appendTail(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        return text.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * Replaces the first subsequence of the input sequence that matches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * pattern with the given replacement string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * <p> This method first resets this matcher.  It then scans the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * sequence looking for a match of the pattern.  Characters that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * part of the match are appended directly to the result string; the match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * is replaced in the result by the replacement string.  The replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * string may contain references to captured subsequences as in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * #appendReplacement appendReplacement} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * <p>Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * the replacement string may cause the results to be different than if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * were being treated as a literal replacement string. Dollar signs may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * treated as references to captured subsequences as described above, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * backslashes are used to escape literal characters in the replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * <p> Given the regular expression <tt>dog</tt>, the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * <tt>"zzzdogzzzdogzzz"</tt>, and the replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * <tt>"cat"</tt>, an invocation of this method on a matcher for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * expression would yield the string <tt>"zzzcatzzzdogzzz"</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * <p> Invoking this method changes this matcher's state.  If the matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * is to be used in further matching operations then it should first be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * reset.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @param  replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *         The replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @return  The string constructed by replacing the first matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *          subsequence by the replacement string, substituting captured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *          subsequences as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    public String replaceFirst(String replacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        if (replacement == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            throw new NullPointerException("replacement");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        if (!find())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            return text.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        appendReplacement(sb, replacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        appendTail(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * Sets the limits of this matcher's region. The region is the part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * input sequence that will be searched to find a match. Invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * method resets the matcher, and then sets the region to start at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * index specified by the <code>start</code> parameter and end at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * index specified by the <code>end</code> parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * <p>Depending on the transparency and anchoring being used (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * {@link #useTransparentBounds useTransparentBounds} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * {@link #useAnchoringBounds useAnchoringBounds}), certain constructs such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * as anchors may behave differently at or around the boundaries of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @param  start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *         The index to start searching at (inclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @param  end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *         The index to end searching at (exclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     *          If start or end is less than zero, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *          start is greater than the length of the input sequence, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     *          end is greater than the length of the input sequence, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *          start is greater than end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @return  this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    public Matcher region(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        if ((start < 0) || (start > getTextLength()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            throw new IndexOutOfBoundsException("start");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        if ((end < 0) || (end > getTextLength()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            throw new IndexOutOfBoundsException("end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        if (start > end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            throw new IndexOutOfBoundsException("start > end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        from = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        to = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * Reports the start index of this matcher's region. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * searches this matcher conducts are limited to finding matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * within {@link #regionStart regionStart} (inclusive) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * {@link #regionEnd regionEnd} (exclusive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * @return  The starting point of this matcher's region
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    public int regionStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        return from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * Reports the end index (exclusive) of this matcher's region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * The searches this matcher conducts are limited to finding matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * within {@link #regionStart regionStart} (inclusive) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * {@link #regionEnd regionEnd} (exclusive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @return  the ending point of this matcher's region
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    public int regionEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        return to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * Queries the transparency of region bounds for this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * <p> This method returns <tt>true</tt> if this matcher uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * <i>transparent</i> bounds, <tt>false</tt> if it uses <i>opaque</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * <p> See {@link #useTransparentBounds useTransparentBounds} for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * description of transparent and opaque bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * <p> By default, a matcher uses opaque region boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * @return <tt>true</tt> iff this matcher is using transparent bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     *         <tt>false</tt> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @see java.util.regex.Matcher#useTransparentBounds(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    public boolean hasTransparentBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        return transparentBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * Sets the transparency of region bounds for this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * <p> Invoking this method with an argument of <tt>true</tt> will set this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * matcher to use <i>transparent</i> bounds. If the boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * argument is <tt>false</tt>, then <i>opaque</i> bounds will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * <p> Using transparent bounds, the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * matcher's region are transparent to lookahead, lookbehind,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * and boundary matching constructs. Those constructs can see beyond the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * boundaries of the region to see if a match is appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * <p> Using opaque bounds, the boundaries of this matcher's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * region are opaque to lookahead, lookbehind, and boundary matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * constructs that may try to see beyond them. Those constructs cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * look past the boundaries so they will fail to match anything outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * of the region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * <p> By default, a matcher uses opaque bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * @param  b a boolean indicating whether to use opaque or transparent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     *         regions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * @return this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * @see java.util.regex.Matcher#hasTransparentBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    public Matcher useTransparentBounds(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        transparentBounds = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * Queries the anchoring of region bounds for this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * <p> This method returns <tt>true</tt> if this matcher uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * <i>anchoring</i> bounds, <tt>false</tt> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * <p> See {@link #useAnchoringBounds useAnchoringBounds} for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * description of anchoring bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * <p> By default, a matcher uses anchoring region boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * @return <tt>true</tt> iff this matcher is using anchoring bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *         <tt>false</tt> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @see java.util.regex.Matcher#useAnchoringBounds(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    public boolean hasAnchoringBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        return anchoringBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * Sets the anchoring of region bounds for this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * <p> Invoking this method with an argument of <tt>true</tt> will set this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * matcher to use <i>anchoring</i> bounds. If the boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * argument is <tt>false</tt>, then <i>non-anchoring</i> bounds will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * <p> Using anchoring bounds, the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * matcher's region match anchors such as ^ and $.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * <p> Without anchoring bounds, the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * matcher's region will not match anchors such as ^ and $.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * <p> By default, a matcher uses anchoring region boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * @param  b a boolean indicating whether or not to use anchoring bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * @return this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * @see java.util.regex.Matcher#hasAnchoringBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    public Matcher useAnchoringBounds(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        anchoringBounds = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * <p>Returns the string representation of this matcher. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * string representation of a <code>Matcher</code> contains information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * that may be useful for debugging. The exact format is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * @return  The string representation of this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        sb.append("java.util.regex.Matcher");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        sb.append("[pattern=" + pattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        sb.append(" region=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        sb.append(regionStart() + "," + regionEnd());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        sb.append(" lastmatch=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        if ((first >= 0) && (group() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            sb.append(group());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        sb.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * <p>Returns true if the end of input was hit by the search engine in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * the last match operation performed by this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * <p>When this method returns true, then it is possible that more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * would have changed the result of the last search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * @return  true iff the end of input was hit in the last match; false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     *          otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    public boolean hitEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        return hitEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * <p>Returns true if more input could change a positive match into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * negative one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * <p>If this method returns true, and a match was found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * input could cause the match to be lost. If this method returns false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * and a match was found, then more input might change the match but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * match won't be lost. If a match was not found, then requireEnd has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * @return  true iff more input could change a positive match into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *          negative one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    public boolean requireEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        return requireEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * Initiates a search to find a Pattern within the given bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * The groups are filled with default values and the match of the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * of the state machine is called. The state machine will hold the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * of the match as it proceeds in this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * Matcher.from is not set here, because it is the "hard" boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * of the start of the search which anchors will set to. The from param
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * is the "soft" boundary of the start of the search, meaning that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * regex tries to match at that index but ^ won't match there. Subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * calls to the search methods start at a new "soft" boundary which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * the end of the previous match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    boolean search(int from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        this.hitEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        this.requireEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        from        = from < 0 ? 0 : from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        this.first  = from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        this.oldLast = oldLast < 0 ? from : oldLast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        for (int i = 0; i < groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        acceptMode = NOANCHOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        boolean result = parentPattern.root.match(this, from, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        if (!result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            this.first = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        this.oldLast = this.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * Initiates a search for an anchored match to a Pattern within the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * bounds. The groups are filled with default values and the match of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * root of the state machine is called. The state machine will hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * state of the match as it proceeds in this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    boolean match(int from, int anchor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        this.hitEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        this.requireEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        from        = from < 0 ? 0 : from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        this.first  = from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        this.oldLast = oldLast < 0 ? from : oldLast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        for (int i = 0; i < groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        acceptMode = anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        boolean result = parentPattern.matchRoot.match(this, from, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        if (!result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            this.first = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        this.oldLast = this.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * Returns the end index of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * @return the index after the last character in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    int getTextLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        return text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * Generates a String from this Matcher's input in the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * @param  beginIndex   the beginning index, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * @param  endIndex     the ending index, exclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * @return A String generated from this Matcher's input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    CharSequence getSubSequence(int beginIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        return text.subSequence(beginIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * Returns this Matcher's input character at index i.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * @return A char from the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    char charAt(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        return text.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1306
    /**
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1307
     * Returns the group index of the matched capturing group.
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1308
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1309
     * @return the index of the named-capturing group
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1310
     */
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1311
    int getMatchedGroupIndex(String name) {
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1312
        Objects.requireNonNull(name, "Group name");
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1313
        if (first < 0)
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1314
            throw new IllegalStateException("No match found");
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1315
        if (!parentPattern.namedGroups().containsKey(name))
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1316
            throw new IllegalArgumentException("No group with name <" + name + ">");
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1317
        return parentPattern.namedGroups().get(name);
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1318
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
}