java.util.regex
Class Matcher

java.lang.Object
  extended by java.util.regex.Matcher
All Implemented Interfaces:
MatchResult

public final class Matcher
extends Object
implements MatchResult

Instance of a regular expression applied to a char sequence.

Since:
1.4

Method Summary
 Matcher appendReplacement(StringBuffer sb, String replacement)
           
 StringBuffer appendTail(StringBuffer sb)
           
 int end()
          Returns the index just after the last matched character.
 int end(int group)
          Returns the index just after the last matched character of the given sub-match group.
 boolean find()
           
 boolean find(int start)
           
 String group()
          Returns the substring of the input which was matched.
 String group(int group)
          Returns the substring of the input which was matched by the given sub-match group.
 int groupCount()
          Returns the number of sub-match groups in the matching pattern.
 boolean hitEnd()
           
 boolean lookingAt()
           
 boolean matches()
          Attempts to match the entire input sequence against the pattern.
 Pattern pattern()
          Returns the Pattern that is interpreted by this Matcher
 String replaceAll(String replacement)
           
 String replaceFirst(String replacement)
           
 Matcher reset()
           
 Matcher reset(CharSequence input)
           
 int start()
          Returns the index of the first character of the match.
 int start(int group)
          Returns the index of the first character of the given sub-match group.
 String toString()
          Convert this Object to a human-readable String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

appendReplacement

public Matcher appendReplacement(StringBuffer sb,
                                 String replacement)
                          throws IllegalStateException
Parameters:
sb - The target string buffer
replacement - The replacement string
Throws:
IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
IndexOutOfBoundsException - If the replacement string refers to a capturing group that does not exist in the pattern

appendTail

public StringBuffer appendTail(StringBuffer sb)
Parameters:
sb - The target string buffer

end

public int end()
        throws IllegalStateException
Description copied from interface: MatchResult
Returns the index just after the last matched character.

Specified by:
end in interface MatchResult
Throws:
IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

end

public int end(int group)
        throws IllegalStateException
Description copied from interface: MatchResult
Returns the index just after the last matched character of the given sub-match group.

Specified by:
end in interface MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
Throws:
IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
IndexOutOfBoundsException - If the replacement string refers to a capturing group that does not exist in the pattern

find

public boolean find()

find

public boolean find(int start)
Parameters:
start - The index to start the new pattern matching
Throws:
IndexOutOfBoundsException - If the replacement string refers to a capturing group that does not exist in the pattern

group

public String group()
Description copied from interface: MatchResult
Returns the substring of the input which was matched.

Specified by:
group in interface MatchResult
Throws:
IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

group

public String group(int group)
             throws IllegalStateException
Description copied from interface: MatchResult
Returns the substring of the input which was matched by the given sub-match group.

Specified by:
group in interface MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
Throws:
IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
IndexOutOfBoundsException - If the replacement string refers to a capturing group that does not exist in the pattern

replaceFirst

public String replaceFirst(String replacement)
Parameters:
replacement - The replacement string

replaceAll

public String replaceAll(String replacement)
Parameters:
replacement - The replacement string

groupCount

public int groupCount()
Description copied from interface: MatchResult
Returns the number of sub-match groups in the matching pattern.

Specified by:
groupCount in interface MatchResult

lookingAt

public boolean lookingAt()

matches

public boolean matches()
Attempts to match the entire input sequence against the pattern. If the match succeeds then more information can be obtained via the start, end, and group methods.

See Also:
start(), end(), group()

pattern

public Pattern pattern()
Returns the Pattern that is interpreted by this Matcher


reset

public Matcher reset()

reset

public Matcher reset(CharSequence input)
Parameters:
input - The new input character sequence

start

public int start()
          throws IllegalStateException
Description copied from interface: MatchResult
Returns the index of the first character of the match.

Specified by:
start in interface MatchResult
Throws:
IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

start

public int start(int group)
          throws IllegalStateException
Description copied from interface: MatchResult
Returns the index of the first character of the given sub-match group.

Specified by:
start in interface MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
Throws:
IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
IndexOutOfBoundsException - If the replacement string refers to a capturing group that does not exist in the pattern

hitEnd

public boolean hitEnd()
Returns:
True if and only if the matcher hit the end of input.

toString

public String toString()
Description copied from class: Object
Convert this Object to a human-readable String. There are no limits placed on how long this String should be or what it should contain. We suggest you make it as intuitive as possible to be able to place it into System.out.println() and such.

It is typical, but not required, to ensure that this method never completes abruptly with a RuntimeException.

This method will be called when performing string concatenation with this object. If the result is null, string concatenation will instead use "null".

The default implementation returns getClass().getName() + "@" + Integer.toHexString(hashCode()).

Overrides:
toString in class Object
Returns:
A string expression of this matcher.
See Also:
Object.getClass(), Object.hashCode(), Class.getName(), Integer.toHexString(int)