I have 2 different instances of java.util.regex.Pattern. How do I combine them so that both apply?
And to be clear, combination means OR, but I intentionally left this question broad because I'd also like to know how to do AND or NOT.
Furthermore, this combined instance of Pattern is going to be doing a A LOT OF PROCESSING, so, performance matters here.
Finally, the obvious solution of just calling Pattern.pattern to extract the String representation of my Pattern DOES NOT WORK -- the 2 instances of Pattern might have different flags.
I have several ways that I could probably solve this. Here are the few that I came up with.
- Use both
Pattern::patternandPattern::flagsto effectively deserialize eachPattern, then use some logic to combine them together into a single instance.- Seems ugly and error-prone.
- Plus, I don't think this will work for
Pattern.LITERAL
- Don't combine them -- just call both individually, and use them together simultaneously.
- This actually feels like what the SDK is pushing me towards. For example, the existence of
Collectors.teeing(...)alongsideMatcher.results()means that it actually wouldn't be that hard to put together at all. - However, the idea of doing 2 separate passes feels wrong. These 2 instances of
Patternwill be processing a lot of data.
- This actually feels like what the SDK is pushing me towards. For example, the existence of
I also had a third idea to use inheritance/composition to solve this, but both Pattern and Matcher are final classes. The interface that Matcher implements is called MatchResult, and that is not final/sealed. But since that interface doesn't contain any of the regex logic in it, it doesn't really help me here.