← All Exercises

Java Regex Pattern Matching

intermediatejava~20min

Background

Java regex API provides compiled patterns for efficient repeated matching with group capture support.

Problem

Write a Java method that extracts all email addresses from a text using Pattern.compile and Matcher.find with groups.

Input

A string potentially containing email addresses.

Output

A list of extracted email address strings.

Sample tests

Sample 1

Input: "Contact: [email protected] or [email protected]"
Expected: ["[email protected]", "[email protected]"]

Your solution

Learning objective

Learn Pattern compilation, Matcher iteration with find, and the performance benefits of pattern reuse.