The intersection of character classes
The intersection operation on character classes results in a composite class that contains every character allowed by all of its operand (inner) classes or, in other words, matches characters that belong to all the character classes in the composite character class pattern. The intersection operator is as follows:
&&
For example, consider the following composite character class using the &&
operator:
[A-Z&&[PQR]]
This matches any character that is in the range of A
to Z
and is one of the single P
, Q
, or R
characters. However, the preceding regular expression can also be simply written as follows:
[PQR]
The following composite character class using intersection matches the digits, 5
and 6
, since only these two digits belong to all the three character classes:
[1-7&&[3-6]&&[5-8]]
To see this regex in action, let's use this complete code:
package example.regex; import java.util.regex.*; public class IntersectionExample...