The Matcher class

An instance of the Matcher class performs various match operations on a character sequence by interpreting a compiled regular expression represented by an instance of Pattern. This is how we use this class to match a regex:

  • We create a matcher instance from a pattern by invoking the pattern's matcher method that requires the input sequence as argument
  • The instance of matcher is used to perform three types of match operations using these three methods, each returning a Boolean value (true indicates success):
    • matches
    • find
    • lookingAt

These methods perform the matching in the following manner:

  • The matches method attempts to match the complete input sequence using the matcher's pattern
  • The find method searches the input sequence for the next substring that matches the pattern
  • The lookingAt method attempts to match the input sequence using the matcher's pattern at the start position.

Let's list down all the important methods from the Matcher class here:

Method Signature

Description

boolean find()

Using the matcher's pattern attempts to find the next matching substring of the input text.

boolean find(int start)

This is the same as the previous, except that the search starts at the start position.

boolean matches()

Attempts to match the complete input text..

boolean lookingAt()

Attempts to match the input text, starting at the beginning of the region. It does not need to match the complete input text.

String group()

Returns the complete input text matched by the previous match.

String group(int group)

Returns the input text captured by the specified group number during the previous match operation.

String group(String groupName)

Returns the input text captured by the given named group during the previous match operation.

int groupCount()

Returns the number of capturing groups in this matcher's pattern.

int start()

Returns the start index of the previous match operation.

int start(int group)

Returns the start position of the text captured by the given group number during the previous match operation.

int start(int groupName)

Returns the start position of the text captured by the given named group during the previous match operation.

int end()

Returns the end position of the previous match operation.

int end(int group)

Returns the end position of the text captured by the given group number during the previous match operation.

int end(int groupName)

Returns the end position of the text captured by the given named group during the previous match operation.

Matcher appendReplacement(StringBuffer buffer, String replacement)

Appends the given replacement text to the string buffer after the last character of the previous match in the string buffer.

StringBuffer appendTail(StringBuffer buffer)

This method reads characters from the input text, starting at the append position, and appends them to the given string buffer. It is intended to be invoked after one or more invocations of the appendReplacement method in order to copy the remainder of the input text to the buffer.

Static String quoteReplacement(String s)

Returns a literal replacement String for the specified String. It makes backslashes and dollar signs to be treated literally.

String replaceAll(String replacement)

Using the current matcher's pattern, it replaces all the matched substrings of the input text with the given replacement string.

String replaceFirst(String replacement)

Using the current matcher's pattern, it replaces the first matched substring of the input text with the given replacement string.

Matcher reset()

Resets this matcher object and initializes all the internal states.

Matcher reset(CharSequence input)

Resets this matcher object with a new input text and initializes all the internal states.

MatchResult toMatchResult()

Returns the match result of the matcher that represents state of the match. This method is usually called after one of the find/matches/lookingAt method calls.

Matcher usePattern(Pattern newPattern)

Updates the pattern used by this matcher to find new matches.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.171.193