Matcher Class

Package: java.util.regex

The Matcher class is used to match strings to a regex pattern represented by the Pattern class. For more information, see Pattern Class and Regular Expressions.

The Matcher class does not have a constructor. Instead, you create an instance of the Matcher class by first creating a Pattern object, and then calling the Pattern object’s matcher method.

Methods

Method

What It Does

int end ()

Returns the offset after the last character matched.

boolean find ()

Finds the next subsequence of the input sequence that matches the pattern.

boolean find (int start)

Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.

boolean hitEnd ()

Returns true if the end of input was hit by the search engine in the last match operation performed by this matcher.

boolean matches ()

Attempts to match the entire input sequence against the pattern.

Pattern pattern ()

Returns the pattern that is interpreted by this matcher.

String replaceAll (String replacement)

Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.

String replaceFirst (String replacement)

Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.

boolean requireEnd ()

Returns true if more input could change a positive match into a negative ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.