Matcher Pattern

6494 ワード

public final class Matcher
extends Object
implements MatchResult

An
エンジン
performs実行match
operations操作on a
character sequence by interpreting a Pattern . A matcher is created from a pattern mode by invoking び しthe pattern'smatcher method.Once created, a matcher can be used to perform three different kinds of match operations:
  • The matches method attemptsはmatch the entire のinput sequence against the patternを みる.
  • The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern.
  • The find method scansはthe input sequence looking for the next subsequence that matches the patternをスキャンする.

  • Each of these methods returns a boolean indicatingはsuccess or failureを す.More information about a successful match can be obtained by querying the state of the matcher.
    A matcher finds matches in a subsetサブセットof its input called the region .By default, the region contains all of the matcher's input. The region can be modified viaはthe region method and queried via the regionStart and regionEnd methods.The way that the region boundaries interact with some pattern constructs can be changed.See useAnchoringBounds and useTransparentBounds for more details.
    This class also defines methods for replacing matched subsequences with new strings whose contents can,if desired 、be computed (new strings contents)from the match result.The appendReplacement and appendTail methods can be used in tandem in order to collect the result into an existing string buffer,or the more convenient replaceAll method can be used to create a string in which every matching subsequence in the input sequence is replaced.
    The explicitはstate of a matcher includes the start and end indicesインデックスof the most recentの のsuccessful matchを する.It also includes the start and end indices of the input subsequence capturedキャプチャby each capturing group in the pattern as well as a total count of such subsequences.As a convenienceは で、methods are also provided for returning these captured subsequences in string form である.
    The explicit state of a matcher is initially undefined;attemptingはto query any part of it before a successful match will cause an IllegalStateException to be thrownを みた.The explicit state of a matcher is recomputed by every match operationを します.
    The implicit state of a matcher includes the input character sequence as well as theappend position,which is initially zero and is updated by the appendReplacement method.
    A matcher may be reset explicitly by invoking its reset() method or,if a new input sequence is desired 、its reset(CharSequence) method.Resetting a matcher discardsはits explicit state information and sets the append position to zeroを した.
    Instances of this class are not safe for use by multiple concurrent threads.
    public final class Pattern
    extends Object
    implements Serializable

    A
    compiledコンパイルrepresentation of a regular expression.
    A regular expression, specified as a string, must first be compiled into an instance of this class. The resultingはpattern can then be used to create a Matcher object that can match arbitrary character sequences against the regular expression. All of the state involved in performing a match resides in the matcher, so many matchers can share the same pattern.

    A typical invocation sequence is thus

     Pattern p = Pattern.compile("a*b");
     Matcher m = p.matcher("aaaaab");
     boolean b = m.matches();

    A matches method is defined by this class as a convenince for when a regular expression is used just once.This method compiles an expression and matches an input sequence against it in a single invocation. The statement

    boolean b = Pattern.matches("a*b", "aaaaab");
    is equivalent to the three statements above, though for repeated matches it is less efficient since it does not allow the compiled pattern to be reused. Instances of this class are immutable and are safe for use by multiple concurrent threads. Instances of theMatcher class are not safe for such use