Java's Regex.Pattern supports the following character class:
[a-z&&[def]]
which matches "d, e, or f" and is called an intersection.
Functionally this is no different from:
[def]
which is simpler to read and understand in a big RE. So my question is, what use are intersections, other than specifying complete support for CSG-like operations on character classes?
(Please note, I understand the utility of subtractions like [a-z&&[^bc]]
and [a-z&&[^m-p]]
, I am asking specifically about intersections as presented above.)
In the context of regular expressions, a character class is a set of characters enclosed within square brackets. It specifies the characters that will successfully match a single character from a given input string.
Backslashes in Java. The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.
Character classes match any symbol from certain character sets e.g., \d , \s , and \w . The character classes \d , \s , and \w have the inverse classes \D , \S and \W that match other characters except \d , \s and \w . The dot( . ) matches any character except the newline character.
\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \. html?\ ' .
Though I've never had the need to do so, I could imagine a use with pre-defined character classes that aren't proper subsets of each other (thus making the intersection produce something different than the original two character classes). E.g. matching only lower case Latin characters:
[\p{Ll}&&\p{InBasicLatin}]
I believe that particular sample is just a "proof of concept." Two intersected character classes only match a character that matches both character sets individually. The substractions you mentioned are the real practical applications of the operator.
Simply put, there is no hidden meaning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With