I am working with java regexes, but I guess the principles apply for every regex.
I have these requirements for the segment a regex should match:
- have at least 3 times 'a'
- have at least 3 times 'b'
- occurrences of 'a' and 'b' can be in any order
Inspired by this post I came up with the following regex (regex101):
(?=([b]*[a]){3})(?=([a]*[b]){3})[ab]+
I am struggling with adding a new requirement:
- Match if there is no or at least 3 'c'
- as above, 'c' can occur anywhere in the segment
Examples for valid sequences:
aaabbb
ababab
aaabbbccc
abcabcabc
ababcabcc
Examples for invalid sequences (as a whole):
aaabbbc
aabbb
abbccc
abcabca
My thoughts so far:
Having at least 3 'c'
(?=([bc]*[a]){3})(?=([ac]*[b]){3})(?=([ab]*[c]){3,})[abc]+Combining this and above solution in a crude manner (regex101) which basically just a large "either none or at least 3"
((?=([bc]*[a]){3})(?=([ac]*[b]){3})(?=([ab]*[c]){3,})[abc]+|(?=([b]*[a]){3})(?=([a]*[b]){3})[ab]+)
Finally the Question: Is there a better way to achieve this using other methods, like or-ing the 'c'-requirement look-ahead, nested look-aheads or something entirely different?