YouTip LogoYouTip

Java Regex Macher

## Java Matcher Class The `Matcher` class in Java is an important class in the `java.util.regex` package, which is used to perform various matching operations on strings. The `Matcher` class cannot be instantiated directly, but needs to be created through the `Pattern` class's `matcher()` method. The main functions of the `Matcher` class include: * Perform regular expression matching operations * Find subsequences in the string that match the pattern * Replace matched text * * * ## Creating a Matcher Object To use the `Matcher` class, you first need to create a `Pattern` object, and then create a `Matcher` object through it: ## Example import java.util.regex.*; // Create Pattern Object Pattern pattern = Pattern.compile("a*b"); // Create Matcher Object Matcher matcher = pattern.matcher("aaaaab"); ### Common Methods of Matcher Class * * * ## Matching Operation Methods ### matches() Method The `matches()` method attempts to match the entire input sequence with the pattern: ## Example boolean result = matcher.matches();// Returns true or false ### lookingAt() Method The `lookingAt()` method attempts to match the pattern from the beginning of the input sequence: ## Example boolean result = matcher.lookingAt();// Does not need to match the entire string ### find() Method The `find()` method finds the next matching subsequence in the input sequence: ## Example while(matcher.find()){ System.out.println("Found match: "+ matcher.group()); } * * * ## Methods for Getting Match Information ### group() Method The `group()` method returns the input subsequence matched by the previous matching operation: ## Example String matchedText = matcher.group(); ### start() and end() Methods The `start()` and `end()` methods return the start and end indices of the previous match: ## Example int start = matcher.start();// Match start position int end = matcher.end();// Match end position ### groupCount() Method `groupCount()` returns the number of capturing groups in this matcher's pattern: ## Example int count = matcher.groupCount(); * * * ## Replacement Operation Methods ### replaceAll() Method The `replaceAll()` method replaces every subsequence in the input sequence that matches the pattern: ## Example String result = matcher.replaceAll("replacement"); ### replaceFirst() Method The `replaceFirst()` method replaces the first subsequence in the input sequence that matches the pattern: ## Example String result = matcher.replaceFirst("replacement"); * * * ## Example The following is a complete example demonstrating how to use the `Matcher` class: ## Example import java.util.regex.*; 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 Original text: The quick brown fox jumps over the lazy dog Matching 4-letter words:Found 'quick' at position 4-8Found 'brown' at position 10-14Found 'jumps' at position 20-24Found 'over' at position 26-29Found 'lazy' at position 35-38Replaced text: The **** **** fox **** **** the **** dog * * * ## Summary Java's `Matcher` class provides powerful regular expression matching capabilities, through which you can: 1. Check if a string matches a specific pattern 2. Find pattern matches in a string 3. Extract matched substrings 4. Replace matched text Mastering the `Matcher` class is very important for handling string and text pattern matching, and it is a very useful tool in Java programming. ### Matching Operation Methods | Method | Description | | --- | --- | | `boolean matches()` | Attempts to match the entire region with the pattern | | `boolean lookingAt()` | Attempts to match the pattern 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 | ### Match Result Retrieval Methods | Method | Description | | --- | --- | | `String group()` | Returns the subsequence matched by the previous match | | `String group(int group)` | Returns the match content of the specified capturing group | | `String group(String name)` | Returns the match 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 non-terminal replacement operation | | `StringBuffer appendTail(StringBuffer sb)` | Performs 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 for 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 if transparent bounds are used | | `Matcher useTransparentBounds(boolean b)` | Sets whether to use transparent bounds | | `boolean hasAnchoringBounds()` | Checks if anchoring bounds are used | | `Matcher useAnchoringBounds(boolean b)` | Sets whether to use anchoring bounds | ### 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 input | | `boolean requireEnd
← Java Simpledateformat ClassJava Nio File Filenewbufferedw β†’