First, let's agree on the terms. My syntactic assumption will be that
The intersection of several regular expressions is one regular expression that matches the lines that each of the component expressions also matches.
General parameter
To check the intersection of two patterns, a common method (pseudo-code):
if match(regex1) && match(regex2) { champagne for everyone! }
Regex Option
In some cases, you can do the same with lookaheads, but little can be done for complex regular expressions, besides making your regular expression more obscure for your enemies. Why is there little use? Because in any case, the engine will parse the entire string several times.
Logical and
A general pattern for checking AND that a string exactly matches regex1 and regex2 would be:
^(?=regex1$)(?=regex2$)
$ in each representation ensures that each line matches the pattern and nothing more.
Matching when AND
Of course, if you do not want to just check the logical value of AND, as well as perform the actual comparison, then after viewing you can add a star point to consume the string:
^(?=regex1$)(?=regex2$).*
Or ... After checking the first condition, simply match the second:
^(?=regex1$)regex2$
This is a method used, for example, when checking a password. For more on this, see Mastering Lookahead and Lookbehind .
Bonus Section: Regular Expression Union
Instead of working at the intersection, let's say you are interested in combining the following regular expressions, i.e. regular expression that matches any of these regular expressions:
This is done using the interleave operator | :
catch|cat1|cat2|cat3|cat5
In addition, such a regular expression can often be compressed, as in:
cat(?:ch|[1-35])