Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersection of Two Regex Classes

Tags:

python

regex

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
like image 409
Malik Brahimi Avatar asked Feb 28 '15 15:02

Malik Brahimi


People also ask

How do you find the intersection of two regular expressions?

The intersection of two regular set is regular. RE (L1 ∩ L2) = aa(aa)* which is a regular expression itself.

What is intersection in regex?

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! }

What is character class in regex?

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.


1 Answers

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.

like image 59
2 revsuser2629998 Avatar answered Sep 17 '22 22:09

2 revsuser2629998