With some regular expression mechanisms, you can use predefined routines (awkwardly) to define context-free grammars , although the syntax varies from engine to engine and is not standardized. Observe (still incomplete, but getting there):
(?(DEFINE) (?'all'(?&az)|(?&by)|(?&abzy)|(?&bayz)) (?'az'a(?&all)*z|z(?&all)*a) (?'by'b(?&all)*y|y(?&all)*b) (?'abzy' a(?&all)*b(?&all)*z(?&all)*y| a(?&all)*y(?&all)*z(?&all)*b| z(?&all)*b(?&all)*a(?&all)*y| z(?&all)*y(?&all)*a(?&all)*b ) (?'bayz' b(?&all)*a(?&all)*y(?&all)*z| b(?&all)*z(?&all)*y(?&all)*a| y(?&all)*a(?&all)*b(?&all)*z| y(?&all)*z(?&all)*b(?&all)*a ) ) ^(?&all)+$
Demo on Regex101
What this means is to define a set of subpatterns and apply them recursively. Using the ^ and $ anchors in the actual "pattern" ensures that the entire string matches. Simplicity.
Although, if you really do something like this in a production environment, someone might shoot you when they find him.
source share