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