Reputation: 31
I've been trying to get this sorted out for quite some time.
Match strings that have even number of
a
s andb
s and contain substringaabb
defined overΣ = {a, b}
For the regex flavor, I'm using regexr to build the expression.
I have tried putting the substring between EVEN EVEN but it doesn't work as expected. For example, in even, for:
(aa|bb|(ab|ba)(aa|bb)*(ab|ba))*(aabb)(aa|bb|(ab|ba)(aa|bb)*(ab|ba))*
It doesn't work for strings where there are odd number of a
and b
before and after the substring. Like for aaabba
, it doesn't work even though there are four a
characters.
Some words of the language would be:
aaabba
, abaabbab
, aaaabbbaabbb
, ababaabb
Upvotes: 2
Views: 315
Reputation: 626738
You may use
^(?=(?:b*ab*a)*b*$)(?=(?:a*ba*b)*a*$)[ab]*aabb[ab]*$
See the JavaScript regex demo
Details
^
- start of string(?=(?:b*ab*a)*b*$)
- (even amount of a
check) up to the end of string, there can be
(?:b*ab*a)*
- 0 or more repetitions of two occurrences of 0 or more b
chars followed by a
and thenb*
- zero or more b
chars(?=(?:a*ba*b)*a*$)
- (even amount of b
check) up to the end of string, there can be any
(?:a*ba*b)*
- 0 or more repetitions of two occurrences of 0 or more a
chars followed by b
and thena*
- zero or more a
chars[ab]*
- 0 or more a
or b
charsaabb
- a required substring[ab]*
- 0 or more a
or b
chars$
- end of string.Bonus: to match the same with odd amount of a
and b
chars use
^(?=(?:b*ab*a)*b*ab*$)(?=(?:a*ba*b)*a*ba*$)[ab]*aabb[ab]*$
See the regex demo
Upvotes: 1