Java : Matcher (regex) with Examples

Matcher (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Matcher methods.


Summary

An engine that performs match operations on a character sequence by interpreting a Pattern.

Class diagram

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaabc");

final var stream = matcher.results();

stream.forEach(matchResult -> {

    System.out.println("------");
    System.out.println("start : " + matchResult.start());
    System.out.println("end : " + matchResult.end());
    System.out.println("group : " + matchResult.group());
});

// Result
// ↓
//------
//start : 0
//end : 3
//group : abc
//------
//start : 8
//end : 13
//group : aaabc

Methods

Matcher appendReplacement (StringBuffer sb, String replacement)

Implements a non-terminal append-and-replace step.

final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");

final var sb = new StringBuffer();

while (matcher.find()) {
    matcher.appendReplacement(sb, "dog");
}

System.out.println(sb); // "one dog two dog"

matcher.appendTail(sb);

System.out.println(sb); // "one dog two dogs in the yard"

Matcher appendReplacement (StringBuilder sb, String replacement)

Implements a non-terminal append-and-replace step.

final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");

final var sb = new StringBuilder();

while (matcher.find()) {
    matcher.appendReplacement(sb, "dog");
}

System.out.println(sb); // "one dog two dog"

matcher.appendTail(sb);

System.out.println(sb); // "one dog two dogs in the yard"

StringBuffer appendTail (StringBuffer sb)

Implements a terminal append-and-replace step.

Please see appendReplacement(StringBuffer sb, String replacement).

StringBuilder appendTail (StringBuilder sb)

Implements a terminal append-and-replace step.

Please see appendReplacement(StringBuilder sb, String replacement).

int end ()

Returns the offset after the last character matched.

final var pattern = Pattern.compile("a+bc");
final var input = "aaaaabc";

final var matcher = pattern.matcher(input);
System.out.println(matcher.matches()); // true

final var start = matcher.start();
System.out.println(start); // 0

final var end = matcher.end();
System.out.println(end); // 7

final var group = input.substring(start, end);
System.out.println(group); // "aaaaabc"
final var pattern = Pattern.compile("a+bc");
final var input = "012abcXYZ";

final var matcher = pattern.matcher(input);
System.out.println(matcher.find()); // true

final var start = matcher.start();
System.out.println(start); // 3

final var end = matcher.end();
System.out.println(end); // 6

final var group = input.substring(start, end);
System.out.println(group); // "abc"

int end (int group)

Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.

final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

System.out.println(matcher.start()); // 0
System.out.println(matcher.end()); // 11
System.out.println(matcher.group()); // "abc_012_xyz"

final var groupCount = matcher.groupCount();
System.out.println(groupCount); // 2

for (int i = 1; i <= groupCount; i++) {

    System.out.println("------");
    System.out.println("i : " + i);
    System.out.println("start : " + matcher.start(i));
    System.out.println("end : " + matcher.end(i));
    System.out.println("group : " + matcher.group(i));
}

// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz

int end (String name)

Returns the offset after the last character of the subsequence captured by the given named-capturing group during the previous match operation.

final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 11
System.out.println(result.group()); // "abc_012_xyz"

{
    final var name = "A";

    System.out.println(matcher.start(name)); // 0
    System.out.println(matcher.end(name)); // 3
    System.out.println(matcher.group(name)); // "abc"
}

{
    final var name = "B";

    System.out.println(matcher.start(name)); // 8
    System.out.println(matcher.end(name)); // 11
    System.out.println(matcher.group(name)); // "xyz"
}

boolean find ()

Attempts to find the next subsequence of the input sequence that matches the pattern.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaabc");

{
    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 0
    System.out.println(result.end()); // 3
    System.out.println(result.group()); // "abc"
}

{
    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 8
    System.out.println(result.end()); // 13
    System.out.println(result.group()); // "aaabc"
}

System.out.println(matcher.find()); // false

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.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("012 abc xyz aaabc");

{
    System.out.println(matcher.find(8)); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 12
    System.out.println(result.end()); // 17
    System.out.println(result.group()); // "aaabc"
}

{
    System.out.println(matcher.find(1)); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 4
    System.out.println(result.end()); // 7
    System.out.println(result.group()); // "abc"
}

String group ()

Returns the input subsequence matched by the previous match.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaabc xyz");

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aaabc"

System.out.println(matcher.find()); // false

String group (int group)

Returns the input subsequence captured by the given group during the previous match operation.

Please see end(int group).

String group (String name)

Returns the input subsequence captured by the given named-capturing group during the previous match operation.

Please see end(String name).

int groupCount ()

Returns the number of capturing groups in this matcher's pattern.

Please see end(int group).

boolean hasAnchoringBounds ()

Queries the anchoring of region bounds for this matcher.

final var pattern = Pattern.compile("^abc xyz$");
final var input = "012 abc xyz";

{
    final var matcher = pattern.matcher(input);

    System.out.println(matcher.hasAnchoringBounds()); // true

    System.out.println(input.substring(4, 11)); // "abc xyz"
    matcher.region(4, 11);

    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 4
    System.out.println(result.end()); // 11
    System.out.println(result.group()); // "abc xyz"
}

{
    final var matcher = pattern.matcher(input);

    matcher.useAnchoringBounds(false);
    System.out.println(matcher.hasAnchoringBounds()); // false

    matcher.region(4, 11);
    System.out.println(matcher.find()); // false
}

boolean hasTransparentBounds ()

Queries the transparency of region bounds for this matcher.

final var pattern = Pattern.compile("\\by...");
final var input = "xyz12 yz34";

{
    final var matcher = pattern.matcher(input);

    System.out.println(matcher.hasTransparentBounds()); // false

    System.out.println(input.substring(1, 10)); // "yz12 yz34"
    matcher.region(1, 10);

    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.group()); // "yz12"
}

