How can create a regex class that is the intersection of two other regex classes? For example, how can I search for consonants with the [a-z]
and [^aeiou]
without explicitly constructing a regex class containing all the consonants like so:
[bcdfghjlkmnpqrstvwxyz] # explicit consonant regex class
The intersection of two regular set is regular. RE (L1 ∩ L2) = aa(aa)* which is a regular expression itself.
The intersection of several regexes is one regex that matches strings that each of the component regexes also match. The General Option. To check for the intersection of two patterns, the general method is (pseudo-code): if match(regex1) && match(regex2) { champagne for everyone! }
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.
This regex should do the trick : (?=[^aeiou])(?=[a-z])
.
The first group (?=...)
asserts that the pattern [^aeiou]
can be matched, then restarts the matching at the beginning and moves on to the second pattern (which works the same way), it's like a logical AND
, and the whole regex will only match if all of these two expressions match.
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