Save This Page
Home » apache-harmony-6.0-src-r917296-snapshot » java » util » regex » [javadoc | source]
java.util.regex
public final class: Matcher [javadoc | source]
java.lang.Object
   java.util.regex.Matcher

All Implemented Interfaces:
    MatchResult

Provides a means of matching regular expressions against a given input, finding occurrences of regular expressions in a given input, or replacing parts of a given input. A {@code Matcher} instance has an associated Pattern instance and an input text. A typical use case is to iteratively find all occurrences of the {@code Pattern}, until the end of the input is reached, as the following example illustrates:

Pattern p = Pattern.compile("[A-Za-z]+");

Matcher m = p.matcher("Hello, Android!");
while (m.find()) {
    System.out.println(m.group()); // prints "Hello" and "Android"
}

The {@code Matcher} has a state that results from the previous operations. For example, it knows whether the most recent attempt to find the {@code Pattern} was successful and at which position the next attempt would resume the search. Depending on the application's needs, it may become necessary to explicitly #reset() this state from time to time.
Field Summary
static  int MODE_FIND     
static  int MODE_MATCH     
Constructor:
 Matcher(Pattern pat,
    CharSequence cs) 
Method from java.util.regex.Matcher Summary:
appendReplacement,   appendTail,   end,   end,   find,   find,   group,   group,   groupCount,   hasAnchoringBounds,   hasTransparentBounds,   hitEnd,   lookingAt,   matches,   pattern,   quoteReplacement,   region,   regionEnd,   regionStart,   replaceAll,   replaceFirst,   requireEnd,   reset,   reset,   start,   start,   toMatchResult,   useAnchoringBounds,   usePattern,   useTransparentBounds
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.util.regex.Matcher Detail:
 public Matcher appendReplacement(StringBuffer buffer,
    String replacement) 
    Appends a literal part of the input plus a replacement for the current match to a given StringBuffer . The literal part is exactly the part of the input between the previous match and the current match. The method can be used in conjunction with #find() and #appendTail(StringBuffer) to walk through the input and replace all occurrences of the {@code Pattern} with something else.
 public StringBuffer appendTail(StringBuffer buffer) 
 public int end() 
    Returns the index of the first character following the text that matched the whole regular expression.
 public int end(int group) 
    Returns the index of the first character following the text that matched a given group.
 public boolean find() 
    Returns the next occurrence of the Pattern in the input. If a previous match was successful, the method continues the search from the first character following that match in the input. Otherwise it searches either from the region start (if one has been set), or from position 0.
 public boolean find(int start) 
    Returns the next occurrence of the Pattern in the input. The method starts the search from the given character in the input.
 public String group() 
    Returns the text that matched the whole regular expression.
 public String group(int group) 
    Returns the text that matched a given group of the regular expression.
 public int groupCount() 
    Returns the number of groups in the results, which is always equal to the number of groups in the original regular expression.
 public boolean hasAnchoringBounds() 
    Indicates whether this matcher has anchoring bounds enabled. When anchoring bounds are enabled, the start and end of the input match the '^' and '$' meta-characters, otherwise not. Anchoring bounds are enabled by default.
 public boolean hasTransparentBounds() 
    Indicates whether this matcher has transparent bounds enabled. When transparent bounds are enabled, the parts of the input outside the region are subject to lookahead and lookbehind, otherwise they are not. Transparent bounds are disabled by default.
 public boolean hitEnd() 
    Indicates whether the last match hit the end of the input.
 public boolean lookingAt() 
    Tries to match the Pattern , starting from the beginning of the region (or the beginning of the input, if no region has been set). Doesn't require the {@code Pattern} to match against the whole region.
 public boolean matches() 
    Tries to match the Pattern against the entire region (or the entire input, if no region has been set).
 public Pattern pattern() 
    Returns the Pattern instance used inside this matcher.
 public static String quoteReplacement(String s) 
    Returns a replacement string for the given one that has all backslashes and dollar signs escaped.
 public Matcher region(int start,
    int end) 
    Resets this matcher and sets a region. Only characters inside the region are considered for a match.
 public int regionEnd() 
    Returns this matcher's region end, that is, the first character that is not considered for a match.
 public int regionStart() 
    Returns this matcher's region start, that is, the first character that is considered for a match.
 public String replaceAll(String replacement) 
    Replaces all occurrences of this matcher's pattern in the input with a given string.
 public String replaceFirst(String replacement) 
    Replaces the first occurrence of this matcher's pattern in the input with a given string.
 public boolean requireEnd() 
    Indicates whether more input might change a successful match into an unsuccessful one.
 public Matcher reset() 
    Resets the {@code Matcher}. This results in the region being set to the whole input. Results of a previous find get lost. The next attempt to find an occurrence of the Pattern in the string will start at the beginning of the input.
 public Matcher reset(CharSequence input) 
    Provides a new input and resets the {@code Matcher}. This results in the region being set to the whole input. Results of a previous find get lost. The next attempt to find an occurrence of the Pattern in the string will start at the beginning of the input.
 public int start() 
    Returns the index of the first character of the text that matched the whole regular expression.
 public int start(int group) 
    Returns the index of the first character of the text that matched a given group.
 public MatchResult toMatchResult() 
    Converts the current match into a separate MatchResult instance that is independent from this matcher. The new object is unaffected when the state of this matcher changes.
 public Matcher useAnchoringBounds(boolean value) 
    Determines whether this matcher has anchoring bounds enabled or not. When anchoring bounds are enabled, the start and end of the input match the '^' and '$' meta-characters, otherwise not. Anchoring bounds are enabled by default.
 public Matcher usePattern(Pattern pattern) 
    Sets a new pattern for the {@code Matcher}. Results of a previous find get lost. The next attempt to find an occurrence of the Pattern in the string will start at the beginning of the input.
 public Matcher useTransparentBounds(boolean value) 
    Determines whether this matcher has transparent bounds enabled or not. When transparent bounds are enabled, the parts of the input outside the region are subject to lookahead and lookbehind, otherwise they are not. Transparent bounds are disabled by default.