{
    final var matcher = pattern.matcher(input);

    matcher.useTransparentBounds(true);
    System.out.println(matcher.hasTransparentBounds()); // true

    matcher.region(1, 10);

    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.group()); // "yz34"
}

boolean hitEnd ()

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

final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");

while (matcher.find()) {

    System.out.println("-------");
    System.out.println("hitEnd : " + matcher.hitEnd());

    final var result = matcher.toMatchResult();
    System.out.println("start : " + result.start());
    System.out.println("end : " + result.end());
}

// Result
// ↓
//-------
//hitEnd : false
//start : 4
//end : 7
//-------
//hitEnd : false
//start : 12
//end : 15

System.out.println(matcher.hitEnd()); // true

boolean lookingAt ()

Attempts to match the input sequence, starting at the beginning of the region, against the pattern.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("aaabc");

System.out.println(matcher.lookingAt()); // true

final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 5
System.out.println(result.group()); // "aaabc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abcXYZ");

System.out.println(matcher.lookingAt()); // true

final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 3
System.out.println(result.group()); // "abc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("012abc");

System.out.println(matcher.lookingAt()); // false

boolean matches ()

Attempts to match the entire region against the pattern.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc");

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 3
System.out.println(result.group()); // "abc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("aaaaabc");

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 7
System.out.println(result.group()); // "aaaaabc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("012abc");

System.out.println(matcher.matches()); // false
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abcZ");

System.out.println(matcher.matches()); // false

Pattern pattern ()

Returns the pattern that is interpreted by this matcher.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("aaabc");

System.out.println(matcher.pattern()); // "a+bc"

static String quoteReplacement (String s)

Returns a literal replacement String for the specified String.

final var pattern = Pattern.compile(" ");
final var matcher = pattern.matcher("abc 012 xyz");

final var replacement = Matcher.quoteReplacement("$");
System.out.println(replacement); // "\$"

final var ret = matcher.replaceAll(replacement);
System.out.println(ret); // "abc$012$xyz"

Matcher region (int start, int end)

Sets the limits of this matcher's region.

final var pattern = Pattern.compile("a+bc+");

final var input = "aaabccc";
final var matcher = pattern.matcher(input);

System.out.println(input.substring(2, 6)); // "abcc"
matcher.region(2, 6);

