Matcher Results Streaming Matches
Stream regex matches as MatchResult objects with Matcher.results, feeding the pipeline directly instead of collecting into a list first.
Code
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
URL Pattern MatcherTest Express-style URL patterns with :param and wildcard segments against concrete paths and extract the captured parameter values.Regular Expression TesterTest regular expressions against sample text with live match highlighting, capture group extraction, and flags support for global, case-insensitive, and multiline modes.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Nginx Location Block TesterTest any URL path against Nginx location blocks using authentic Nginx precedence (= exact, ^~ prefix, regex ~ and ~*, and longest prefix) with trace output.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.