← All Exercises
Java Regex Pattern Matching
intermediatejava
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: alice@example.com or bob@test.org"
Expected: ["alice@example.com", "bob@test.org"]
Your solution
Learning objective
Learn Pattern compilation, Matcher iteration with find, and the performance benefits of pattern reuse.