System.out.println(matcher.regionStart()); // 2
System.out.println(matcher.regionEnd()); // 6

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.group()); // "abcc"
final var pattern = Pattern.compile("a+bc+");
final var matcher = pattern.matcher("aaabccc");

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.group()); // "aaabccc"

int regionEnd ()

Reports the end index (exclusive) of this matcher's region.

Please see region(int start, int end).

int regionStart ()

Reports the start index of this matcher's region.

Please see region(int start, int end).

String replaceAll (String replacement)

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

final var pattern = Pattern.compile("a*b");
final var matcher = pattern.matcher("aabfooaabfooabfoob");

final var result = matcher.replaceAll("-");
System.out.println(result); // "-foo-foo-foo-"

String replaceAll (Function<MatchResult,String> replacer)

Replaces every subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence.

final var pattern = Pattern.compile("dog");
final var matcher = pattern.matcher("zzzdogzzzdogzzz");

final var result = matcher.replaceAll(mr -> mr.group().toUpperCase());
System.out.println(result); // "zzzDOGzzzDOGzzz"

String replaceFirst (String replacement)

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

final var pattern = Pattern.compile("dog");
final var matcher = pattern.matcher("zzzdogzzzdogzzz");

final var result = matcher.replaceFirst("cat");
System.out.println(result); // "zzzcatzzzdogzzz"

String replaceFirst (Function<MatchResult,String> replacer)

Replaces the first subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence.

final var pattern = Pattern.compile("dog");
final var matcher = pattern.matcher("zzzdogzzzdogzzz");

final var result = matcher.replaceFirst(mr -> mr.group().toUpperCase());
System.out.println(result); // "zzzDOGzzzdogzzz"

boolean requireEnd ()

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

final var pattern = Pattern.compile("abc$");
final var matcher = pattern.matcher("abc");

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"
System.out.println(matcher.requireEnd()); // true
final var pattern = Pattern.compile("abc");
final var matcher = pattern.matcher("abcd");

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"
System.out.println(matcher.requireEnd()); // false

Matcher reset ()

Resets this matcher.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc aabc");

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aabc"

System.out.println(matcher.find()); // false

matcher.reset();

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aabc"

System.out.println(matcher.find()); // false

Matcher reset (CharSequence input)

Resets this matcher with a new input sequence.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc aabc");

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aabc"

System.out.println(matcher.find()); // false

matcher.reset("aaaabc");

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aaaabc"

System.out.println(matcher.find()); // false

Stream<MatchResult> results ()

Returns a stream of match results for each subsequence of the input sequence that matches the pattern.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aabc");

final var stream = matcher.results();

stream.forEach(matchResult -> {
    System.out.println(matchResult.group());
});

// Result
// ↓
// "abc"
// "aabc"

int start ()

Returns the start index of the previous match.

Please see end().

int start (int group)

Returns the start index of the subsequence captured by the given group during the previous match operation.

Please see end(int group).

int start (String name)

Returns the start index of the subsequence captured by the given named-capturing group during the previous match operation.

Please see end(String name).

MatchResult toMatchResult ()

Returns the match state of this matcher as a MatchResult.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc aabc");

{
    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 0
    System.out.println(result.end()); // 3
    System.out.println(result.group()); // "abc"
}

{
    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 4
    System.out.println(result.end()); // 8
    System.out.println(result.group()); // "aabc"
}

String toString ()

Returns the string representation of this matcher.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc aabc");

System.out.println(matcher.find()); // true

final var str = matcher.toString();

// java.util.regex.Matcher[pattern=a+bc region=0,8 lastmatch=abc]
System.out.println(str);

Matcher useAnchoringBounds (boolean b)

Sets the anchoring of region bounds for this matcher.

Please see hasAnchoringBounds().

Matcher usePattern (Pattern newPattern)

Changes the Pattern that this Matcher uses to find matches with.

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("aabc xyz");

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aabc"

System.out.println(matcher.find()); // false

matcher.usePattern(Pattern.compile("x.."));

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "xyz"

Matcher useTransparentBounds (boolean b)

Sets the transparency of region bounds for this matcher.

Please see hasTransparentBounds().


Related posts

To top of page