Team LiB
Previous Section Next Section

Method Class Methods

public Pattern pattern()

The pattern method returns the Pattern that created this particular Matcher object. The Pattern returned doesn't contain any of the flags that are explicitly set by using the Pattern constants when the pattern is compiled, such as Pattern.MULTILINE.

public Matcher reset()

The reset() method clears all state information from the Matcher object it is called on. The Matcher is, in effect, reverted to the state it originally had when you first received a reference to it.

public Matcher reset(CharSequence input)

The reset(CharSequnce input) method clears the state of the Matcher object it's called on and replaces the candidate String with the new input. This has the same effect as creating a new Matcher object, except that it doesn't have the associated overhead. This recycling can be a useful optimization, and it's one that I often use.

public int start()

This method returns the index of the first character of the candidate String matched. If there are no matches, or if no matches have been attempted, an IllegalStateException is thrown.

public int start(int group)

This method allows you to specify which subgroup within a matching you're interested in and returns the index of the first character in which that subgroup starts. If there are no matches, or if no matches have been attempted, an IllegalStateException is thrown. If you refer to a group number that doesn't exist, an IndexOutOfBoundsException is thrown.

public int end()

The end method returns the ending index plus 1 of the last successful match the Matcher object had. If no matches exist, or if no matches have been attempted, this method throws an IllegalStateException.

public int end(int group)

Like the start(int) method, this method allows you to specify which subgroup within a matching you're interested in, except that it returns the last index of matching character sequence plus 1. If no matches exist, or if no matches have been attempted, this method throws an IllegalStateException. If you refer to a group number that doesn't exist, an IndexOutOfBoundsException is thrown.

public String group()

The group method can be a powerful and convenient tool in the war against jumbled code. It simply returns the substring of the candidate String that matches the original regex pattern. The group() method throws an IllegalStateException if the find method call wasn't successful. Similarly, it throws an IllegalStateException if find has never been called at all.

public String group(int group)

This method is a more powerful counterpart to the group() method. It allows you to extract parts of a candidate string that match a subgroup within your pattern.

The group(int) method throws an IllegalStateException if the find method call wasn't successful. Similarly, it throws an IllegalStateException is find has never been called at all. If called for a group number that doesn't exist, it throws an IndexOutOfBoundsException.

public int groupCount()

This method simply returns the number of groups that the Pattern defined. There's a very important, and somewhat counterintuitive, subtlety to notice about this method: It returns the number of possible groups based on the original Pattern, without even considering the candidate String. Thus, it's not really information about the Matcher object; instead, it's about the Pattern that helped spawn it. This can be tricky, because the fact that this method lives on the Matcher object could be interpreted as meaning that it's providing feedback about the state of the Matcher. It isn't. It's telling you how many matches are theoretically possible for the given Pattern.

public boolean matches()

This method is designed to help you match a candidate String against the matcher's Pattern. It returns true if, and only if, the candidate String under consideration matches the pattern exactly.

public boolean find()

The find() method parses just enough of the candidate String to find a match. If such a substring is found, then true is returned and find stops parsing the candidate. If no part of the candidate String matches the pattern, then find returns false.

public boolean find(int start)

find(int start) works exactly like its overloaded counterpart, with the exception of where it starts searching. The int parameter start simply tells the Matcher at which character to start its search.

Thus, for the candidate String I love Java. Java is my favorite language. Java Java Java. and the Pattern Java, if you only want to start searching at character index 11, you use the command find(11).

public Matcher appendReplacement(StringBuffer sb, String replacement)

The appendReplace method allows you to modify the contents of a StringBuffer based on a regular expression. It even allows you to use back references by using the $n notation, in which n refers to some captured subgroup. For example, for the StringBuffer Waldo Smith, the Pattern (\w+) (\w+), and the replacement $2, $1, the contents of the StringBuffer will be modified to Smith, Waldo.

The appendReplacement method will throw an IllegalStateException if a find() hasn't been called, or if find would have returned false if called. It will throw an IndexOutOfBoundsException if the capturing group referred to by $1, $2, etc., doesn't exist in the part of the pattern currently being scrutinized by the Matcher.

public StringBuffer appendTail(StringBuffer sb)

The appendTail method is a supplement to the appendReplacement method. It simply appends every remaining subsequence from the original candidate String to the StringBuffer, if reading from the append position, which I explained in the appendReplacement section, to the end of the candidate string.

public String replaceAll(String replacement)

The replaceAll method returns a String that replaces every occurrence of the description with the replacement. Using this method will change the state of your Matcher object. Specifically, the reset() method will be called. Therefore, remember that all start, end, group, and find calls will have to be reexecuted.

Like the appendReplacement method, the replaceAll method can contain references to substring by using the $ symbol. For details, please see the appendReplacement documentation presented earlier.

public String replaceFirst(String replacement)

The replaceFirst method is a more limited version of the replaceAll method. This method returns a String that replaces the first occurrence of the description with the replacement. Using this method will change the state of your Matcher object. Specifically, the reset() method will be called. Therefore, remember that all start, end, group, and find calls will have to be reexecuted after replaceFirst is called.

Like the appendReplacement method, the replaceFirst method can contain references to substring by using the $ symbol. For details, please see the appendReplacement documentation presented earlier.


Team LiB
Previous Section Next Section