title: How do you perform an intersection of two named regex character classes in Java?
body: <p>I know how to find the intersection of two constant sets from this <a href="https://docs.oracle.com/javase/tutorial/essential/regex/char_classes.html" rel="nofollow">link</a> to the Java tutorials.</p>
  
  <p>Example:</p>
  
  <pre><code> [0-9&amp;&amp;[345]]
  </code></pre>
  
  <p>Will produce a character class containing all of the characters which intersect the set of single digit integers ('[0-9]') and a subset of that set containing the integers 3, 4, and 5 ('[345]').</p>
  
  <p>This works fine for most needs, but it requires that both sets be known at compile-time. Let's say I'm writing a lexical analyzer for mathematical expressions and want to find the intersection between the set of unary operators and binary operators. Currently, the only operator that exists in both sets is '-'. As a binary operator '-' represents subtraction but as a unary operator it also represents negation. I anticipate that other operators may be pressed into dual-usage in the future, and want to develop a solution that not only works for the present case, but also the future case without changing the code. To do that, I need to be able to determine at run-time which operators exist in both sets.</p>
  
  <p>Let's say I have two character classes defined as follows:</p>
  
  <pre><code>public static final String UNARYOPS = "[!~-]";
  public static final String BINARYOPS = "[|&amp;%^*/+-]";
  </code></pre>
  
  <p>How would I find the intersection between these two named character sets using the regex engine in Java so that I can identify which operators need to be checked for ambiguity between binary and unary?</p>