Matcher Results Streaming Matches

Stream regex matches as MatchResult objects with Matcher.results, feeding the pipeline directly instead of collecting into a list first.

JavaData Fetching

Code

snippet
import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class TicketIds {

    public static void main(String[] args) {
        Pattern ticket = Pattern.compile("[A-Z]{2}-\\d{3,}");
        String changelog = """
                fixes AB-101, adds CD-20456
                reopens AB-102 and XY-9
                """;

        // results() 把每个匹配变成 MatchResult 流,直接进流水线
        Stream<MatchResult> matches = ticket.matcher(changelog).results();
        List<String> ids = matches
                .map(MatchResult::group)
                .distinct()
                .toList();
        System.out.println(ids);

        // group() 取整段匹配:编号部分从下标 3 起单独提取
        List<Integer> numbers = ticket.matcher(changelog).results()
                .map(m -> Integer.valueOf(m.group().substring(3)))
                .toList();
        System.out.println(numbers);
    }
}

How it works

Count the class TicketIds and the function main as the entire surface area of Matcher Results Streaming Matches.

Core lines

.map(m -> Integer.valueOf(m.group().substring(3)))
.toList();
.map(MatchResult::group)
.distinct()
.toList();

Interactive Tools for This Topic

FAQ

How do you run Matcher Results Streaming Matches?
Run the file on its own; every call resolves against the standard surface it already imports.