Java Matcher Class
In Java,Matcherthe class isjava.util.regexan important class in the package, used to perform various matching operations on strings.MatcherThe class itself cannot be directly instantiated; instead, it needs to be created throughPatternthe class'smatcher()method to create.
MatcherThe main functions of the class include:
- Perform regular expression matching operations
- Find subsequences in the string that match the pattern
- Replace matched text
Creating a Matcher Object
To useMatcherclass, you first need to create aPatternobject, then use it to createMatcherobject:
Example
// Create a Pattern object
Pattern pattern = Pattern.compile("a*b");
// Create a Matcher object
Matcher matcher = pattern.matcher("aaaaab");
Common Methods of the Matcher Class
Matching Operation Methods
matches() Method
matches()The method attempts to match the entire input sequence against the pattern:
Example
lookingAt() Method
lookingAt()The method attempts to match the pattern starting from the beginning of the input sequence:
Example
find() Method
find()The method finds the next matching subsequence in the input sequence:
Example
System.out.println("Found match: " + matcher.group());
}
Methods for Getting Match Information
group() Method
group()The method returns the input subsequence matched by the previous match operation:
Example
start() and end() Methods
start()andend()The methods return the start and end indices of the previous match:
Example
int end = matcher.end(); // Match end position
groupCount() Method
groupCount()Returns the number of capturing groups in this matcher's pattern:
Example
Replacement Operation Methods
replaceAll() Method
replaceAll()The method replaces every subsequence in the input sequence that matches the pattern:
Example
replaceFirst() Method
replaceFirst()The method replaces the first subsequence in the input sequence that matches the pattern:
Example
Example
Below is a complete example showing how to useMatcherclass:
Example
public class MatcherExample {
public static void main(String[] args) {
String text = "The quick brown fox jumps over the lazy dog";
Pattern pattern = Pattern.compile("\\b\\w{4}\\b"); // Match 4-letter words
Matcher matcher = pattern.matcher(text);
System.out.println("Original text: " + text);
System.out.println("Matching 4-letter words:");
while(matcher.find()) {
System.out.println("Found '" + matcher.group() +
"' at position " + matcher.start() + "-" + (matcher.end()-1));
}
// Replace all 4-letter words with "****"
String replaced = matcher.replaceAll("****");
System.out.println("\n"Replaced text: " + replaced);
}
}
Output Result
原始文本: The quick brown fox jumps over the lazy dog 匹配4字母单词: 找到 'quick' 在位置 4-8 找到 'brown' 在位置 10-14 找到 'jumps' 在位置 20-24 找到 'over' 在位置 26-29 找到 'lazy' 在位置 35-38 替换后的文本: The **** **** fox **** **** the **** dog
Summary
Java'sMatcherclass provides powerful regular expression matching functionality, through which you can:
- Check whether a string matches a specific pattern
- Find pattern matches in a string
- Extract matched substrings
- Replace matched text
MasteringMatcherthe class is very important for handling string and text pattern matching, and is a very practical tool in Java programming.
Matching Operation Methods
| Method | Description |
|---|---|
boolean matches() |
Attempts to match the entire region against the pattern |
boolean lookingAt() |
Attempts to match the pattern starting from the beginning of the region |
boolean find() |
Attempts to find the next matching subsequence |
boolean find(int start) |
Finds the next match starting from the specified position |
Methods for Getting Match Results
| Method | Description |
|---|---|
String group() |
Returns the subsequence of the previous match |
String group(int group) |
Returns the matched content of the specified capturing group |
String group(String name) |
Returns the matched content of the named capturing group (Java 7+) |
int groupCount() |
Returns the number of capturing groups in the pattern |
int start() |
Returns the start index of the previous match |
int start(int group) |
Returns the start index of the match for the specified group |
int end() |
Returns the end index of the previous match |
int end(int group) |
Returns the end index of the match for the specified group |
Replacement Operation Methods
| Method | Description |
|---|---|
Matcher appendReplacement(StringBuffer sb, String replacement) |
Performs a non-terminal replacement operation |
StringBuffer appendTail(StringBuffer sb) |
Performs a terminal replacement operation |
String replaceAll(String replacement) |
Replaces all matching subsequences |
String replaceFirst(String replacement) |
Replaces the first matching subsequence |
static String quoteReplacement(String s) |
Returns the literal of the replacement string |
Region Control Methods
| Method | Description |
|---|---|
Matcher region(int start, int end) |
Sets the search region of the matcher |
int regionStart() |
Returns the start index of the matcher's region |
int regionEnd() |
Returns the end index of the matcher's region |
boolean hasTransparentBounds() |
Checks whether transparent bounds are used |
Matcher useTransparentBounds(boolean b) |
Sets whether transparent bounds are used |
boolean hasAnchoringBounds() |
Checks whether anchoring bounds are used |
Matcher useAnchoringBounds(boolean b) |
Sets whether anchoring bounds are used |
Other Methods
| Method | Description |
|---|---|
Pattern pattern() |
Returns the pattern of this matcher |
Matcher reset() |
Resets the matcher |
Matcher reset(CharSequence input) |
Resets the matcher with new input |
boolean hitEnd() |
Indicates whether the matcher has reached the end of the input |
boolean requireEnd() |
Indicates whether more input is needed for a match |