src/java.base/share/classes/java/util/regex/Matcher.java

Print this page




 161     int first = -1, last = 0;
 162 
 163     /**
 164      * The end index of what matched in the last match operation.
 165      */
 166     int oldLast = -1;
 167 
 168     /**
 169      * The index of the last position appended in a substitution.
 170      */
 171     int lastAppendPosition = 0;
 172 
 173     /**
 174      * Storage used by nodes to tell what repetition they are on in
 175      * a pattern, and where groups begin. The nodes themselves are stateless,
 176      * so they rely on this field to hold state during a match.
 177      */
 178     int[] locals;
 179 
 180     /**








 181      * Boolean indicating whether or not more input could change
 182      * the results of the last match.
 183      *
 184      * If hitEnd is true, and a match was found, then more input
 185      * might cause a different match to be found.
 186      * If hitEnd is true and a match was not found, then more
 187      * input could cause a match to be found.
 188      * If hitEnd is false and a match was found, then more input
 189      * will not change the match.
 190      * If hitEnd is false and a match was not found, then more
 191      * input will not cause a match to be found.
 192      */
 193     boolean hitEnd;
 194 
 195     /**
 196      * Boolean indicating whether or not more input could change
 197      * a positive match into a negative one.
 198      *
 199      * If requireEnd is true, and a match was found, then more
 200      * input could cause the match to be lost.


 222      */
 223     int modCount;
 224 
 225     /**
 226      * No default constructor.
 227      */
 228     Matcher() {
 229     }
 230 
 231     /**
 232      * All matchers have the state used by Pattern during a match.
 233      */
 234     Matcher(Pattern parent, CharSequence text) {
 235         this.parentPattern = parent;
 236         this.text = text;
 237 
 238         // Allocate state storage
 239         int parentGroupCount = Math.max(parent.capturingGroupCount, 10);
 240         groups = new int[parentGroupCount * 2];
 241         locals = new int[parent.localCount];

 242 
 243         // Put fields into initial states
 244         reset();
 245     }
 246 
 247     /**
 248      * Returns the pattern that is interpreted by this matcher.
 249      *
 250      * @return  The pattern for which this matcher was created
 251      */
 252     public Pattern pattern() {
 253         return parentPattern;
 254     }
 255 
 256     /**
 257      * Returns the match state of this matcher as a {@link MatchResult}.
 258      * The result is unaffected by subsequent operations performed upon this
 259      * matcher.
 260      *
 261      * @return  a {@code MatchResult} with the state of this matcher


 358       * @param  newPattern
 359       *         The new pattern used by this matcher
 360       * @return  This matcher
 361       * @throws  IllegalArgumentException
 362       *          If newPattern is {@code null}
 363       * @since 1.5
 364       */
 365     public Matcher usePattern(Pattern newPattern) {
 366         if (newPattern == null)
 367             throw new IllegalArgumentException("Pattern cannot be null");
 368         parentPattern = newPattern;
 369 
 370         // Reallocate state storage
 371         int parentGroupCount = Math.max(newPattern.capturingGroupCount, 10);
 372         groups = new int[parentGroupCount * 2];
 373         locals = new int[newPattern.localCount];
 374         for (int i = 0; i < groups.length; i++)
 375             groups[i] = -1;
 376         for (int i = 0; i < locals.length; i++)
 377             locals[i] = -1;

 378         modCount++;
 379         return this;
 380     }
 381 
 382     /**
 383      * Resets this matcher.
 384      *
 385      * <p> Resetting a matcher discards all of its explicit state information
 386      * and sets its append position to zero. The matcher's region is set to the
 387      * default region, which is its entire character sequence. The anchoring
 388      * and transparency of this matcher's region boundaries are unaffected.
 389      *
 390      * @return  This matcher
 391      */
 392     public Matcher reset() {
 393         first = -1;
 394         last = 0;
 395         oldLast = -1;
 396         for(int i=0; i<groups.length; i++)
 397             groups[i] = -1;
 398         for(int i=0; i<locals.length; i++)
 399             locals[i] = -1;




 400         lastAppendPosition = 0;
 401         from = 0;
 402         to = getTextLength();
 403         modCount++;
 404         return this;
 405     }
 406 
 407     /**
 408      * Resets this matcher with a new input sequence.
 409      *
 410      * <p> Resetting a matcher discards all of its explicit state information
 411      * and sets its append position to zero.  The matcher's region is set to
 412      * the default region, which is its entire character sequence.  The
 413      * anchoring and transparency of this matcher's region boundaries are
 414      * unaffected.
 415      *
 416      * @param  input
 417      *         The new input character sequence
 418      *
 419      * @return  This matcher


1689      * Initiates a search to find a Pattern within the given bounds.
1690      * The groups are filled with default values and the match of the root
1691      * of the state machine is called. The state machine will hold the state
1692      * of the match as it proceeds in this matcher.
1693      *
1694      * Matcher.from is not set here, because it is the "hard" boundary
1695      * of the start of the search which anchors will set to. The from param
1696      * is the "soft" boundary of the start of the search, meaning that the
1697      * regex tries to match at that index but ^ won't match there. Subsequent
1698      * calls to the search methods start at a new "soft" boundary which is
1699      * the end of the previous match.
1700      */
1701     boolean search(int from) {
1702         this.hitEnd = false;
1703         this.requireEnd = false;
1704         from        = from < 0 ? 0 : from;
1705         this.first  = from;
1706         this.oldLast = oldLast < 0 ? from : oldLast;
1707         for (int i = 0; i < groups.length; i++)
1708             groups[i] = -1;




1709         acceptMode = NOANCHOR;
1710         boolean result = parentPattern.root.match(this, from, text);
1711         if (!result)
1712             this.first = -1;
1713         this.oldLast = this.last;
1714         this.modCount++;
1715         return result;
1716     }
1717 
1718     /**
1719      * Initiates a search for an anchored match to a Pattern within the given
1720      * bounds. The groups are filled with default values and the match of the
1721      * root of the state machine is called. The state machine will hold the
1722      * state of the match as it proceeds in this matcher.
1723      */
1724     boolean match(int from, int anchor) {
1725         this.hitEnd = false;
1726         this.requireEnd = false;
1727         from        = from < 0 ? 0 : from;
1728         this.first  = from;
1729         this.oldLast = oldLast < 0 ? from : oldLast;
1730         for (int i = 0; i < groups.length; i++)
1731             groups[i] = -1;




1732         acceptMode = anchor;
1733         boolean result = parentPattern.matchRoot.match(this, from, text);
1734         if (!result)
1735             this.first = -1;
1736         this.oldLast = this.last;
1737         this.modCount++;
1738         return result;
1739     }
1740 
1741     /**
1742      * Returns the end index of the text.
1743      *
1744      * @return the index after the last character in the text
1745      */
1746     int getTextLength() {
1747         return text.length();
1748     }
1749 
1750     /**
1751      * Generates a String from this Matcher's input in the specified range.




 161     int first = -1, last = 0;
 162 
 163     /**
 164      * The end index of what matched in the last match operation.
 165      */
 166     int oldLast = -1;
 167 
 168     /**
 169      * The index of the last position appended in a substitution.
 170      */
 171     int lastAppendPosition = 0;
 172 
 173     /**
 174      * Storage used by nodes to tell what repetition they are on in
 175      * a pattern, and where groups begin. The nodes themselves are stateless,
 176      * so they rely on this field to hold state during a match.
 177      */
 178     int[] locals;
 179 
 180     /**
 181      * Storage used by top greedy Loop node to store a specific hash set to
 182      * keep the beginning index of the failed repetition match. The nodes
 183      * themselves are stateless, so they rely on this field to hold state
 184      * during a match.
 185      */
 186     IntHashSet[] localsPos;
 187 
 188     /**
 189      * Boolean indicating whether or not more input could change
 190      * the results of the last match.
 191      *
 192      * If hitEnd is true, and a match was found, then more input
 193      * might cause a different match to be found.
 194      * If hitEnd is true and a match was not found, then more
 195      * input could cause a match to be found.
 196      * If hitEnd is false and a match was found, then more input
 197      * will not change the match.
 198      * If hitEnd is false and a match was not found, then more
 199      * input will not cause a match to be found.
 200      */
 201     boolean hitEnd;
 202 
 203     /**
 204      * Boolean indicating whether or not more input could change
 205      * a positive match into a negative one.
 206      *
 207      * If requireEnd is true, and a match was found, then more
 208      * input could cause the match to be lost.


 230      */
 231     int modCount;
 232 
 233     /**
 234      * No default constructor.
 235      */
 236     Matcher() {
 237     }
 238 
 239     /**
 240      * All matchers have the state used by Pattern during a match.
 241      */
 242     Matcher(Pattern parent, CharSequence text) {
 243         this.parentPattern = parent;
 244         this.text = text;
 245 
 246         // Allocate state storage
 247         int parentGroupCount = Math.max(parent.capturingGroupCount, 10);
 248         groups = new int[parentGroupCount * 2];
 249         locals = new int[parent.localCount];
 250         localsPos = new IntHashSet[parent.localTCNCount];
 251 
 252         // Put fields into initial states
 253         reset();
 254     }
 255 
 256     /**
 257      * Returns the pattern that is interpreted by this matcher.
 258      *
 259      * @return  The pattern for which this matcher was created
 260      */
 261     public Pattern pattern() {
 262         return parentPattern;
 263     }
 264 
 265     /**
 266      * Returns the match state of this matcher as a {@link MatchResult}.
 267      * The result is unaffected by subsequent operations performed upon this
 268      * matcher.
 269      *
 270      * @return  a {@code MatchResult} with the state of this matcher


 367       * @param  newPattern
 368       *         The new pattern used by this matcher
 369       * @return  This matcher
 370       * @throws  IllegalArgumentException
 371       *          If newPattern is {@code null}
 372       * @since 1.5
 373       */
 374     public Matcher usePattern(Pattern newPattern) {
 375         if (newPattern == null)
 376             throw new IllegalArgumentException("Pattern cannot be null");
 377         parentPattern = newPattern;
 378 
 379         // Reallocate state storage
 380         int parentGroupCount = Math.max(newPattern.capturingGroupCount, 10);
 381         groups = new int[parentGroupCount * 2];
 382         locals = new int[newPattern.localCount];
 383         for (int i = 0; i < groups.length; i++)
 384             groups[i] = -1;
 385         for (int i = 0; i < locals.length; i++)
 386             locals[i] = -1;
 387         localsPos = new IntHashSet[parentPattern.localTCNCount];
 388         modCount++;
 389         return this;
 390     }
 391 
 392     /**
 393      * Resets this matcher.
 394      *
 395      * <p> Resetting a matcher discards all of its explicit state information
 396      * and sets its append position to zero. The matcher's region is set to the
 397      * default region, which is its entire character sequence. The anchoring
 398      * and transparency of this matcher's region boundaries are unaffected.
 399      *
 400      * @return  This matcher
 401      */
 402     public Matcher reset() {
 403         first = -1;
 404         last = 0;
 405         oldLast = -1;
 406         for(int i=0; i<groups.length; i++)
 407             groups[i] = -1;
 408         for(int i=0; i<locals.length; i++)
 409             locals[i] = -1;
 410         for (int i = 0; i < localsPos.length; i++) {
 411             if (localsPos[i] != null)
 412                 localsPos[i].clear();
 413         }
 414         lastAppendPosition = 0;
 415         from = 0;
 416         to = getTextLength();
 417         modCount++;
 418         return this;
 419     }
 420 
 421     /**
 422      * Resets this matcher with a new input sequence.
 423      *
 424      * <p> Resetting a matcher discards all of its explicit state information
 425      * and sets its append position to zero.  The matcher's region is set to
 426      * the default region, which is its entire character sequence.  The
 427      * anchoring and transparency of this matcher's region boundaries are
 428      * unaffected.
 429      *
 430      * @param  input
 431      *         The new input character sequence
 432      *
 433      * @return  This matcher


1703      * Initiates a search to find a Pattern within the given bounds.
1704      * The groups are filled with default values and the match of the root
1705      * of the state machine is called. The state machine will hold the state
1706      * of the match as it proceeds in this matcher.
1707      *
1708      * Matcher.from is not set here, because it is the "hard" boundary
1709      * of the start of the search which anchors will set to. The from param
1710      * is the "soft" boundary of the start of the search, meaning that the
1711      * regex tries to match at that index but ^ won't match there. Subsequent
1712      * calls to the search methods start at a new "soft" boundary which is
1713      * the end of the previous match.
1714      */
1715     boolean search(int from) {
1716         this.hitEnd = false;
1717         this.requireEnd = false;
1718         from        = from < 0 ? 0 : from;
1719         this.first  = from;
1720         this.oldLast = oldLast < 0 ? from : oldLast;
1721         for (int i = 0; i < groups.length; i++)
1722             groups[i] = -1;
1723         for (int i = 0; i < localsPos.length; i++) {
1724             if (localsPos[i] != null)
1725                 localsPos[i].clear();
1726         }
1727         acceptMode = NOANCHOR;
1728         boolean result = parentPattern.root.match(this, from, text);
1729         if (!result)
1730             this.first = -1;
1731         this.oldLast = this.last;
1732         this.modCount++;
1733         return result;
1734     }
1735 
1736     /**
1737      * Initiates a search for an anchored match to a Pattern within the given
1738      * bounds. The groups are filled with default values and the match of the
1739      * root of the state machine is called. The state machine will hold the
1740      * state of the match as it proceeds in this matcher.
1741      */
1742     boolean match(int from, int anchor) {
1743         this.hitEnd = false;
1744         this.requireEnd = false;
1745         from        = from < 0 ? 0 : from;
1746         this.first  = from;
1747         this.oldLast = oldLast < 0 ? from : oldLast;
1748         for (int i = 0; i < groups.length; i++)
1749             groups[i] = -1;
1750         for (int i = 0; i < localsPos.length; i++) {
1751             if (localsPos[i] != null)
1752                 localsPos[i].clear();
1753         }
1754         acceptMode = anchor;
1755         boolean result = parentPattern.matchRoot.match(this, from, text);
1756         if (!result)
1757             this.first = -1;
1758         this.oldLast = this.last;
1759         this.modCount++;
1760         return result;
1761     }
1762 
1763     /**
1764      * Returns the end index of the text.
1765      *
1766      * @return the index after the last character in the text
1767      */
1768     int getTextLength() {
1769         return text.length();
1770     }
1771 
1772     /**
1773      * Generates a String from this Matcher's input in the specified range.