Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Matcher Class

Java Matcher Class

Java Date Class Matcher The class is java.util.regex is an important class in the package; it is used to perform various matching operations on strings.Matcher The class itself cannot be instantiated directly; instead, it needs to be created through the Pattern The class's matcher() method to create.

Matcher The main functions of the class include:


Creating a Matcher object

To use Matcher class, you first need to create a Pattern object, and then use it to create a Matcher object:

Example

import

java.util.regex.*

;

// 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

boolean

result

=

matcher.

matches

(

)

;

// Returns true or false

lookingAt() method

lookingAt() The method attempts to match the pattern starting from the beginning of the input sequence:

Example

boolean

result

=

matcher.

lookingAt

(

)

;

// Does not need to match the entire string

find() method

find() The method finds the next matching subsequence in the input sequence:

Example

while

(

matcher.

find

(

)

)

{

   

System

.

out

.

println

(

"Match found: "

matcher.

group

(

)

)

;

}


Methods for obtaining match information

group() method

group() The method returns the input subsequence matched by the previous match operation:

Example

String

matchedText

=

matcher.

group

(

)

;

The start() and end() methods

start() and end() The method returns the start and end indices of the previous match:

Example

int

start

=

matcher.

start

(

)

;

// Start position of the match

int

end

=

matcher.

end

(

)

;

   

// End position of the match

groupCount() method

groupCount() Returns the number of capturing groups in this matcher's pattern:

Example

int

count

=

matcher.

groupCount

(

)

;


Replacement operation methods

replaceAll() method

replaceAll() The method replaces each subsequence in the input sequence that matches the pattern:

Example

String

result

=

matcher.

replaceAll

(

"replacement"

)

;

replaceFirst() method

replaceFirst() The 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

(

"4-letter words matched:"

)

;

       

       

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

(

"\nReplaced text: "

replaced

)

;

   

}

}

Output result

原始文本: The quick brown fox jumps over the lazy dog

# Match 4-letter words:

找到 'quick' 在位置 4-8

找到 'brown' 在位置 10-14

找到 'jumps' 在位置 20-24

找到 'over' 在位置 26-29

找到 'lazy' 在位置 35-38



替换后的文本: The **** **** fox **** **** the **** dog


Summary

Java's Matcher class provides powerful regex matching capabilities, through which you can:

  1. Check whether 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 strings 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

Match result retrieval methods

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 matcher's search region
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 boundaries are used
Matcher useTransparentBounds(boolean b) Sets whether transparent boundaries are used
boolean hasAnchoringBounds() Checks whether anchored boundaries are used
Matcher useAnchoringBounds(boolean b) Sets whether anchored boundaries 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 to match