A regular expression that matches strings with the same number as z and b, like y

I am currently working on a book on regular expressions, and one of the practical tasks is to write a regular expression that matches strings that have the same number a as z and b as y. So far I have come up with the following regex.

^(?=[^az]*([az][^az]*[az][^az]*)*$)(?=[^by]*([by][^by]*[by][^by]*)*$).*$ 

The problem is that it does not match correctly when a and z are even and b and y are even (i.e. azzz will match, but has more z than a). Is there a way to change my regex to fit correctly, or am I not taking the wrong approach?

+5
source share
1 answer

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.

+1
source

Source: https://habr.com/ru/post/1259773/


All Articles